I would like to send and receive packets on the same socket, is this possible or do I need to create two sockets, one for sending and one for receiving? If so, can you give me an example?
Another question: how can I get the source ip from the received packet?
EDIT (sample code):
int main(void) { struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) die("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(1234); si_me.sin_addr.s_addr = htonl(192.168.1.1); if (bind(s, &si_me, sizeof(si_me))==-1) die("bind"); if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1) diep("recvfrom()"); printf("Data: %s \nReceived from %s:%d\n\n", buf, inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
source share