-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindows_server.c
More file actions
106 lines (80 loc) · 2.42 KB
/
windows_server.c
File metadata and controls
106 lines (80 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
// Winsock library
#pragma comment(lib, "ws2_32.lib")
#define MAX_PENDING 5
#define RECEIVE_BUFFER_SIZE 32
void error(char *message)
{
fprintf(stderr, "%s: %d\n", message, WSAGetLastError());
exit(1);
return;
}
void close(SOCKET so)
{
closesocket(so);
WSACleanup();
return;
}
void handle_client(int client_socket)
{
char buffer[RECEIVE_BUFFER_SIZE];
char message[200] = "Welcome to the server!\n";
int receive_message_size;
send(client_socket, message, strlen(message) + 1, 0);
while (strcmp(message, "QUIT") != 0)
{
if ((receive_message_size = recv(client_socket, buffer, RECEIVE_BUFFER_SIZE, 0)) < 0)
error("Error on receive data.\n");
buffer[receive_message_size] = '\0';
printf("[TCP Client]: %s\n", buffer);
scanf("%s", message);
printf("[Server]: %s\n", message);
send(client_socket, message, strlen(message) + 1, 0);
}
puts("Closing client socket..");
closesocket(client_socket);
}
int main(int argc, char *argv[])
{
SOCKET server_so, client_so;
WSADATA wsa;
char *ip;
int bytes_received;
unsigned int client_length;
unsigned short port;
struct sockaddr_in server, client;
if (argc != 3)
{
printf("Usage: %s <server_ip> <server_port>\n", argv[0]);
return 1;
}
ip = argv[1];
port = atoi(argv[2]);
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
error("Failed WSAStartup.\n");
printf("Winsock Initialised.\n");
if ((server_so = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
error("Failed socket.\n");
printf("Socket created.\n");
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ip);
server.sin_port = htons(port);
if (bind(server_so, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
error("Bind failed.\n");
if (listen(server_so, MAX_PENDING) < 0)
error("Listen failed.\n");
puts("Waiting for incoming connections..");
while (1)
{
client_length = sizeof(client);
if ((client_so = accept(server_so, (struct sockaddr *)&client, &client_length)) < 0)
error("Error on accept.\n");
printf("New connection accepted %s\n", inet_ntoa(client.sin_addr));
handle_client(client_so);
}
close(server_so);
return 0;
}