how to get client port number in server socket programming??
Answers were Sorted based on User's Feedback
Answer / nainesh
After accepting connection on socket of server side. we can
get the client ip address and port by to functions. These
functions are belonging in "/usr/include/arpa/inet.h" header
file.
Here is some scratch from the code.
socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
printf("%s\n",inet_ntoa(client.sin_addr));
printf("%d\n",(int) ntohs(client.sin_port));
Is This Answer Correct ? | 14 Yes | 3 No |
Answer / supraja aldena
After accepting connection.Just type this, u wil get
client ip address and port number.
printf("\n(%s , %d)\n",inet_ntoa(client.sin_addr),ntohs
(client.sin_port));
Is This Answer Correct ? | 8 Yes | 0 No |
Answer / raguraman sify tech
Please elaborate this question.. what is your exact request?..
Is This Answer Correct ? | 9 Yes | 3 No |
Answer / rps
@Maddy :-)
After the Accept Call in server Program..
ClientFD = accept(SockFD,(struct sockaddr *)&ClientAddr,(int*)&SinSize);
You will get Client IP and Port Number using this --
printf(" Server got a connection from Client (%s : %d)\n",
inet_ntoa(ClientAddr.sin_addr),ntohs(ClientAddr.sin_port));
Is This Answer Correct ? | 3 Yes | 3 No |
Are sockets bidirectional?
Why bind system call is required in socket programming?
Can you send and receive on the same socket at the same time?
Q. what is the difference between socket & port ?
How to Test the Datagram Client and Server?
What is the use of PF_INET or AF_INET?
How to Reply to a Wild Address?
What is socket linux?
How to Choose a Socket Type?
What is the use of endservent(3) Function?
How to Bind Addresses to a Socket?
What is the Use of the inet_lnaof() Function?