// mt4.cpp : 定义控制台应用程序的入口点。
//
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#include <mstcpip.h>
// 测试tcp
// Link to ws2_32.lib
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#define PORT 5150
#define DATA_BUFSIZE 8192
typedef struct _SOCKET_INFORMATION
{
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
WSABUF SendBuf;
SOCKET Socket;
OVERLAPPED Overlapped;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
// Prototypes
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Index);
// Global var
SOCKET AcceptSocket;
DWORD TotalSockets = 0;
LPSOCKET_INFORMATION SocketArray[FD_SETSIZE];
SOCKET ListenSocket;
//1对多肯定带来很多问题
int initserver()
{
SOCKADDR_IN InternetAddr;
WSADATA wsaData;
INT Ret;
DWORD i;
DWORD Total;
ULONG NonBlock;
DWORD Flags;
if ((Ret = WSAStartup(0x0202,&wsaData)) != 0)
{
printf("WSAStartup() failed with error %d\n", Ret);
WSACleanup();
return 1;
}
else
printf("WSAStartup() is fine!\n");
// Prepare a socket to listen for connections
if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
printf("WSASocket() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("WSASocket() is OK!\n");
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(ListenSocket, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("bind() is OK!\n");
if (listen(ListenSocket, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("listen() is OK!\n");
// Change the socket mode on the listening socket from blocking to
// non-block so the application will not block waiting for requests
NonBlock = 1;
if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("ioctlsocket() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("ioctlsocket() is OK!\n");
}
//控制发送接口
int server_write( char *buffer, int len)
{
LPSOCKET_INFORMATION SocketInfo = NULL;
for (int i = 0; i <TotalSockets; i++ )
{
SocketInfo = SocketArray[i];
if (SocketInfo && SocketInfo->Socket == AcceptSocket)
{
break;
}else
{
SocketInfo = NULL;
}
}
SocketInfo->BytesSEND = len;
SocketInfo->SendBuf.buf = buffer;
SocketInfo->SendBuf.len = len;
return 0;
}
int processor()
{
DWORD SendBytes;
DWORD RecvBytes;
FD_SET WriteSet;
FD_SET ReadSet;
DWORD Flags;
DWORD Total;
ULONG NonBlock;
// Prepare the Read and Write socket sets for network I/O notification
FD_ZERO(&ReadSet);
FD_ZERO(&WriteSet);
INT Ret;
// Always look for connection attempts
FD_SET(ListenSocket, &ReadSet);
// Set Read and Write notification for each socket based on the
// current state the buffer. If there is data remaining in the
// buffer then set the Write set otherwise the Read set
for (int i = 0; i < TotalSockets; i++)
{
FD_SET(SocketArray[i]->Socket, &ReadSet);
FD_SET(SocketArray[i]->Socket, &WriteSet);
}
if ((Total = select(0, &ReadSet, &WriteSet, NULL, NULL)) == SOCKET_ERROR)
{
printf("select() returned with error %d\n", WSAGetLastError());
return 1;
}
else
printf("select() is OK!\n");
if (FD_ISSET(ListenSocket, &ReadSet))
{
Total--;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET)
{
NonBlock = 1;
if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("ioctlsocket(FIONBIO) failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("ioctlsocket(FIONBIO) is OK!\n");
if (CreateSocketInformation(AcceptSocket) == FALSE)
{
printf("CreateSocketInformation(AcceptSocket) failed!\n");
return 1;
}
else
printf("CreateSocketInformation() is OK!\n");
}
else
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("accept() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("accept() is fine!\n");
}
}
for (int i = 0; Total > 0 && i < TotalSockets; i++)
{
LPSOCKET_INFORMATION SocketInfo = SocketArray[i];
if (FD_ISSET(SocketInfo->Socket, &ReadSet))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = DATA_BUFSIZE;
Flags = 0;
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);
}
else
printf("WSARecv() is OK!\n");
continue;
}
else
{
SocketInfo->BytesRECV = RecvBytes;
//接收到数据
// If zero bytes are received, this indicates the peer closed the connection.
if (RecvBytes == 0)
{
FreeSocketInformation(i);
continue;
}
}
}
// If the WriteSet is marked on this socket then this means the internal
// data buffers are available for more data
if (FD_ISSET(SocketInfo->Socket, &WriteSet))
{
if (SocketInfo->BytesSEND == 0)
{
continue;
}
if (WSASend(SocketInfo->Socket, &(SocketInfo->SendBuf), 1, &SendBytes, 0, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(i);
}
else
{
SocketInfo->BytesSEND -= SendBytes;
printf("WSASend() is OK!\n");
}
}
}
}
}
int main(int argc, char **argv)
{
initserver();
while(TRUE)
{
processor();
server_write("fuck",strlen("fuck"));
}
}
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;
}
else
printf("GlobalAlloc() for SOCKET_INFORMATION is OK!\n");
// Prepare SocketInfo structure for use
SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;
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--;
}
//
简单select模型设计
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#include <mstcpip.h>
// 测试tcp
// Link to ws2_32.lib
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#define PORT 5150
#define DATA_BUFSIZE 8192
typedef struct _SOCKET_INFORMATION
{
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
WSABUF SendBuf;
SOCKET Socket;
OVERLAPPED Overlapped;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
// Prototypes
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Index);
// Global var
SOCKET AcceptSocket;
DWORD TotalSockets = 0;
LPSOCKET_INFORMATION SocketArray[FD_SETSIZE];
SOCKET ListenSocket;
//1对多肯定带来很多问题
int initserver()
{
SOCKADDR_IN InternetAddr;
WSADATA wsaData;
INT Ret;
DWORD i;
DWORD Total;
ULONG NonBlock;
DWORD Flags;
if ((Ret = WSAStartup(0x0202,&wsaData)) != 0)
{
printf("WSAStartup() failed with error %d\n", Ret);
WSACleanup();
return 1;
}
else
printf("WSAStartup() is fine!\n");
// Prepare a socket to listen for connections
if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
printf("WSASocket() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("WSASocket() is OK!\n");
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(ListenSocket, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("bind() is OK!\n");
if (listen(ListenSocket, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("listen() is OK!\n");
// Change the socket mode on the listening socket from blocking to
// non-block so the application will not block waiting for requests
NonBlock = 1;
if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("ioctlsocket() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("ioctlsocket() is OK!\n");
}
//控制发送接口
int server_write( char *buffer, int len)
{
LPSOCKET_INFORMATION SocketInfo = NULL;
for (int i = 0; i <TotalSockets; i++ )
{
SocketInfo = SocketArray[i];
if (SocketInfo && SocketInfo->Socket == AcceptSocket)
{
break;
}else
{
SocketInfo = NULL;
}
}
SocketInfo->BytesSEND = len;
SocketInfo->SendBuf.buf = buffer;
SocketInfo->SendBuf.len = len;
return 0;
}
int processor()
{
DWORD SendBytes;
DWORD RecvBytes;
FD_SET WriteSet;
FD_SET ReadSet;
DWORD Flags;
DWORD Total;
ULONG NonBlock;
// Prepare the Read and Write socket sets for network I/O notification
FD_ZERO(&ReadSet);
FD_ZERO(&WriteSet);
INT Ret;
// Always look for connection attempts
FD_SET(ListenSocket, &ReadSet);
// Set Read and Write notification for each socket based on the
// current state the buffer. If there is data remaining in the
// buffer then set the Write set otherwise the Read set
for (int i = 0; i < TotalSockets; i++)
{
FD_SET(SocketArray[i]->Socket, &ReadSet);
FD_SET(SocketArray[i]->Socket, &WriteSet);
}
if ((Total = select(0, &ReadSet, &WriteSet, NULL, NULL)) == SOCKET_ERROR)
{
printf("select() returned with error %d\n", WSAGetLastError());
return 1;
}
else
printf("select() is OK!\n");
if (FD_ISSET(ListenSocket, &ReadSet))
{
Total--;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET)
{
NonBlock = 1;
if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("ioctlsocket(FIONBIO) failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("ioctlsocket(FIONBIO) is OK!\n");
if (CreateSocketInformation(AcceptSocket) == FALSE)
{
printf("CreateSocketInformation(AcceptSocket) failed!\n");
return 1;
}
else
printf("CreateSocketInformation() is OK!\n");
}
else
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("accept() failed with error %d\n", WSAGetLastError());
return 1;
}
else
printf("accept() is fine!\n");
}
}
for (int i = 0; Total > 0 && i < TotalSockets; i++)
{
LPSOCKET_INFORMATION SocketInfo = SocketArray[i];
if (FD_ISSET(SocketInfo->Socket, &ReadSet))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = DATA_BUFSIZE;
Flags = 0;
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);
}
else
printf("WSARecv() is OK!\n");
continue;
}
else
{
SocketInfo->BytesRECV = RecvBytes;
//接收到数据
// If zero bytes are received, this indicates the peer closed the connection.
if (RecvBytes == 0)
{
FreeSocketInformation(i);
continue;
}
}
}
// If the WriteSet is marked on this socket then this means the internal
// data buffers are available for more data
if (FD_ISSET(SocketInfo->Socket, &WriteSet))
{
if (SocketInfo->BytesSEND == 0)
{
continue;
}
if (WSASend(SocketInfo->Socket, &(SocketInfo->SendBuf), 1, &SendBytes, 0, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(i);
}
else
{
SocketInfo->BytesSEND -= SendBytes;
printf("WSASend() is OK!\n");
}
}
}
}
}
int main(int argc, char **argv)
{
initserver();
while(TRUE)
{
processor();
server_write("fuck",strlen("fuck"));
}
}
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;
}
else
printf("GlobalAlloc() for SOCKET_INFORMATION is OK!\n");
// Prepare SocketInfo structure for use
SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;
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--;
}