-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.c
55 lines (42 loc) · 1.59 KB
/
functions.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
#include <headers.h>
#include <colours.h>
#include <userdefs.h>
Connection_Details Establish_Connection_as_Server (Connection_Details Details)
{
struct sockaddr_in server_addr;
Details.Server_socket = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = Details.port;
server_addr.sin_addr.s_addr = inet_addr(Details.IP_address);
bind(Details.Server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
listen(Details.Server_socket, 1);
printf(GRN"\n--- Server Connection started ---\n" reset);
return Details;
}
Connection_Details Accept_Connection (Connection_Details Details)
{
struct sockaddr_in client_addr;
socklen_t addr_size = sizeof(client_addr);
Details.Client_socket = accept (Details.Server_socket, (struct sockaddr *) &client_addr, &addr_size);
Details.Comm_socket = Details.Client_socket;
printf(BLU"\n--- Client Found ---\n"reset);
return Details;
}
Connection_Details Establish_Connection_as_Client (Connection_Details Details)
{
int port = Details.port;
char *ip = Details.IP_address;
struct sockaddr_in addr;
socklen_t addr_size;
int sock = socket(AF_INET, SOCK_STREAM, 0);
Details.Client_socket = sock;
Details.Comm_socket = sock;
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock, (struct sockaddr *)&addr, sizeof(addr));
printf(GRN"\n --- Client Connection Started --- \n"reset);
return Details;
}