-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
175 lines (140 loc) · 4.48 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <dirent.h>
#include <stdbool.h>
#define SERVER_IP "127.0.0.1"
#define PORT_NO 5120
static int sock;
static struct sockaddr_in addr;
void CreateSocket(bool server)
{
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
printf("Socket couldn't be created.\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT_NO);
if (server) {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
printf("Server socket couldn't be created.\n");
listen(sock, 3);
printf("Created server socket.\n");
} else {
char server_ip[] = SERVER_IP;
struct hostent *host;
if ((addr.sin_addr.s_addr = inet_addr(server_ip)) == INADDR_NONE) {
if ((host = gethostbyname(server_ip)) == NULL)
printf("Socket couldn't get host by name.\n");
memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
}
printf("Created client socket.\n");
}
}
void *Server(void *param)
{
int selected_core = 3;
cpu_set_t cpu;
CPU_ZERO(&cpu);
CPU_SET(selected_core, &cpu);
if (sched_setaffinity(syscall(SYS_gettid), sizeof(cpu), &cpu) == -1)
printf("Core didn't select.\n");
int cli_length = 0, res;
struct sockaddr_in client = {0};
const char *message = "Hello Client!";
while (true) {
printf("Waiting for client connection.\n");
cli_length = sizeof(struct sockaddr_in);
res = accept(sock, (struct sockaddr *)&client, (socklen_t*)&cli_length);
if (res < 0)
printf("Server accept failed.\n");
else
printf("Client connection accepted.\n");
if (send(res, message, strlen(message), 0) < 0)
printf("Server send failed.\n");
usleep(1000);
}
shutdown(sock, SHUT_RDWR);
close(sock);
return param;
}
void *Client(void *param)
{
int selected_core = 3;
cpu_set_t cpu;
CPU_ZERO(&cpu);
CPU_SET(selected_core, &cpu);
if (sched_setaffinity(syscall(SYS_gettid), sizeof(cpu), &cpu) == -1)
printf("Core didn't select.\n");
bool conn_state = false;
char data[255];
int recv_count = 0;
int recv_size = 12;
while (true) {
if (!conn_state) {
struct timeval time;
time.tv_sec = 1;
time.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&time, sizeof(time));
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
printf("Disconnected.\n");
} else {
conn_state = true;
printf("Client connected.\n");
}
} else {
if ((recv_count = recv(sock, &data, recv_size, 0)) == -1) {
if (errno == ECONNRESET)
printf("Client Socket Error : ECONNRESET");
else if (errno == EBADF)
printf("Client Socket Error : EBADF");
shutdown(sock, SHUT_RDWR);
close(sock);
conn_state = false;
}
if (recv_count > 0) {
for (uint i = 0; i < recv_size; i++)
printf("%c", data[i]);
printf("\n");
}
}
usleep(1000);
}
shutdown(sock, SHUT_RDWR);
close(sock);
return param;
}
int main(int argc, char *argv[])
{
printf("TCP Socket Programming\n");
if (argc >= 2) {
if (atoi(argv[1]) == 0) {
printf("Server\n");
CreateSocket(true);
pthread_t serv_thread;
if (pthread_create(&serv_thread, NULL, *Server, NULL) == -1)
printf("Server thread couldn't be created.\n");
pthread_join(serv_thread, NULL);
} else if (atoi(argv[1]) == 1) {
printf("Client\n");
CreateSocket(false);
pthread_t cli_thread;
if (pthread_create(&cli_thread, NULL, *Client, NULL) == -1)
printf("Client thread couldn't be created.\n");
pthread_join(cli_thread, NULL);
} else {
printf("Invalid parameter.\n");
}
}
exit(0);
}