15 #pragma comment(lib, "ws2_32.lib") 22 #include <arpa/inet.h> 24 #include <netinet/tcp.h> 25 #include <sys/ioctl.h> 31 WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
38 #define DB_FLG "[net work] " 39 #define PRINT_ERR printf 42 #define PERRNO(ret, db_flg, str) \ 45 PRINT_ERR("%s%s, errno=%d\n", db_flg, str, WSAGetLastError()); \ 51 #define PERRNO(ret, db_flg, str) \ 54 PRINT_ERR("%s%s, errno=%d\n", db_flg, str, errno); \ 63 int socket_init(
char *local_ip,
int port,
int is_server) {
107 WORD sockVersion = MAKEWORD(2, 2);
109 PERRNO(WSAStartup(sockVersion, &data),
DB_FLG,
"Error: ESAStartup");
110 int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
116 int keepInterval = 1;
118 struct timeval timeout = { 2, 0 };
121 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
char *)&on,
sizeof(on));
123 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
char *)&keepAlive,
127 tcp_keepalive alive_in;
128 tcp_keepalive alive_out;
129 alive_in.keepalivetime = 2000;
130 alive_in.keepaliveinterval = 5000;
132 unsigned long ulBytesReturn = 0;
133 ret = WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, &alive_in,
sizeof(alive_in),
134 &alive_out,
sizeof(alive_out), &ulBytesReturn, NULL, NULL);
135 if (ret == SOCKET_ERROR)
150 ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&timeout,
151 sizeof(
struct timeval));
155 struct sockaddr_in local_addr;
156 local_addr.sin_family = AF_INET;
157 local_addr.sin_port = htons(port);
159 inet_pton(AF_INET, local_ip, (
void *)&local_addr.sin_addr.S_un.S_addr);
160 ret = bind(sockfd, (
struct sockaddr *)&local_addr,
sizeof(local_addr));
163 int ret = listen(sockfd, 10);
170 struct sockaddr_in server_addr;
171 server_addr.sin_family = AF_INET;
172 server_addr.sin_port = htons(server_port);
174 inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
177 connect(*socket, (
struct sockaddr *)&server_addr,
sizeof(server_addr));
183 int ret = send(client_fp, (
char *)data, len, 0);
191 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
197 int keepInterval = 10;
199 struct timeval timeout = { 2, 0 };
202 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
void *)&on,
sizeof(on));
204 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (
void *)&keepAlive,
207 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (
void *)&keepIdle,
210 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (
void *)&keepInterval,
211 sizeof(keepInterval));
212 PERRNO(ret,
DB_FLG,
"Error: setsockopt[TCP_KEEPINTVL]");
213 ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (
void *)&keepCount,
216 ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&timeout,
217 sizeof(
struct timeval));
221 struct sockaddr_in local_addr;
222 local_addr.sin_family = AF_INET;
223 local_addr.sin_port = htons(port);
224 local_addr.sin_addr.s_addr = inet_addr(local_ip);
225 ret = bind(sockfd, (
struct sockaddr *)&local_addr,
sizeof(local_addr));
228 int ret = listen(sockfd, 10);
235 struct sockaddr_in server_addr;
236 server_addr.sin_family = AF_INET;
237 server_addr.sin_port = htons(server_port);
238 inet_aton(server_ip, &server_addr.sin_addr);
240 connect(*socket, (
struct sockaddr *)&server_addr,
sizeof(server_addr));
246 int ret = send(client_fp, (
void *)data, len, 0);
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)
#define PERRNO(ret, db_flg, str)