Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<arpa/inet.h>
- #include<sys/socket.h>
- #define BUFLEN 512
- #define PORT 8888
- int main(void)
- {
- struct sockaddr_in si_me;
- memset(&si_me, 0, sizeof(struct sockaddr_in));
- si_me.sin_family = AF_INET;
- si_me.sin_port = htons(PORT);
- si_me.sin_addr.s_addr = htonl(INADDR_ANY);
- struct sockaddr_in si_other;
- int i = sizeof(si_other);
- int slen = recv_len;
- char buf[BUFLEN];
- //create a UDP socket
- int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
- //bind socket to port
- bind(s, (struct sockaddr*) &si_me, sizeof(si_me) )
- //keep listening for data
- while(1)
- {
- printf("Waiting for data...");
- fflush(stdout);
- recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)
- printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
- printf("Data: %s\n" , buf);
- //now reply the client with the same data
- sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen)
- }
- close(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement