winsock select 学习代码(2)

之前文章的改进版

服务器仅仅接受客户端发送的字符串并显示

客户端可以调节发送数目 但是不能超过64

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SelectServer.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
 
#pragma comment(lib,"ws2_32")
 
#define SOCKET_MAXCNT   64
#define THREAD_NUM      2
#define CONNECT_PORT    8773   
#define CONNECT_ADDR    "127.0.0.1"
#define DATA_BUF_SZIE   8192
 
typedef struct _SOCKET_INFORMATION {
    CHAR Buffer[DATA_BUF_SZIE];
    WSABUF DataBuf;
    SOCKET Socket;
    OVERLAPPED Overlapped;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
 
DWORD   totalSockets = 0;
LPSOCKET_INFORMATION SocketArray[FD_SETSIZE];
 
 
BOOL CreateSocketInformation(SOCKET s)
{
    LPSOCKET_INFORMATION SI;
 
    printf("Accepted socket number %d\n", s);
 
    if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
        sizeof(SOCKET_INFORMATION))) == NULL)
    {
        printf("GlobalAlloc() failed with error %d\n", GetLastError());
        return FALSE;
    }
 
    // Prepare SocketInfo structure for use.
 
    SI->Socket = s;
 
    SocketArray[totalSockets] = SI;
 
    totalSockets++;
 
    return(TRUE);
}
 
 
void FreeSocketInformation(DWORD Index)
{
    LPSOCKET_INFORMATION SI = SocketArray[Index];
    DWORD i;
 
    closesocket(SI->Socket);
 
    printf("Closing socket number %d\n", SI->Socket);
 
    GlobalFree(SI);
 
    // Squash the socket array
 
    for (i = Index; i < totalSockets; i++)
    {
        SocketArray[i] = SocketArray[i + 1];
    }
 
    totalSockets--;
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    WSAData wsaData;
    SOCKADDR_IN InternetAddr = {0};
    FD_SET ReadSet;
    DWORD   i = 0;
    DWORD   total = 0;
    DWORD   Flags;
    DWORD   RecvBytes = 0;
    SOCKET  listenSock = INVALID_SOCKET;
    SOCKET  acceptSock = INVALID_SOCKET;
    if( WSAStartup(MAKEWORD(2,2),&wsaData) != 0 )
    {
        printf("WSAStartup error,exit\n");
        goto SOCKET_CLEAN;
    }
 
    listenSock = WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,
        WSA_FLAG_OVERLAPPED);
    if(listenSock == INVALID_SOCKET)
    {
        printf("WSASocket error \n");
        goto SOCKET_CLEAN;
    }
 
    InternetAddr.sin_family = AF_INET;
    InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    InternetAddr.sin_port = htons(CONNECT_PORT);
 
    if (bind(listenSock, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr))
        == SOCKET_ERROR)
    {
        printf("bind() failed with error %d\n", WSAGetLastError());
        goto SOCKET_CLEAN;
    }
 
    if (listen(listenSock, 5))
    {
        printf("listen() failed with error %d\n", WSAGetLastError());
        goto SOCKET_CLEAN;
    }
 
    // Change the socket mode on the listening socket from blocking to
    // non-block so the application will not block waiting for requests.
 
    ULONG NonBlock = 1;
    if (ioctlsocket(listenSock, FIONBIO, &NonBlock) == SOCKET_ERROR)
    {
        printf("ioctlsocket() failed with error %d\n", WSAGetLastError());
        goto SOCKET_CLEAN;
    }
 
    while(1)
    {
        FD_ZERO(&ReadSet); 
        FD_SET(listenSock, &ReadSet);
 
        for (i = 0; i < totalSockets; i++)
        {
            FD_SET(SocketArray[i]->Socket, &ReadSet);
        }
 
        total = select(0,&ReadSet,NULL,NULL,NULL);
        if(total == SOCKET_ERROR)
        {
            printf("select() failed with error %d\n", WSAGetLastError());
            goto SOCKET_CLEAN;
        }
 
        if(FD_ISSET(listenSock,&ReadSet))
        {
            total--;
            acceptSock = accept(listenSock,NULL,NULL);
            if(acceptSock == INVALID_SOCKET)
            {
                if (WSAGetLastError() != WSAEWOULDBLOCK)
                {
                    printf("accept() failed with error %d\n", WSAGetLastError());
                    goto SOCKET_CLEAN;
                }
            }else
            {
                NonBlock = 1;
                int ret = ioctlsocket(acceptSock,FIONBIO, &NonBlock);
                if(ret == SOCKET_ERROR)
                {
                    printf("ioctlsocket() failed with error %d\n", WSAGetLastError());
                    goto SOCKET_CLEAN;
                }
 
                if (CreateSocketInformation(acceptSock) == FALSE)
                    goto SOCKET_CLEAN;
            }
        }
 
 
        for (i = 0; total > 0 && i < totalSockets; i++)
        {
            LPSOCKET_INFORMATION SocketInfo = SocketArray[i];
 
            SocketInfo->DataBuf.buf = SocketInfo->Buffer;
            SocketInfo->DataBuf.len = DATA_BUF_SZIE;
 
            if(FD_ISSET(SocketInfo->Socket,&ReadSet))
            {
                total--;
                Flags = 0;
                ZeroMemory(SocketInfo->Buffer,DATA_BUF_SZIE);
                if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
                    &Flags, NULL, NULL) == SOCKET_ERROR)
                {
                    if (WSAGetLastError() != WSAEWOULDBLOCK)
                    {
                        printf("WSARecv() failed with error %d\n", WSAGetLastError());
                        FreeSocketInformation(i);
                    }
                    continue;
                }else
                {
                    printf(SocketInfo->Buffer);
                    if(RecvBytes == 0)
                    {
                        FreeSocketInformation(i);
                        continue;
                    }
                }
            }
        }
    }
 
 
