Skip to content

Commit 83a8239

Browse files
kvedalagithub-actions
andauthored
[enhancement] Client_server folder code updated for windows OS as well. (#577)
* update codes to run on Windows platform as well * added cmake for client_server * added scope parameters * force use of unistd.h in non-windows * use size_t instead of int * use unsigned int instead of size_t * clang-tidy fixes for ac0991e * updated UDP server-client as well * use unsigned int * added documentation * spell correction Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent e43024e commit 83a8239

File tree

6 files changed

+261
-38
lines changed

6 files changed

+261
-38
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_subdirectory(sorting)
3030
add_subdirectory(graphics)
3131
add_subdirectory(searching)
3232
add_subdirectory(conversions)
33+
add_subdirectory(client_server)
3334
add_subdirectory(project_euler)
3435
add_subdirectory(machine_learning)
3536
add_subdirectory(numerical_methods)

client_server/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# include(CheckIncludeFile)
2+
# check_include_file(arpa/inet.h ARPA_HEADERS)
3+
# if(NOT ARPA_HEADERS)
4+
# check_include_file(winsock2.h WINSOCK_HEADER)
5+
# if(NOT WINSOCK_HEADER)
6+
# message(FATAL_ERROR "socket headers not found in system.")
7+
# endif()
8+
# endif()
9+
10+
# check_include_file(unistd.h HAS_UNISTD)
11+
12+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
13+
# with full pathname. RELATIVE may makes it easier to extract an executable name
14+
# automatically.
15+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
16+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
17+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
18+
foreach( testsourcefile ${APP_SOURCES} )
19+
# I used a simple string replace, to cut off .cpp.
20+
string( REPLACE ".c" "" testname ${testsourcefile} )
21+
add_executable( ${testname} ${testsourcefile} )
22+
23+
if(OpenMP_C_FOUND)
24+
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
25+
endif()
26+
if(MATH_LIBRARY)
27+
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
28+
endif()
29+
30+
if(HAS_UNISTD)
31+
target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
32+
endif()
33+
# if(ARPA_HEADERS)
34+
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
35+
# else()
36+
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
37+
# endif()
38+
39+
if(WIN32)
40+
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
41+
endif()
42+
43+
install(TARGETS ${testname} DESTINATION "bin/client_server")
44+
45+
endforeach( testsourcefile ${APP_SOURCES} )

client_server/client.c

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
// Write CPP code here
2-
#include <arpa/inet.h>
3-
#include <netdb.h>
1+
/**
2+
* @file
3+
* @author [Nairit11](https://github.com/Nairit11)
4+
* @author [Krishna Vedala](https://github.com/kvedala)
5+
* @brief Client side implementation of Server-Client system.
6+
* @see client_server/server.c
7+
*/
48
#include <stdio.h>
59
#include <stdlib.h>
610
#include <string.h>
11+
12+
#ifdef _WIN32 // if compiling for Windows
13+
#define _WINSOCK_DEPRECATED_NO_WARNINGS // will make the code invalid for next
14+
// MSVC compiler versions
15+
#include <winsock2.h>
16+
#define bzero(b, len) \
17+
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
18+
#define read(a, b, c) recv(a, b, c, 0) /**< map BSD name to Winsock */
19+
#define write(a, b, c) send(a, b, c, 0) /**< map BSD name to Winsock */
20+
#define close closesocket /**< map BSD name to Winsock */
21+
#else // if not windows platform
22+
#include <arpa/inet.h>
23+
#include <netdb.h>
724
#include <sys/socket.h>
825
#include <unistd.h>
9-
#define MAX 80
10-
#define PORT 8080
11-
#define SA struct sockaddr
26+
#endif
27+
28+
#define MAX 80 /**< max. characters per message */
29+
#define PORT 8080 /**< port number to connect to */
30+
#define SA struct sockaddr /**< shortname for sockaddr */
31+
32+
/**
33+
* Continuous loop to send and receive over the socket.
34+
* Exits when "exit" is sent from commandline.
35+
* @param sockfd socket handle number
36+
*/
1237
void func(int sockfd)
1338
{
1439
char buff[MAX];
@@ -19,7 +44,9 @@ void func(int sockfd)
1944
printf("Enter the string : ");
2045
n = 0;
2146
while ((buff[n++] = getchar()) != '\n')
47+
{
2248
;
49+
}
2350
write(sockfd, buff, sizeof(buff));
2451
bzero(buff, sizeof(buff));
2552
read(sockfd, buff, sizeof(buff));
@@ -32,20 +59,42 @@ void func(int sockfd)
3259
}
3360
}
3461

