feiutm9898
Full Member level 4
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);
}
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);
}