2020-07-22 05:59:18 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author [Nairit11](https://github.com/Nairit11)
|
|
|
|
* @author [Krishna Vedala](https://github.com/kvedala)
|
|
|
|
* @brief Server side implementation of Server-Client system.
|
|
|
|
* @see client_server/client.c
|
|
|
|
*/
|
2019-02-18 09:32:47 +03:00
|
|
|
#include <stdio.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-07-22 05:59:18 +03:00
|
|
|
|
|
|
|
// #ifdef HAS_UNISTD
|
|
|
|
// #include <unistd.h>
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS // will make the code invalid for next
|
|
|
|
// MSVC compiler versions
|
|
|
|
#include <winsock2.h>
|
|
|
|
#define bzero(b, len) \
|
|
|
|
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
|
|
|
|
#define read(a, b, c) recv(a, b, c, 0) /**< map BSD name to Winsock */
|
|
|
|
#define write(a, b, c) send(a, b, c, 0) /**< map BSD name to Winsock */
|
|
|
|
#define close closesocket /**< map BSD name to Winsock */
|
|
|
|
#else
|
|
|
|
// if not windows platform
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2020-07-22 05:59:18 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX 80 /**< max. characters per message */
|
|
|
|
#define PORT 8080 /**< port number to connect to */
|
|
|
|
#define SA struct sockaddr /**< shortname for sockaddr */
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Continuous loop to send and receive over the socket.
|
|
|
|
* Exits when "exit" is sent from commandline.
|
|
|
|
* @param sockfd socket handle number
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
void func(int sockfd)
|
|
|
|
{
|
|
|
|
char buff[MAX];
|
|
|
|
int n;
|
|
|
|
// infinite loop for chat
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
bzero(buff, MAX);
|
|
|
|
|
|
|
|
// read the message from client and copy it in buffer
|
|
|
|
read(sockfd, buff, sizeof(buff));
|
|
|
|
// print buffer which contains the client contents
|
|
|
|
printf("From client: %s\t To client : ", buff);
|
|
|
|
bzero(buff, MAX);
|
|
|
|
n = 0;
|
|
|
|
// copy server message in the buffer
|
|
|
|
while ((buff[n++] = getchar()) != '\n')
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
;
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
|
|
|
|
// and send that buffer to client
|
|
|
|
write(sockfd, buff, sizeof(buff));
|
|
|
|
|
|
|
|
// if msg contains "Exit" then server exit and chat ended.
|
|
|
|
if (strncmp("exit", buff, 4) == 0)
|
|
|
|
{
|
|
|
|
printf("Server Exit...\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
/** 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
|
|
|
|
|
|
|
|
int sockfd, connfd;
|
|
|
|
unsigned int len;
|
2020-05-29 23:23:24 +03:00
|
|
|
struct sockaddr_in servaddr, cli;
|
|
|
|
|
|
|
|
// socket create and verification
|
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sockfd == -1)
|
|
|
|
{
|
2020-07-22 05:59:18 +03:00
|
|
|
perror("socket creation failed...\n");
|
2020-05-29 23:23:24 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Socket successfully created..\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
|
|
|
|
// assign IP, PORT
|
|
|
|
servaddr.sin_family = AF_INET;
|
|
|
|
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
servaddr.sin_port = htons(PORT);
|
|
|
|
|
|
|
|
// Binding newly created socket to given IP and verification
|
|
|
|
if ((bind(sockfd, (SA *)&servaddr, sizeof(servaddr))) != 0)
|
|
|
|
{
|
2020-07-22 05:59:18 +03:00
|
|
|
perror("socket bind failed...\n");
|
2020-05-29 23:23:24 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Socket successfully binded..\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
|
|
|
|
// Now server is ready to listen and verification
|
|
|
|
if ((listen(sockfd, 5)) != 0)
|
|
|
|
{
|
2020-07-22 05:59:18 +03:00
|
|
|
perror("Listen failed...\n");
|
2020-05-29 23:23:24 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Server listening..\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
len = sizeof(cli);
|
|
|
|
|
|
|
|
// Accept the data packet from client and verification
|
|
|
|
connfd = accept(sockfd, (SA *)&cli, &len);
|
|
|
|
if (connfd < 0)
|
|
|
|
{
|
2020-07-22 05:59:18 +03:00
|
|
|
perror("server acccept failed...\n");
|
2020-05-29 23:23:24 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("server acccept the client...\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
|
|
|
|
// Function for chatting between client and server
|
|
|
|
func(connfd);
|
|
|
|
|
|
|
|
// After chatting close the socket
|
|
|
|
close(sockfd);
|
2020-07-22 05:59:18 +03:00
|
|
|
return 0;
|
2020-05-29 23:23:24 +03:00
|
|
|
}
|