62+
#ifdef _WIN32
63+
/** Cleanup function will be automatically called on program exit */
64+
void cleanup() { WSACleanup(); }
65+
#endif
66+
67+
/**
68+
* @brief Driver code
69+
*/
3570
int main()
3671
{
72+
#ifdef _WIN32
73+
// when using winsock2.h, startup required
74+
WSADATA wsData;
75+
if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
76+
{
77+
perror("WSA Startup error: \n");
78+
return 0;
79+
}
80+
81+
atexit(cleanup); // register at-exit function
82+
#endif
83+
3784
int sockfd, connfd;
3885
struct sockaddr_in servaddr, cli;
3986

40-
// socket create and varification
87+
// socket create and verification
4188
sockfd = socket(AF_INET, SOCK_STREAM, 0);
4289
if (sockfd == -1)
4390
{
4491
printf("socket creation failed...\n");
4592
exit(0);
4693
}
4794
else
95+
{
4896
printf("Socket successfully created..\n");
97+
}
4998
bzero(&servaddr, sizeof(servaddr));
5099

51100
// assign IP, PORT
@@ -60,11 +109,14 @@ int main()
60109
exit(0);
61110
}
62111
else
112+
{
63113
printf("connected to the server..\n");
114+
}
64115

65116
// function for chat
66117
func(sockfd);
67118

68119
// close the socket
69120
close(sockfd);
121+
return 0;
70122
}

client_server/server.c

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
1-
#include <netdb.h>
2-
#include <netinet/in.h>
1+
/**
2+
* @file
3+
* @author [Nairit11](https://github.com/Nairit11)
4+
* @author [Krishna Vedala](https://github.com/kvedala)
5+
* @brief Server side implementation of Server-Client system.
6+
* @see client_server/client.c
7+
*/
38
#include <stdio.h>
49
#include <stdlib.h>
510
#include <string.h>
11+
12+
// #ifdef HAS_UNISTD
13+
// #include <unistd.h>
14+
// #endif
15+
16+
#ifdef _WIN32
17+
#define _WINSOCK_DEPRECATED_NO_WARNINGS // will make the code invalid for next
18+
// MSVC compiler versions
19+
#include <winsock2.h>
20+
#define bzero(b, len) \
21+
(memset((b), '\0', (len)), (void)0) /**< BSD name not in windows */
22+
#define read(a, b, c) recv(a, b, c, 0) /**< map BSD name to Winsock */
23+
#define write(a, b, c) send(a, b, c, 0) /**< map BSD name to Winsock */
24+
#define close closesocket /**< map BSD name to Winsock */
25+
#else
26+
// if not windows platform
27+
#include <arpa/inet.h>
28+
#include <netdb.h>
629
#include <sys/socket.h>
7-
#include <sys/types.h>
830
#include <unistd.h>
9-
#define MAX 80
10-
#define PORT 8080
11-
#define SA struct sockaddr
31+
#endif
32+
33+
#define MAX 80 /**< max. characters per message */
34+
#define PORT 8080 /**< port number to connect to */
35+
#define SA struct sockaddr /**< shortname for sockaddr */
1236

13-
// Function designed for chat between client and server.
37+
#ifdef _WIN32
38+
/** Cleanup function will be automatically called on program exit */
39+
void cleanup() { WSACleanup(); }
40+
#endif
41+
42+
/**
43+
* Continuous loop to send and receive over the socket.
44+
* Exits when "exit" is sent from commandline.
45+
* @param sockfd socket handle number
46+
*/
1447
void func(int sockfd)
1548
{
1649
char buff[MAX];
@@ -28,7 +61,9 @@ void func(int sockfd)
2861
n = 0;
2962
// copy server message in the buffer
3063
while ((buff[n++] = getchar()) != '\n')
64+
{
3165
;
66+
}
3267

3368
// and send that buffer to client
3469
write(sockfd, buff, sizeof(buff));
@@ -42,21 +77,36 @@ void func(int sockfd)
4277
}
4378
}
4479

