Winsock 入门 Echo 示例
Posted shuoed
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winsock 入门 Echo 示例相关的知识,希望对你有一定的参考价值。
1 #include <stdio.h> 2 #include <winsock2.h> 3 4 #pragma comment(lib, "ws2_32") /* WinSock 使用的库函数 */ 5 6 #define ECHO_DEF_PORT 7 /* 侦听的缺省端口 */ 7 #define ECHO_BUF_SIZE 256 /* 缓冲区的大小 */ 8 9 int main(int argc, char **argv) 10 { 11 WSADATA wsa_data; 12 SOCKET echo_soc = 0, /* 侦听 socket 句柄 */ 13 acpt_soc = 0; 14 15 struct sockaddr_in serv_addr, /* socket的本地地址 */ 16 clnt_addr; /* socket的远端地址 */ 17 unsigned short port = ECHO_DEF_PORT; 18 int result = 0; 19 int addr_len = sizeof(struct sockaddr_in); 20 char recv_buf[ECHO_BUF_SIZE]; 21 22 if (argc == 2) 23 port = atoi(argv[1]); 24 25 WSAStartup(MAKEWORD(2,0), &wsa_data);/* 初始化 WinSock资源 */ 26 echo_soc = socket(AF_INET, SOCK_STREAM, 0); /* 创建 socket */ 27 28 /* socket 的本地地址 */ 29 serv_addr.sin_family = AF_INET; 30 serv_addr.sin_port = htons(port); 31 serv_addr.sin_addr.s_addr = INADDR_ANY; 32 33 result = bind(echo_soc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); 34 if (result == SOCKET_ERROR) 35 { 36 printf("[Echo Server] bind error: %d\n", WSAGetLastError()); 37 closesocket(echo_soc); 38 return -1; 39 } 40 41 listen(echo_soc, SOMAXCONN); 42 43 printf("[Echo Server] is running ... ...\n"); 44 while (1) 45 { 46 acpt_soc = accept(echo_soc, (struct sockaddr *)&clnt_addr, &addr_len); 47 if (acpt_soc == INVALID_SOCKET) 48 { 49 printf("[Echo Server] accept error: %d\n", WSAGetLastError()); 50 break; 51 } 52 53 result = recv(acpt_soc, recv_buf, ECHO_BUF_SIZE, 0); 54 if (result > 0) 55 { 56 recv_buf[result] = 0; 57 printf("[Echo Server] receives: \"%s\", from %s\r\n", 58 recv_buf, inet_ntoa(clnt_addr.sin_addr)); 59 60 result = send(acpt_soc, recv_buf, result, 0); 61 } 62 63 closesocket(acpt_soc); 64 } 65 66 closesocket(echo_soc); 67 WSACleanup(); 68 69 return 0; 70 }
1 #include <stdio.h> 2 #include <winsock2.h> 3 4 #pragma comment(lib, "ws2_32") /* WinSock 使用的库函数 */ 5 6 #define ECHO_DEF_PORT 7 /* 连接的缺省端口 */ 7 #define ECHO_BUF_SIZE 256 /* 缓冲区的大小 */ 8 #define ECHO_DEF_ADD "127.0.0.1"/*连接的默认服务器为本机*/ 9 10 int main(int argc, char **argv) 11 { 12 WSADATA wsa_data; 13 SOCKET echo_soc = 0; /* socket 句柄 */ 14 struct sockaddr_in serv_addr; /* 服务器地址 */ 15 unsigned short port = ECHO_DEF_PORT; 16 char *conAdd=ECHO_DEF_ADD; 17 int result = 0, send_len = 0; 18 char *test_data = "Hello server!", recv_buf[ECHO_BUF_SIZE]; 19 20 WSAStartup(MAKEWORD(2,0), &wsa_data);/* 初始化 WinSock资源 */ 21 send_len = strlen(test_data); 22 23 /* 服务器地址 */ 24 serv_addr.sin_family = AF_INET; 25 serv_addr.sin_port = htons(port); 26 serv_addr.sin_addr.s_addr = inet_addr(conAdd); 27 28 if (serv_addr.sin_addr.s_addr == INADDR_NONE) 29 { 30 printf("[ECHO] invalid address\n"); 31 return -1; 32 }; 33 34 echo_soc = socket(AF_INET, SOCK_STREAM, 0); /* 创建 socket */ 35 result = connect(echo_soc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); 36 37 if (result == 0) /* 连接成功 */ 38 { 39 result = send(echo_soc, test_data, send_len, 0); 40 result = recv(echo_soc, recv_buf, ECHO_BUF_SIZE, 0); 41 } 42 43 if (result > 0) 44 { 45 recv_buf[result] = 0; 46 printf("[Echo Client] receives: \"%s\"\r\n", recv_buf); 47 } 48 else 49 printf("[Echo Client] error : %d.\r\n", WSAGetLastError()); 50 51 getchar(); 52 closesocket(echo_soc); 53 WSACleanup(); 54 55 return 0; 56 }
以上是关于Winsock 入门 Echo 示例的主要内容,如果未能解决你的问题,请参考以下文章
在 Winsock ws 客户端和 Websocket 服务器之间建立握手