-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.c
More file actions
91 lines (74 loc) · 2.45 KB
/
Copy pathrun.c
File metadata and controls
91 lines (74 loc) · 2.45 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
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define DOMAIN AF_INET
#define SERVER_IP "149.125.45.77"
#define PORT 8080
int main(int argc, char* argv[]) {
WSADATA wsaData = {0};
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
wprintf(L"WSAStartup failed: %d\n", iResult);
return 1;
}
printf("select socket type:\n0: server\n1+: client\n");
int clientID;
wscanf(L"%d", &clientID);
if (clientID == 0) {
printf("input number of clients: ");
unsigned int numClients;
scanf("%u", &numClients);
SOCKET server = socket(DOMAIN, SOCK_STREAM, IPPROTO_TCP);
if (server == INVALID_SOCKET) {
wprintf(L"socket couldn't be opened (error: %d)\n", WSAGetLastError());
WSACleanup();
return 1;
}
SOCKADDR_IN address;
address.sin_family = DOMAIN;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
int addrlen;
bind(server, (PSOCKADDR) &address, sizeof(address));
listen(server, 5);
SOCKET clients[numClients];
for (int i = 0; i < numClients; i++) {
clients[i] = accept(server, (PSOCKADDR) &address, &addrlen);
}
char buffer[1024] = {0};
do {
for (int i = 0; i < numClients; i++)
{
recv(clients[i], buffer, 1, 0);
printf("%d", i);
}
if (strncmp(buffer, "", 1)) printf("%s\n", buffer);
} while (strcmp(buffer, "q") != 0);
return 0;
closesocket(server);
for (int i = 0; i < numClients; i++) {
closesocket(clients[i]);
}
}
else {
SOCKET client = socket(DOMAIN, SOCK_STREAM, IPPROTO_TCP);
if (client == INVALID_SOCKET) {
wprintf(L"socket couldn't be opened (error: %d)\n", WSAGetLastError());
WSACleanup();
return 1;
}
SOCKADDR_IN address = {.sin_family = DOMAIN, .sin_port = htons(PORT)};
inet_pton(DOMAIN, SERVER_IP, &address.sin_addr);
connect(client, (PSOCKADDR) &address, sizeof(address));
char message[20] = {0};
do {
scanf("%s", message);
send(client, message, sizeof(message), 0);
} while (strcmp(message, "q") != 0);
closesocket(client);
}
WSACleanup();
return 0;
}