UDP data receiving Network programming

Status
Not open for further replies.

feiutm9898

Full Member level 4
Joined
May 31, 2004
Messages
224
Helped
4
Reputation
8
Reaction score
0
Trophy points
1,296
Location
Singapore
Activity points
2,027
UDP Network Programming

Hi. I need a Network programming receiving UDP data. I have one example found from a book as shown at below. I need to filter out the source and destination address for my project.

I am a newbie for LINUX programming. I hope some LINUX people can help me. I would like to have the example, referance link and any guidence. Thanks....


#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
char * host_name = “127.0.0.1”; // local host
void main() {
int sin_len;
int port = 8080;
char message[256];
int socket_descriptor;
struct sockaddr_in sin;
struct hostent *server_host_name;
server_host_name = gethostbyname(“127.0.0.1”);
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
// set socket using SOCK_DGRAM for UDP:
socket_descriptor = socket(PF_INET, SOCK_DGRAM, 0);
bind(socket_descriptor, (struct sockaddr *)&sin, sizeof(sin));
while (1) {
sin_len = sizeof(sin);
recvfrom(socket_descriptor, message, 256, 0,
(struct sockaddr *)&sin, &sin_len);
printf(“\nResponse from server:\n\n%s\n”, message);
if (strncmp(message, “stop”, 4) == 0) break;
}
close(socket_descriptor);
}
 

Re: UDP Network Programming

You can try the classic book on network programming by Richard Stevens
Internetworking with TCP/IP
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…