2020-07-22 05:59:18 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author [TheShubham99](https://github.com/TheShubham99)
|
|
|
|
* @author [Krishna Vedala](https://github.com/kvedala)
|
|
|
|
* @brief Server side implementation of UDP client-server model
|
|
|
|
* @see client_server/udp_client.c
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32 // if compiling for Windows
|
|
|
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS // will make the code invalid for next
|
|
|
|
// MSVC compiler versions
|
|
|
|
#define close closesocket /**< map BSD name to Winsock */
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else // if not windows platform
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <arpa/inet.h>
|
2020-07-22 05:59:18 +03:00
|
|
|
#include <netdb.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2020-07-22 05:59:18 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
#define PORT 8080 /**< port number to connect to */
|
|
|
|
#define MAXLINE 1024 /**< maximum characters per line */
|
2020-05-29 23:23:24 +03:00
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
/** Cleanup function will be automatically called on program exit */
|
|
|
|
void cleanup() { WSACleanup(); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Driver code */
|
2020-05-29 23:23:24 +03:00
|
|
|
int main()
|
|
|
|
{
|
2020-07-22 05:59:18 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
// when using winsock2.h, startup required
|
|
|
|
WSADATA wsData;
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
|
|
|
|
{
|
|
|
|
perror("WSA Startup error: \n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(cleanup); // register at-exit function
|
|
|
|
#endif
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int sockfd;
|
|
|
|
char buffer[MAXLINE];
|
|
|
|
char *hello = "Hello from server";
|
|
|
|
struct sockaddr_in servaddr, cliaddr;
|
|
|
|
|
|
|
|
// Creating socket file descriptor
|
|
|
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
perror("socket creation failed");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&servaddr, 0, sizeof(servaddr));
|
|
|
|
memset(&cliaddr, 0, sizeof(cliaddr));
|
|
|
|
|
|
|
|
// Filling server information
|
2020-06-28 18:25:37 +03:00
|
|
|
servaddr.sin_family = AF_INET; // IPv4
|
2020-05-29 23:23:24 +03:00
|
|
|
servaddr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
servaddr.sin_port = htons(PORT);
|
|
|
|
|
|
|
|
// Bind the socket with the server address
|
|
|
|
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
|
|
|
|
{
|
|
|
|
perror("bind failed");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
unsigned int len;
|
|
|
|
int n;
|
2020-05-29 23:23:24 +03:00
|
|
|
n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL,
|
|
|
|
(struct sockaddr *)&cliaddr, &len);
|
|
|
|
buffer[n] = '\0';
|
|
|
|
printf("Client : %s\n", buffer);
|
2020-07-22 05:59:18 +03:00
|
|
|
sendto(sockfd, (const char *)hello, strlen(hello), 0,
|
2020-05-29 23:23:24 +03:00
|
|
|
(const struct sockaddr *)&cliaddr, len);
|
|
|
|
printf("Hello message sent.\n");
|
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
close(sockfd);
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|