实现的功能:服务器端向客户端发送20到1的数字,客户端收到数据后在回送给服务器。
客户端代码:
// client_test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <WinSock2.h> //使用socket需要的头文件
#if WINCE //client在wince设备上,所以这里包含的是ws2库
#pragma comment(lib,"ws2")
#else
#pragma comment(lib,"ws2_32")
#endif
#define SERVER_IP "10.11.11.219"
#define PORT_NUM (4567)
#define DataBufferLength 5
SOCKET g_sClient = INVALID_SOCKET;
char uDataBuffer[DataBufferLength] = {0}; //数据buffer
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1),&wsaData) != 0) //初始化socket
{
return 0;
}
//创建一个基于TCP协议的套接口
g_sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (g_sClient == INVALID_SOCKET)
{
printf("Failed to create socket!\n");
return 0;
}
//填写远程地址信息
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(PORT_NUM);//端口号
servAddr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP); //服务器的ip地址
//建立与服务器的socket连接,需要先打开服务器,否则这里会连接失败
if (connect(g_sClient, (sockaddr *)&servAddr, sizeof(servAddr)) == -1)
{
printf("Failed to connect server!\n");
closesocket(g_sClient); //关闭套接口
return 0;
}
int nRecv;
int SendLength = 1;
while(true)
{
//在这里挂起等待,直到接收到数据
nRecv = recv(g_sClient, uDataBuffer, DataBufferLength, 0);
if (nRecv <=0) //如果接收到的字节数小于等于0,表示出错
{
printf("recv data failed!\n");
return 0;
}
//将接收到的数据发送回服务器,send()返回值为实际发送数据的数目
if ( send(g_sClient, uDataBuffer, SendLength, 0) != SendLength)
{
printf("Ack is failed!\n");
}
}
closesocket(g_sClient);
return 1;
}
服务器端代码:
// server_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WinSock2.h> //使用socket需要的头文件
#if WINCE //server在pc设备上,所以这里包含的是ws2_32库
#pragma comment(lib, "ws2")
#else
#pragma comment(lib, "ws2_32")
#endif
#define PORT_NUM (4567)
SOCKET g_sClient = INVALID_SOCKET; //客户端套接口
SOCKET g_sListen = INVALID_SOCKET; //服务器端套接口
char g_uDataBuffer[5] = {0}; //数据buffer
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) //初始化socket
{
return 0;
}
//创建一个套接口
g_sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (g_sListen == INVALID_SOCKET)
{
printf("Failed socket!\n");
return 0;
}
sockaddr_in SockIn;
SockIn.sin_family = AF_INET;
SockIn.sin_port = htons(PORT_NUM); //服务器侦听的端口号
SockIn.sin_addr.S_un.S_addr = INADDR_ANY; //INADDR_ANY表示不关心IP
//绑定这个套接口到PORT_NUM端口
if (bind(g_sListen, (LPSOCKADDR)&SockIn, sizeof(SockIn)) == SOCKET_ERROR)
{
printf("Failed bind!\n");
return 0;
}
//进入侦听模式
if (listen(g_sListen, 2) == SOCKET_ERROR)
{
printf("Failed listen!\n");
return 0;
}
sockaddr_in RemoteAddr;
int nAddrlen = sizeof(RemoteAddr);
//服务器在这里等待客户端的连接请求,并返回客户端的socket类型值
g_sClient = accept(g_sListen, (SOCKADDR *)&RemoteAddr, &nAddrlen);
if (g_sClient == INVALID_SOCKET)
{
printf("received an invalid connection!\n");
return 0;
}
int i = 20;
char receivedata[5] = {0};
for (i = 20; i > 0; i--)
{
g_uDataBuffer[0] = i;
//将g_uDataBuffer[0]发送给客户端
if ( send(g_sClient, g_uDataBuffer, 1, 0) != 1)
{
printf("Failed to send data!\n");
break;
}
//在这里挂起等待,接受客户端的应答数据
int nRecv = recv(g_sClient, receivedata, 5, 0);
if (nRecv <=0)
{
printf("recv Ack failed!\n");
return 0;
}
//Sleep(200);//在客户端不回送数据的情况下,如果不加延时,客户端接受不到所有数据,原因待查。
}
while(1);
return 1;
}