-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
137 lines (104 loc) · 3.23 KB
/
main.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <plibsys.h>
#include <psocket.h>
#define MAX_MESSAGE_LENGTH 1024
#define SERVER_PORT 5432
int do_server_things()
{
char buffer[MAX_MESSAGE_LENGTH + 1]; // Supports messages up to max length (plus null character that terminates the string, since we're sending text)
PSocketAddress* addr;
PSocket* sock;
// Binding socket to local host (we are a server, the appropriate port. Typically this will always be a localhost port, because we are going to listen to this)
if ((addr = p_socket_address_new("127.0.0.1", SERVER_PORT)) == NULL)
return 1;
// Create socket
if ((sock = p_socket_new(P_SOCKET_FAMILY_INET, P_SOCKET_TYPE_STREAM, P_SOCKET_PROTOCOL_TCP, NULL)) == NULL)
{
// Failed to create socket -- cleanup
p_socket_address_free(addr);
return 2;
}
// Bind to local host (server) socket
if (!p_socket_bind(sock, addr, FALSE, NULL))
{
// Couldn't bind socket, cleanup
p_socket_address_free(addr);
p_socket_free(sock);
return 3;
}
// Listen for incoming connections on localhost SERVER_PORT
if (!p_socket_listen(sock, NULL))
{
// Couldn't start listening, cleanup
p_socket_address_free(addr);
p_socket_close(sock, NULL);
return 4;
}
// Forever, try to accept incoming connections.
while (1)
{
// Blocks until connection accept happens by default -- this can be changed
PSocket* con = p_socket_accept(sock, NULL);
if (con != NULL)
{
// Send "Message from server" to the client, and terminate their connection.
strcpy(buffer, "Message from server");
p_socket_send(con, buffer, strlen(buffer), NULL);
p_socket_close(con, NULL);
}
else
printf("Can't make con, tried and failed...\n");
}
// Cleanup
p_socket_address_free(addr);
p_socket_close(sock, NULL);
return 0;
}
int do_client_things()
{
char buffer[MAX_MESSAGE_LENGTH + 1]; // Supports messages up to max message length characters long (plus null character)
PSocketAddress* addr;
PSocket* sock;
// Construct address for server. Since the server is assumed to be on the same machine for the sake of this program, the address is loopback, but typically this would be an external address.
if ((addr = p_socket_address_new("127.0.0.1", SERVER_PORT)) == NULL)
return 1;
// Create socket
if ((sock = p_socket_new(P_SOCKET_FAMILY_INET, P_SOCKET_TYPE_STREAM, P_SOCKET_PROTOCOL_TCP, NULL)) == NULL)
{
// Can't construct socket, cleanup and exit.
p_socket_address_free(addr);
return 2;
}
// Connect to server.
if (!p_socket_connect(sock, addr, NULL))
{
// Couldn't connect, cleanup.
p_socket_address_free(addr);
p_socket_free(sock);
return 4;
}
// Receive our message and print.
pssize sizeOfRecvData = p_socket_receive(sock, buffer, sizeof(buffer) - 1, NULL);
buffer[sizeOfRecvData] = '\0'; // Set null character 1 after message
printf("We received %s\n", buffer);
// Cleanup
p_socket_address_free(addr);
p_socket_close(sock, NULL);
return 0;
}
int main()
{
p_libsys_init();
int inputChar;
printf("Type s to make this a server instance, or c for client instance:");
inputChar = getchar();
int runResult;
if (inputChar == 's')
runResult = do_server_things();
else
runResult = do_client_things();
p_libsys_shutdown();
return runResult;
}