45-
// Driver function
80+
/** Driver code */
4681
int main()
4782
{
48-
int sockfd, connfd, len;
83+
#ifdef _WIN32
84+
// when using winsock2.h, startup required
85+
WSADATA wsData;
86+
if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
87+
{
88+
perror("WSA Startup error: \n");
89+
return 0;
90+
}
91+
92+
atexit(cleanup); // register at-exit function
93+
#endif
94+
95+
int sockfd, connfd;
96+
unsigned int len;
4997
struct sockaddr_in servaddr, cli;
5098

5199
// socket create and verification
52100
sockfd = socket(AF_INET, SOCK_STREAM, 0);
53101
if (sockfd == -1)
54102
{
55-
printf("socket creation failed...\n");
103+
perror("socket creation failed...\n");
56104
exit(0);
57105
}
58106
else
107+
{
59108
printf("Socket successfully created..\n");
109+
}
60110
bzero(&servaddr, sizeof(servaddr));
61111

62112
// assign IP, PORT
@@ -67,35 +117,42 @@ int main()
67117
// Binding newly created socket to given IP and verification
68118
if ((bind(sockfd, (SA *)&servaddr, sizeof(servaddr))) != 0)
69119
{
70-
printf("socket bind failed...\n");
120+
perror("socket bind failed...\n");
71121
exit(0);
72122
}
73123
else
124+
{
74125
printf("Socket successfully binded..\n");
126+
}
75127

76128
// Now server is ready to listen and verification
77129
if ((listen(sockfd, 5)) != 0)
78130
{
79-
printf("Listen failed...\n");
131+
perror("Listen failed...\n");
80132
exit(0);
81133
}
82134
else
135+
{
83136
printf("Server listening..\n");
137+
}
84138
len = sizeof(cli);
85139

86140
// Accept the data packet from client and verification
87141
connfd = accept(sockfd, (SA *)&cli, &len);
88142
if (connfd < 0)
89143
{
90-
printf("server acccept failed...\n");
144+
perror("server acccept failed...\n");
91145
exit(0);
92146
}
93147
else
148+
{
94149
printf("server acccept the client...\n");
150+
}
95151

96152
// Function for chatting between client and server
97153
func(connfd);
98154

99155
// After chatting close the socket
100156
close(sockfd);
157+
return 0;
101158
}

client_server/udp_client.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1-
// Client side implementation of UDP client-server model
1+
/**
2+
* @file
3+
* @author [TheShubham99](https://github.com/TheShubham99)
4+
* @author [Krishna Vedala](https://github.com/kvedala)
5+
* @brief Client side implementation of UDP client-server model
6+
* @see client_server/udp_server.c
7+
*/
8+
#ifdef _WIN32 // if compiling for Windows
9+
#define _WINSOCK_DEPRECATED_NO_WARNINGS // will make the code invalid for next
10+
// MSVC compiler versions
11+
#include <winsock2.h>
12+
#define close closesocket /**< map BSD name to Winsock */
13+
#else // if not windows platform
214
#include <arpa/inet.h>
15+
#include <netdb.h>
316
#include <netinet/in.h>
4-
#include <stdio.h>
5-
#include <stdlib.h>
6-
#include <string.h>
717
#include <sys/socket.h>
818
#include <sys/types.h>
919
#include <unistd.h>
20+
#endif
21+
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <string.h>
25+
26+
#define PORT 8080 /**< port number to connect to */
27+
#define MAXLINE 1024 /**< maximum characters per line */
1028

11-
#define PORT 8080
12-
#define MAXLINE 1024
29+
#ifdef _WIN32
30+
/** Cleanup function will be automatically called on program exit */
31+
void cleanup() { WSACleanup(); }
32+
#endif
1333

14-
// Driver code
34+
/** Driver code */
1535
int main()
1636
{
37+
#ifdef _WIN32
38+
// when using winsock2.h, startup required
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); // register at-exit function
47+
#endif
48+
1749
int sockfd;
1850
char buffer[MAXLINE];
1951
char *hello = "Hello from client";
@@ -33,9 +65,10 @@ int main()
3365
servaddr.sin_port = htons(PORT);
3466
servaddr.sin_addr.s_addr = INADDR_ANY;
3567

36-
int n, len;
68+
int n;
69+
unsigned int len;
3770

38-
sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM,
71+
sendto(sockfd, (const char *)hello, strlen(hello), 0,
3972
(const struct sockaddr *)&servaddr, sizeof(servaddr));
4073
printf("Hello message sent.\n");
4174

0 commit comments

Comments
 (0)