Server side implementation of UDP client-server model.
More...
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
#define | PORT 8080 |
| port number to connect to
|
|
#define | MAXLINE 1024 |
| maximum characters per line
|
|
Server side implementation of UDP client-server model.
- Author
- TheShubham99
-
Krishna Vedala
- See also
- client_server/udp_client.c
◆ main()
Driver code.
40 if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
42 perror(
"WSA Startup error: \n");
51 char *hello =
"Hello from server";
52 struct sockaddr_in servaddr, cliaddr;
55 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
57 perror(
"socket creation failed");
61 memset(&servaddr, 0,
sizeof(servaddr));
62 memset(&cliaddr, 0,
sizeof(cliaddr));
65 servaddr.sin_family = AF_INET;
66 servaddr.sin_addr.s_addr = INADDR_ANY;
67 servaddr.sin_port = htons(
PORT);
70 if (bind(sockfd, (
const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0)
72 perror(
"bind failed");
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");
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