krackoder
krackoder

Reputation: 2981

Obtaining IP address after broadcasting using C in linux

I have written a simple client server program in C under linux. I have created UDP sockets. The client broadcasts a message using

sendto(clientsock, buf, 100, 0, (struct sockaddr *)&to, sizeof (to)); 

The server receives the broadcasted message using

recvfrom(serversock, buf, 100, 0, (struct sockaddr *)&rcv,&size);

The message is being successfully received by the server. On the server side, I need to find the source IP address of the message received. How can I do that ?

Upvotes: 2

Views: 416

Answers (1)

ayush
ayush

Reputation: 14568

for a call like -

recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)

use

printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);

Upvotes: 5

Related Questions