服务器端
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
using namespace std;
#define MAXDATASIZE 200
int main()
{
WSABUF DataBuf;
char buffer[MAXDATASIZE];
DWORD EventTotal = 0, RecvByte = 0, Flags = 0, ByteTransferred = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
WSAOVERLAPPED AcceptOverlapped;
WSADATA wsadata;
WSAStartup(0x0202, &wsadata);
SOCKET ListenSocket, AcceptSocket;
ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
/*hostent * thishost = gethostbyname("");
char *ip = inet_ntoa(*(struct in_addr*)thishost->h_addr_list);*/
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(27105);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
int ret = bind(ListenSocket, (SOCKADDR*)&sin, sizeof(sin));
if (ret == SOCKET_ERROR)
{
closesocket(ListenSocket);
WSACleanup();
cout << "bind() failed with error " << GetLastError() << endl;
return -1;
}
listen(ListenSocket, 5);
cout << "Server start listening..." << endl;
AcceptSocket = accept(ListenSocket, NULL, NULL);
cout << "accept() successfully." << endl;
EventArray[EventTotal] = WSACreateEvent();
ZeroMemory(buffer, sizeof(buffer));
ZeroMemory(&AcceptOverlapped, sizeof(AcceptOverlapped));
AcceptOverlapped.hEvent = EventArray[EventTotal];
++EventTotal;
DataBuf.len = MAXDATASIZE;
DataBuf.buf = buffer;
while (true)
{
DWORD Index;
if (WSARecv(AcceptSocket, &DataBuf, 1, &RecvByte, &Flags, &AcceptOverlapped, NULL) == SOCKET_ERROR)
{
if (GetLastError() != WSA_IO_PENDING)
{
cout << "Error occured at WSARecv()" << endl;
}
}
Index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, FALSE);
WSAGetOverlappedResult(AcceptSocket, &AcceptOverlapped, &ByteTransferred, FALSE, &Flags);
cout << "WSAGetOverlappedResult() successfully." << endl;
if (ByteTransferred == 0)
{
cout << "Closing Socket : " << AcceptSocket << endl;
closesocket(ListenSocket);
WSACleanup();
return 0;
}
cout << "RECV :" << DataBuf.buf << endl;
ret = WSASend(AcceptSocket, &DataBuf, 1, &RecvByte, Flags, &AcceptOverlapped, NULL);
if (ret == SOCKET_ERROR)
{
cout << "WSASend() occur error." << endl;
}
WSAResetEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
Flags = 0;
ZeroMemory(buffer, sizeof(buffer));
ZeroMemory(&AcceptOverlapped, sizeof(AcceptOverlapped));
AcceptOverlapped.hEvent = EventArray[Index - WSA_WAIT_EVENT_0];
DataBuf.len = MAXDATASIZE;
DataBuf.buf = buffer;
}
closesocket(ListenSocket);
WSACleanup();
system("pause");
return 0;
}
客户端
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include <iostream>
using namespace std;
#define BUF_SIZE 2000
int main()
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR)
{
printf("WSAStartup() 无法初始化!\n");
return -1;
}
SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET)
{
printf("socket() 出错!\n");
WSACleanup();
return -1;
}
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_port = htons(27105);
char cp[] = "127.0.0.1";
clientService.sin_addr.S_un.S_addr = inet_addr(cp);
if (connect(ConnectSocket, (sockaddr*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("connect() 出错!\n");
closesocket(ConnectSocket);
WSACleanup(); system("pause");
return -1;
}
int byteSent, byteRecv;
char buf[BUF_SIZE];
while (true)
{
printf("Input a string to send: ");
cin >> buf;
byteSent = send(ConnectSocket, buf, strlen(buf), 0);
if (byteSent == SOCKET_ERROR)
{
printf("send() error!\n");
closesocket(ConnectSocket);
::WSACleanup();
return -1;
}
printf("send() successfully!\n");
byteRecv = recv(ConnectSocket, buf, BUF_SIZE, 0);
if (byteRecv == 0 || byteRecv == WSAECONNRESET)
{
printf("Connection Closed!\n");
break;
}
buf[byteRecv] = '\0';
printf("Recv from Server: %s\n", buf);
if (strcmp(buf, "quit") == 0)
{
printf("quit!\n");
break;
}
}
closesocket(ConnectSocket);
WSACleanup();
system("pause");
return 0;
}