-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
56 lines (46 loc) · 1.27 KB
/
Copy pathclient.c
File metadata and controls
56 lines (46 loc) · 1.27 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
#include "socket.h"
int main(int argc, char *argv[]) {
printf("starting client...\n");
start_client();
return 0;
}
int start_client(void) {
struct sockaddr_in addr;
struct sockaddr_in server_addr;
printf("opening socket...\n");
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* error handling */
if (sockfd < 0) {
perror("[client] socket error\n");
return -1;
}
printf("done!\n");
printf("generating local client socket...\n");
/* zero out all fields in addr */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(9998);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
printf("done!\n");
printf("binding address to socket...\n");
int bind_val = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
if (bind_val < 0) {
perror("[client] bind error.\n");
return -1;
}
printf("done!\n");
printf("generating local server address...\n");
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9999);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
printf("done!\n");
/* send data */
char buffer[BUF_SIZE] = "hello there!\n";
int val = send_text(sockfd, server_addr, buffer);
if (val < 0) {
perror("[client] cant send text.\n");
return -1;
}
return sockfd;
}