关闭

windows socket 编程

729人阅读 评论(0) 收藏 举报

Windows下Socket编程主要包括以下几部分:
服务端
   1、初始化Windows Socket库。
   2、创建Socket。
   3、绑定Socket。
   4、监听。
   5、Accept。
   6、接收、发送数据。

客户端
   1、初始化Windows Socket库。
   2、创建Socket。
   3、连接Socket。
   4、接收、发送数据。

服务端每接收到一个客户端的Socket,则创建一个线程。满足一个服务端连接多个客户端。

  1 //Server.cpp
  2 #include <iostream>
  3 #include <Windows.h>
  4
  5 using namespace std;
  6
  7 #define  PORT 4000
  8 #define  IP_ADDRESS "192.168.1.145"
  9
10 DWORD WINAPI ClientThread(LPVOID lpParameter)
11 {
12     SOCKET CientSocket = (SOCKET)lpParameter;
13     int Ret = 0;
14     char RecvBuffer[MAX_PATH];
15
16     while ( true )
17     {
18         memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
19         Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
20         if ( Ret == 0 || Ret == SOCKET_ERROR )
21         {
22             cout<<"客户端退出!"<<endl;
23             break;
24         }
25         cout<<"接收到客户信息为:"<<RecvBuffer<<endl;
26     }
27
28     return 0;
29 }
30
31 int main(int argc, char* argv[])
32 {
33     WSADATA  Ws;
34     SOCKET ServerSocket, CientSocket;
35     struct sockaddr_in LocalAddr, ClientAddr;
36     int Ret = 0;
37     int AddrLen = 0;
38     HANDLE hThread = NULL;
39
40     //Init Windows Socket
41     if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
42     {
43         cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
44         return -1;
45     }
46    
47     //Create Socket
48     ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
49     if ( ServerSocket == INVALID_SOCKET )
50     {
51         cout<<"Create Socket Failed::"<<GetLastError()<<endl;
52         return -1;
53     }
54
55     LocalAddr.sin_family = AF_INET;
56     LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
57     LocalAddr.sin_port = htons(PORT);
58     memset(LocalAddr.sin_zero, 0x00, ;
59
60     //Bind Socket
61     Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
62     if ( Ret != 0 )
63     {
64         cout<<"Bind Socket Failed::"<<GetLastError()<<endl;
65         return -1;
66     }
67
68     Ret = listen(ServerSocket, 10);
69     if ( Ret != 0 )
70     {
71         cout<<"listen Socket Failed::"<<GetLastError()<<endl;
72         return -1;
73     }
74
75     cout<<"服务端已经启动"<<endl;
76
77     while ( true )
78     {
79         AddrLen = sizeof(ClientAddr);
80         CientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
81         if ( CientSocket == INVALID_SOCKET )
82         {
83             cout<<"Accept Failed::"<<GetLastError()<<endl;
84             break;
85         }
86
87         cout<<"客户端连接::"<<inet_ntoa(ClientAddr.sin_addr)<<":"<<ClientAddr.sin_port<<endl;
88        
89         hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)CientSocket, 0, NULL);
90         if ( hThread == NULL )
91         {
92             cout<<"Create Thread Failed!"<<endl;
93             break;
94         }
95
96         CloseHandle(hThread);
97     }
98
99     closesocket(ServerSocket);
100     closesocket(CientSocket);
101     WSACleanup();
102
103     return 0;
104 }

1 //Client.cpp
2 #include <iostream>
3 #include <Windows.h>
4
5 using namespace std;
6
7 #define  PORT 4000
8 #define  IP_ADDRESS "192.168.1.145"
9
10
11 int main(int argc, char* argv[])
12 {
13     WSADATA  Ws;
14     SOCKET CientSocket;
15     struct sockaddr_in ServerAddr;
16     int Ret = 0;
17     int AddrLen = 0;
18     HANDLE hThread = NULL;
19     char SendBuffer[MAX_PATH];
20
21     //Init Windows Socket
22     if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
23     {
24         cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
25         return -1;
26     }
27
28     //Create Socket
29     CientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
30     if ( CientSocket == INVALID_SOCKET )
31     {
32         cout<<"Create Socket Failed::"<<GetLastError()<<endl;
33         return -1;
34     }
35
36     ServerAddr.sin_family = AF_INET;
37     ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
38     ServerAddr.sin_port = htons(PORT);
39     memset(ServerAddr.sin_zero, 0x00, ;
40
41     Ret = connect(CientSocket,(struct sockaddr*)&ServerAddr, sizeof(ServerAddr));
42     if ( Ret == SOCKET_ERROR )
43     {
44         cout<<"Connect Error::"<<GetLastError()<<endl;
45         return -1;
46     }
47     else
48     {
49         cout<<"连接成功!"<<endl;
50     }
51
52     while ( true )
53     {
54         cin.getline(SendBuffer, sizeof(SendBuffer));
55         Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
56         if ( Ret == SOCKET_ERROR )
57         {
58             cout<<"Send Info Error::"<<GetLastError()<<endl;
59             break;
60         }
61     }
62    
63     closesocket(CientSocket);
64     WSACleanup();
65
66     return 0;
67 }
http://www.cppblog.com/bujiwu/archive/2009/01/11/71707.aspx

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:520977次
    • 积分:5754
    • 等级:
    • 排名:第4666名
    • 原创:43篇
    • 转载:205篇
    • 译文:0篇
    • 评论:161条
    最新评论