-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_server.c
78 lines (62 loc) · 1.93 KB
/
tcp_server.c
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
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void) {
int socket_desc, client_sock, client_size;
struct sockaddr_in server_addr, client_addr;
char server_message[2000], client_message[2000];
// Clean buffers
memset(server_message, '\0', sizeof(server_message));
memset(client_message, '\0', sizeof(client_message));
// Create Socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc < 0) {
printf("Error while creating socket.\n");
return -1;
}
printf("Socket Created Succesfully !!\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(2000);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Bind to the set port and IP
if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
0) {
printf("Couldn't bind to the port.\n");
return -1;
}
printf("Done with binding\n");
// Listen for clients:
if (listen(socket_desc, 1) < 0) {
printf("Error while listening\n");
return -1;
}
printf("\nListening for incoming connections...\n");
// Accept an incoming connection.
client_size = sizeof(client_addr);
client_sock =
accept(socket_desc, (struct sockaddr *)&client_addr, &client_size);
if (client_sock < 0) {
printf("Can't accept!\n");
return -1;
}
printf("Client connected at IP: %s and port: %i\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Recieve Message
if (recv(client_sock, client_message, sizeof(client_message), 0) < 0) {
printf("Can't Send");
return -1;
}
printf("Msg from Client: %s\n", client_message);
// Respond to Client
strcpy(server_message, "Hello from the Server");
if (send(client_sock, server_message, strlen(server_message), 0) < 0) {
printf("Can't Send.\n");
return -1;
}
// Closing the Connection
close(client_sock);
close(socket_desc);
return 0;
}