SOCKET_CLEAN:
    if(listenSock != INVALID_SOCKET)
    {
        closesocket(listenSock);
        listenSock = INVALID_SOCKET;
    }
    if(acceptSock != INVALID_SOCKET)
    {
        closesocket(acceptSock);
        acceptSock = INVALID_SOCKET;
    }
    WSACleanup();
 
    return 0;
}

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// CommonClient.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
 
#pragma comment(lib,"ws2_32")
 
#define SOCKET_MAXCNT   64
#define THREAD_NUM      22
#define CONNECT_PORT    8773   
#define CONNECT_ADDR    "127.0.0.1"
 
DWORD WINAPI SocketFunc(LPVOID pM)
{
    char buf[512] = {0}; 
    SOCKET  sock;
    int result;
    struct sockaddr_in cli_addr;
    int num = *(int*)pM;
 
    sock = socket(AF_INET,SOCK_STREAM,0);
 
    cli_addr.sin_family = AF_INET;
    cli_addr.sin_port = htons(CONNECT_PORT);
    cli_addr.sin_addr.s_addr = inet_addr(CONNECT_ADDR);
 
 
    result = connect(sock,(struct sockaddr*)&cli_addr,sizeof(cli_addr));
    if(result == SOCKET_ERROR)
    {
        printf("%d thread: connect error\n",num);
        exit(1);
    }
 
    while(1)
    {
 
        sprintf(buf,"%d thread: %s",num,"hello world!\n");
        printf(buf);
        result = send(sock,buf,100,0);
        if(result == SOCKET_ERROR)
        {
            printf("%d error \n",num);
            return 0;
        }
        Sleep(3000);
    }
 
    exit(1);
    return 0;
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    int num[SOCKET_MAXCNT] = {0};
    WSADATA wsa_data;
 
    WSAStartup(WINSOCK_VERSION,&wsa_data);
 
    // 初始化数组 传递给线程  区别各个线程号
    for(int i = 0 ;i < SOCKET_MAXCNT;i++)
    {
        num[i] = i;
    }
 
    for(int i = 0; i < THREAD_NUM;i++)
    {
        HANDLE hThread = CreateThread(NULL,0,SocketFunc,&num[i],0,NULL);
        if(hThread != NULL)
        {
            CloseHandle(hThread);
        }
    }
 
    while(1)
    {
        Sleep(100000);
    }
 
 
    return 0;
}

 

winsock select 学习代码(2),布布扣,bubuko.com

winsock select 学习代码(2)

上一篇:《深入理解Windows Phone 8.1 UI控件编程》


下一篇:WIN XP蓝屏代码大全