15 #pragma comment(lib, "ws2_32.lib") 19 #define errno WSAGetLastError() 23 #include <arpa/inet.h> 25 #include <netinet/tcp.h> 26 #include <sys/ioctl.h> 32 WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
39 #define DB_FLG "[net work] " 40 #define PRINT_ERR printf 42 #define PERRNO(ret, db_flg, str) \ 45 PRINT_ERR("%s%s, errno=%d\n", db_flg, str, errno); \ 53 int socket_init(
char *local_ip,
int port,
int is_server) {
97 WORD sockVersion = MAKEWORD(2, 2);
99 PERRNO(WSAStartup(sockVersion, &data),
DB_FLG,
"ESAStartup");
100 int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
106 int keepInterval = 1;
108 struct timeval timeout = { 2, 0 };
111 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
char *)&on,
sizeof(on));
113 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
char *)&keepAlive,
117 tcp_keepalive alive_in;
118 tcp_keepalive alive_out;
119 alive_in.keepalivetime = 1000;
120 alive_in.keepaliveinterval = 1000;
122 unsigned long ulBytesReturn = 0;
123 ret = WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, &alive_in,
sizeof(alive_in),
124 &alive_out,
sizeof(alive_out), &ulBytesReturn, NULL, NULL);
125 if (ret == SOCKET_ERROR)
141 ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&timeout,
142 sizeof(
struct timeval));
146 struct sockaddr_in local_addr;
147 local_addr.sin_family = AF_INET;
148 local_addr.sin_port = htons(port);
150 inet_pton(AF_INET, local_ip, (
void *)&local_addr.sin_addr.S_un.S_addr);
151 ret = bind(sockfd, (
struct sockaddr *)&local_addr,
sizeof(local_addr));
154 int ret = listen(sockfd, 10);
161 struct sockaddr_in server_addr;
162 server_addr.sin_family = AF_INET;
163 server_addr.sin_port = htons(server_port);
165 inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
168 connect(*socket, (
struct sockaddr *)&server_addr,
sizeof(server_addr));
174 int ret = send(client_fp, (
char *)data, len, 0);
183 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
189 int keepInterval = 10;
191 struct timeval timeout = { 2, 0 };
194 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
void *)&on,
sizeof(on));
196 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
void *)&keepAlive,
199 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (
void *)&keepIdle,
202 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (
void *)&keepInterval,
203 sizeof(keepInterval));
205 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (
void *)&keepCount,
208 ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&timeout,
209 sizeof(
struct timeval));
213 struct sockaddr_in local_addr;
214 local_addr.sin_family = AF_INET;
215 local_addr.sin_port = htons(port);
216 local_addr.sin_addr.s_addr = inet_addr(local_ip);
217 ret = bind(sockfd, (
struct sockaddr *)&local_addr,
sizeof(local_addr));
220 int ret = listen(sockfd, 10);
227 struct sockaddr_in server_addr;
228 server_addr.sin_family = AF_INET;
229 server_addr.sin_port = htons(server_port);
230 inet_aton(server_ip, &server_addr.sin_addr);
232 connect(*socket, (
struct sockaddr *)&server_addr,
sizeof(server_addr));
238 int ret = send(client_fp, (
void *)data, len, 0);
#define PERRNO(ret, db_flg, str)
int socket_connect_server(int *socket, char server_ip[], int server_port)
int socket_send_data(int client_fp, unsigned char *data, int len)
int socket_init(char *local_ip, int port, int is_server)