服务器端
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#define InternetAddr "127.0.0.1"
#define iPort 6000
#pragma comment(lib, "ws2_32.lib")
#pragma pack(push, 1)
struct Person
{
char name[20];
int age;
};
#pragma pack(pop)
void main()
{
Person person;
memset(&person, 0x00, sizeof(person));
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET fdServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(InternetAddr);
server.sin_port = htons(iPort);
int ret = bind(fdServer, (sockaddr*)&server, sizeof(server));
ret = listen(fdServer, 4);
SOCKET AcceptSocket;
fd_set fdread;
timeval tv;
int nSize;
while (1)
{
FD_ZERO(&fdread);//初始化fd_set
FD_SET(fdServer, &fdread);//分配套接字句柄到相应的fd_set
tv.tv_sec = 0;//这里我们打算让select等待两秒后返回,避免被锁死,也避免马上返回
tv.tv_usec = 0;
select(0, &fdread, NULL, NULL, &tv);
static int num = 0;
nSize = sizeof(server);
if (FD_ISSET(fdServer, &fdread))//如果套接字句柄还在fd_set里,说明客户端已经有connect的请求发过来了,马上可以accept成功
{
num++;
AcceptSocket = accept(fdServer, (sockaddr*)&server, &nSize);
char Buf[] = "back to client";
char sendBuf[100] = { 0 };
sprintf(sendBuf, "[%d] %s", num, Buf);
send(AcceptSocket, sendBuf, strlen(sendBuf) + 1, 0);
char* buffer = new char(sizeof(Person));
// ret = recv(AcceptSocket,(char*) &person, sizeof(Person), 0);
ret = recv(AcceptSocket, buffer, sizeof(Person), 0);
memcpy(&person, buffer, sizeof(Person));
person.age = ntohl(person.age);
printf("num=%d name:%s, age:%d\n",num, person.name, person.age);
// printf("%s\n", buffer);
}
else//还没有客户端的connect请求,我们可以去做别的事,避免像没有用select方式的阻塞套接字程序被锁死的情况,如果没用select,当程序运行到accept的时候客户端恰好没有connect请求,那么程序就会被锁死,做不了任何事情
{
//do something
// ::MessageBox(NULL, "waiting", "recv", MB_ICONINFORMATION);//别的事做完后,继续去检查是否有客户端连接请求
//printf("nothing....\n");
//Sleep(2000);
}
}
char buffer[128];
ZeroMemory(buffer, 128);
ret = recv(AcceptSocket, buffer, 128, 0);//这里同样可以用select,用法和上面一样
// ::MessageBox(NULL, buffer, "recv", MB_ICONINFORMATION);
// printf("%s", buffer);
closesocket(AcceptSocket);
WSACleanup();
return;
}
客户端
#include <Winsock2.h>
#include <stdio.h>
#pragma comment (lib, "ws2_32.lib")
#pragma pack(push, 1)
struct Person
{
char name[20];
int age;
};
#pragma pack(pop)
void main()
{
Person person;
memset(&person, 0x00, sizeof(Person));
char* sn = "Jame";
memcpy(person.name, sn, strlen(sn));
person.age = 14;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return;
}
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
char* str = new char(sizeof(Person));
person.age = htonl(person.age);
memcpy(str, &person, sizeof(Person));
// send(sockClient,(const char*) &person, sizeof(Person), 0);
send(sockClient, str, sizeof(Person), 0);
printf("name:%s, age:%d\n", person.name, ntohl(person.age));
char recvBuf[100];
recv(sockClient, recvBuf, 100, 0);
printf("%s\n", recvBuf);
closesocket(sockClient);
WSACleanup();
getchar();
}