-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsample_server.c
More file actions
86 lines (65 loc) · 2.6 KB
/
sample_server.c
File metadata and controls
86 lines (65 loc) · 2.6 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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void DieWithError(char* msg)
{
printf("%s\n", msg);
exit (1);
}
int main()
{
int host_sockid, client_sockid, status, num_bytes, clientaddr_size;
char buf[1024];
struct sockaddr_in hostaddr, clientaddr;
// Create socket of domain PF_INET, using TCP
// socket(family, type, protocol);
// family: PF_INET for IPv4 protocol
// type: SOCK_STREAM for a TCP socket
// protocol: SOCK_STREAM has only one protocol, thus using zero (first enum element)
host_sockid = socket(PF_INET, SOCK_STREAM, 0);
if(host_sockid == -1) DieWithError("[-] socket() failed\n");
printf("[+] Created socket for server with fd %d.\n", host_sockid);
// Initialize the sockaddr_in struct
// sin_family: AF_INET for IPv4 protocol
// sin_port: 1337 !111!!!
// sin_addr.s_addr: Any interface
hostaddr.sin_family = AF_INET;
hostaddr.sin_port = htons(1337);
hostaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Assign address to sockid using bind
// bind(sockid, &addrport, size);
// sockid: created by socket()
// addrport: address and port to which the socket will bind
// size: size of addrport struct
status = bind(host_sockid, (struct sockaddr*) &hostaddr, sizeof(hostaddr));
if(status == -1) DieWithError("[-] bind() failed\n");
printf("Binded successfully!\n");
// Listen for connections, should be used for a server ONLY
// listen(sockid, queuelimit);
// sockid: created by socket()
// queuelimit: max number of participants waiting for a connection
status = listen(host_sockid, 5);
if(status == -1) DieWithError("[-] listen() failed\n");
printf("[+] Currently listening...\n");
// Establish a connection initiated by client
// client_sockid = accept(sockid, &clientAddr, &addrLen);
// client_sockid: new socket
// sockid: existing socket
// clientAddr: struct defining the client, filled in upon return
// addrLen: size of clientAddr
clientaddr_size = sizeof(clientaddr);
client_sockid = accept(host_sockid, (struct sockaddr*) &clientaddr, &clientaddr_size);
if(status == -1) DieWithError("[-] accept() failed\n");
printf("[+] Connection established successfully! Client ip: %s, client port: %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
// Receive data
// count = recv(sockid, recvBuf, bufLen, flags)
num_bytes = recv(client_sockid, buf, 1024, 0);
if(num_bytes == -1) DieWithError("recv() failed\n");
// Close a socket
// close(sockid);
status = close(host_sockid);
if(status == -1) DieWithError("[-] close() failed.\n");
printf("[+] Closed connection successfully.\n");
return 0;
}