forked from 3p1c/MUEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
77 lines (70 loc) · 1.46 KB
/
client.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#ifdef __WIN32
#include <windows.h>
#include <winsock2.h>
#else
#include "pthread.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#endif
bool setup_client(void);
bool connect_client(char* ip, long port);
#ifdef __WIN32
SOCKET client_socket;
#else
int client_socket;
#endif
int main(int argc, char** argv)
{
if (setup_client()) {
if (connect_client("127.0.0.1", 1001)) {
printf("Connected!\n");
}
}
return 0;
}
bool setup_client(void)
{
#ifdef _WIN32
WSADATA wdata;
if (WSAStartup(MAKEWORD(2, 2), &wdata)) {
return false;
}
#endif
return true;
}
bool connect_client(char* ip, long port)
{
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(struct sockaddr_in));
#if _WIN32
if ((client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET) {
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
#else
if ((client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
inet_aton(ip, &(serv_addr.sin_addr));
serv_addr.sin_family = PF_INET;
serv_addr.sin_port = port;
#endif
if (!connect(client_socket, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in))) {
return true;
}
}
return false;
}
void cleanup_client(void)
{
#ifdef _WIN32
closesocket(client_socket);
WSACleanup();
#else
close(client_socket);
#endif
return;
}