Mercurial
annotate seobeo/s_linux_network.c @ 4:0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 26 Sep 2025 15:14:46 -0700 |
| parents | 2758f5527d2b |
| children | 1e61008b9980 |
| rev | line source |
|---|---|
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "seobeo/seobeo.h" |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
3 int Seobeo_CreateSocket(int32 stream, const char *host, const char* port, int32 backlog) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
4 { |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
5 struct addrinfo hints = {0}, *server_infos, *free_server_info; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
6 int32 sock_fd, yes = 1; // Need this for setsockopt |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
7 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
8 hints.ai_family = AF_INET; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
9 hints.ai_socktype = stream ? SOCK_STREAM : SOCK_DGRAM; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
10 hints.ai_protocol = stream ? IPPROTO_TCP : IPPROTO_UDP; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
11 hints.ai_flags = stream ? AI_PASSIVE : 0; |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
12 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
13 if (getaddrinfo(host, port, &hints, &server_infos) != 0) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
14 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
15 perror("getaddrinfo"); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
16 return -1; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
17 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
18 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
19 for |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
20 ( |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
21 free_server_info = server_infos; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
22 free_server_info != NULL; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
23 free_server_info = free_server_info->ai_next |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
24 ) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
25 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
26 if |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
27 ( |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
28 (sock_fd = socket( |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
29 free_server_info->ai_family, free_server_info->ai_socktype, |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
30 free_server_info->ai_protocol |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
31 )) == -1 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
32 ) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
33 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
34 perror("socket"); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
35 continue; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
36 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
37 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
38 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
39 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
40 perror("setsockopt"); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
41 continue; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
42 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
43 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
44 if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
45 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
46 close(sock_fd); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
47 perror("setsockopt"); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
48 continue; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
49 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
50 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
51 // UDP should be non blocking |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
52 if(!stream) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
53 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
54 if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0) |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
55 { |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
56 close(sock_fd); |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
57 perror("v_network: Couldn't make socket non-blocking\n"); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
58 return -1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
59 } |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
60 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
61 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
62 // binded to a open server infos; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
63 break; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
64 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
65 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
66 // No longer need these values |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
67 freeaddrinfo(server_infos); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
68 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
69 if (free_server_info == NULL) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
70 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
71 perror("No free server"); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
72 return -1; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
73 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
74 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
75 // TCP listen |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
76 if(stream) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
77 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
78 if (listen(sock_fd, backlog) != 0) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
79 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
80 perror("listen"); |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
81 close(sock_fd); |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
82 return -1; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
83 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
84 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
85 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
86 return sock_fd; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
87 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
88 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
89 Seobeo_PHandle Seobeo_Stream_Handle_Create(const char *host, const char* port) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
90 { |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
91 Seobeo_PHandle p_handle; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
92 p_handle = malloc(sizeof(*p_handle)); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
93 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
94 p_handle->socket = Seobeo_CreateSocket(1, host, port, 10); // socke fd |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
95 p_handle->host = host != NULL ? strdup(host) : "localhost"; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
96 p_handle->port = strdup(port); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
97 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
98 p_handle->read_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
99 p_handle->read_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
100 p_handle->read_buffer_len = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
101 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
102 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
103 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
104 p_handle->write_buffer_len = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
105 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
106 p_handle->file = NULL; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
107 p_handle->text_copy = NULL; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
108 p_handle->file_name = NULL; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
109 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
110 return p_handle; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
111 } |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
112 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
113 Seobeo_PHandle Seobeo_Stream_Handle_Accept(Seobeo_PHandle server_h) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
114 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
115 struct sockaddr_storage addr; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
116 socklen_t addrlen = sizeof addr; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
117 char client_inet_addr[INET6_ADDRSTRLEN]; |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
118 int client_fd = accept(server_h->socket, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
119 (struct sockaddr*)&addr, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
120 &addrlen); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
121 inet_ntop( |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
122 addr.ss_family, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
123 Seobeo_GetIP4OrIP6((struct sockaddr *)&addr), |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
124 client_inet_addr, sizeof client_inet_addr); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
125 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
126 if (client_fd == -1) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
127 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
128 return NULL; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
129 } |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
130 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
131 Seobeo_PHandle h = malloc(sizeof *h); |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
132 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
133 h->socket = client_fd; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
134 h->host = strdup(client_inet_addr); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
135 h->port = NULL; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
136 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
137 h->read_buffer_capacity = server_h->read_buffer_capacity; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
138 h->read_buffer_len = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
139 h->read_buffer = malloc(h->read_buffer_capacity); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
140 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
141 h->write_buffer_capacity = server_h->write_buffer_capacity; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
142 h->write_buffer_len = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
143 h->write_buffer = malloc(h->write_buffer_capacity); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
144 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
145 return h; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
146 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
147 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
148 void Seobeo_Handle_Destroy(Seobeo_PHandle h) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
149 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
150 close(h->socket); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
151 free(h->host); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
152 free(h->port); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
153 free(h->read_buffer); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
154 free(h->write_buffer); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
155 free(h->file); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
156 free(h->text_copy); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
157 free(h->file_name); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
158 free(h); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
159 } |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
160 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
161 int Seobeo_Handle_Flush(Seobeo_PHandle h) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
162 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
163 uint32 total = h->write_buffer_len; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
164 uint32 sent = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
165 |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
166 while (sent < total) { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
167 ssize_t n = write( |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
168 h->socket, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
169 h->write_buffer + sent, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
170 total - sent |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
171 ); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
172 if (n < 0) { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
173 if (errno == EINTR) continue; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
174 if (errno == EAGAIN) return 1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
175 return -1; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
176 } |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
177 sent += (uint32)n; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
178 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
179 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
180 h->write_buffer_len = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
181 return 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
182 } |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
183 |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
184 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
185 int Seobeo_Handle_QueueData(Seobeo_PHandle h, const uint8_t *data, uint32_t data_size) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
186 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
187 if (h->write_buffer_len + data_size > h->write_buffer_capacity) { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
188 int rc = Seobeo_Handle_Flush(h); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
189 if (rc < 0) return -1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
190 if (rc > 0) return 1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
191 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
192 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
193 if (data_size > h->write_buffer_capacity) |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
194 { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
195 uint32_t offset = 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
196 while (offset < data_size) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
197 { |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
198 ssize_t n = write(h->socket, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
199 data + offset, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
200 data_size - offset); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
201 if (n < 0) { |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
202 if (errno == EINTR) continue; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
203 if (errno == EAGAIN) return 1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
204 return -1; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
205 } |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
206 offset += (uint32_t)n; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
207 } |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
208 return 0; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
209 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
210 |
|
4
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
211 memcpy(h->write_buffer + h->write_buffer_len, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
212 data, |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
213 data_size); |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
214 h->write_buffer_len += data_size; |
|
0b3b4f5887bb
[Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
215 return 0; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
216 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
217 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
218 void *Seobeo_GetIP4OrIP6(struct sockaddr *sa) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
219 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
220 if (sa->sa_family == AF_INET) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
221 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
222 return &(((struct sockaddr_in*)sa)->sin_addr); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
223 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
224 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
225 return &(((struct sockaddr_in6*)sa)->sin6_addr); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
226 } |