Driver code.
36{
37#ifdef _WIN32
38
39 WSADATA wsData;
40 if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
41 {
42 perror("WSA Startup error: \n");
43 return 0;
44 }
45
46 atexit(cleanup);
47#endif
48
49 int sockfd;
51 char *hello = "Hello from server";
52 struct sockaddr_in servaddr, cliaddr;
53
54
55 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
56 {
57 perror("socket creation failed");
58 exit(EXIT_FAILURE);
59 }
60
61 memset(&servaddr, 0, sizeof(servaddr));
62 memset(&cliaddr, 0, sizeof(cliaddr));
63
64
65 servaddr.sin_family = AF_INET;
66 servaddr.sin_addr.s_addr = INADDR_ANY;
67 servaddr.sin_port = htons(
PORT);
68
69
70 if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
71 {
72 perror("bind failed");
73 exit(EXIT_FAILURE);
74 }
75
76 unsigned int len;
77 int n;
79 (struct sockaddr *)&cliaddr, &len);
81 printf(
"Client : %s\n",
buffer);
82 sendto(sockfd, (const char *)hello, strlen(hello), 0,
83 (const struct sockaddr *)&cliaddr, len);
84 printf("Hello message sent.\n");
85
86 close(sockfd);
87
88 return 0;
89}
struct used to store character in certain times
Definition: min_printf.h:31
#define MAXLINE
maximum characters per line
Definition: udp_server.c:27
#define PORT
port number to connect to
Definition: udp_server.c:26