码迷,mamicode.com
首页 > Windows程序 > 详细

Winsock 入门 Echo 示例

时间:2016-11-18 00:00:20      阅读:482      评论:0      收藏:0      [点我收藏+]

标签:print   last   color   缺省   list   使用   signed   nbsp   char   

 

 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 示例

标签:print   last   color   缺省   list   使用   signed   nbsp   char   

原文地址:http://www.cnblogs.com/A--Q/p/6075820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!