2020-07-22 05:59:18 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author [Nairit11](https://github.com/Nairit11)
|
|
|
|
* @author [Krishna Vedala](https://github.com/kvedala)
|
|
|
|
* @brief Client side implementation of Server-Client system.
|
|
|
|
* @see client_server/server.c
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-04-08 16:41:12 +03:00
|
|
|
#include <string.h>
|
2020-07-22 05:59:18 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32 // if compiling for Windows
|
|
|
|
#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-04-08 16:41:12 +03:00
|
|
|
#include <sys/socket.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Continuous loop to send and receive over the socket.
|
|
|
|
* Exits when "exit" is sent from commandline.
|
|
|
|
* @param sockfd socket handle number
|
|
|
|
*/
|
2020-04-08 16:41:12 +03:00
|
|
|
void func(int sockfd)
|
|
|
|
{
|
|
|
|
char buff[MAX];
|
|
|
|
int n;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
bzero(buff, sizeof(buff));
|
|
|
|
printf("Enter the string : ");
|
|
|
|
n = 0;
|
|
|
|
while ((buff[n++] = getchar()) != '\n')
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
;
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
write(sockfd, buff, sizeof(buff));
|
|
|
|
bzero(buff, sizeof(buff));
|
|
|
|
read(sockfd, buff, sizeof(buff));
|
|
|
|
printf("From Server : %s", buff);
|
|
|
|
if ((strncmp(buff, "exit", 4)) == 0)
|
|
|
|
{
|
|
|
|
printf("Client Exit...\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
/** Cleanup function will be automatically called on program exit */
|
|
|
|
void cleanup() { WSACleanup(); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Driver code
|
|
|
|
*/
|
2020-04-08 16:41:12 +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-04-08 16:41:12 +03:00
|
|
|
int sockfd, connfd;
|
|
|
|
struct sockaddr_in servaddr, cli;
|
|
|
|
|
2020-07-22 05:59:18 +03:00
|
|
|
// socket create and verification
|
2020-04-08 16:41:12 +03:00
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sockfd == -1)
|
|
|
|
{
|
|
|
|
printf("socket creation failed...\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
printf("Socket successfully created..\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
|
|
|
|
// assign IP, PORT
|
|
|
|
servaddr.sin_family = AF_INET;
|
|
|
|
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
|
|
servaddr.sin_port = htons(PORT);
|
|
|
|
|
|
|
|
// connect the client socket to server socket
|
|
|
|
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
|
|
|
|
{
|
|
|
|
printf("connection with the server failed...\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-20 11:11:26 +03:00
|
|
|
else
|
2020-07-22 05:59:18 +03:00
|
|
|
{
|
2020-04-08 16:41:12 +03:00
|
|
|
printf("connected to the server..\n");
|
2020-07-22 05:59:18 +03:00
|
|
|
}
|
2020-04-08 16:41:12 +03:00
|
|
|
|
|
|
|
// function for chat
|
|
|
|
func(sockfd);
|
|
|
|
|
|
|
|
// close the socket
|
|
|
|
close(sockfd);
|
2020-07-22 05:59:18 +03:00
|
|
|
return 0;
|
2020-04-08 16:41:12 +03:00
|
|
|
}
|