服务端:
#include <stdlib.h>
#include <stdio.h>
#include <WINSOCK2.H>
#include <WS2TCPIP.H>
#include <windows.h>
//////////////////////////////////////////////////////////////////////////
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
//////////////////////////////////////////////////////////////////////////
int main()
{
WSADATA wsaData;
SOCKET listenSocket = INVALID_SOCKET;
SOCKET clientSocket = INVALID_SOCKET;
char recvBuf[DEFAULT_BUFLEN];
int iResult,iSendResult;
int recvBuflen = DEFAULT_BUFLEN;
iResult = WSAStartup(MAKEWORD(2,0),&wsaData);
if(iResult)
{
printf("WSASTATUP failed!\n");
return 1;
}
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenSocket == INVALID_SOCKET)
{
printf("socket failed:%ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = inet_addr("127.0.0.1");
saServer.sin_port = htons(5150);
iResult = bind(listenSocket,(sockaddr *)&saServer, sizeof(saServer));
if(iResult == SOCKET_ERROR)
{
printf("bind failed!");
closesocket(listenSocket);
WSACleanup();
return 1;
}
iResult = listen(listenSocket, SOMAXCONN);
if(iResult == SOCKET_ERROR)
{
printf("LISTEN failed!");
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Wait client connect");
clientSocket = accept(listenSocket, NULL, NULL);
if(clientSocket == INVALID_SOCKET)
{
printf("accept failed!");
closesocket(listenSocket);
WSACleanup();
return 1;
}
closesocket(listenSocket);
printf("Now wait the data arrive...\n");
do
{
iResult = recv(clientSocket, recvBuf, recvBuflen, 0);
if(iResult > 0)
{
printf("Bytes received:%d\n",iResult);
iSendResult = send(clientSocket, recvBuf, iResult, 0);
if(iSendResult == SOCKET_ERROR)
{
printf("send failed");
closesocket(clientSocket);
WSACleanup();
return 1;
}
else if (iResult == 0)
{
printf("Connect closed!");
}
else
{
printf("recv failed");
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
} while(iResult > 0);
iResult = shutdown(clientSocket, SD_SEND);
if(iResult == SOCKET_ERROR)
{
printf("shut dowm failed!");
closesocket(clientSocket);
WSACleanup();
return 1;
}
closesocket(clientSocket);
WSACleanup();
getchar();
return 0;
}
//客户端
#include <stdlib.h>
#include <stdio.h>
#include <WINSOCK2.H>
#include <WS2TCPIP.H>
#include <windows.h>
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
//////////////////////////////////////////////////////////////////////////
int main()
{
WSADATA wsaData;
SOCKET connectSocket = INVALID_SOCKET;
char *sendBuf = "This is a test";
char recvBuf[DEFAULT_BUFLEN];
int iResult;
int recvBufLen = DEFAULT_BUFLEN;
// if(argc != 2)
// {
// printf("useage:%s server -name\n",argv[0]);
// return 1;
// }
iResult = WSAStartup(MAKEWORD(2,0),&wsaData);
if(iResult)
{
printf("WSASTATUP FAILED.");
return 1;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5150);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(connectSocket == INVALID_SOCKET)
{
printf("Error at socket!");
WSACleanup();
return 1;
}
iResult = connect(connectSocket, (sockaddr *)&addr, sizeof(addr));
if(iResult == SOCKET_ERROR)
{
printf("Connect failed!");
WSACleanup();
return 1;
}
iResult = send(connectSocket, sendBuf, (int)strlen(sendBuf), 0);
if(iResult == SOCKET_ERROR)
{
printf("Send failed");
closesocket(connectSocket);
WSACleanup();
return 1;
}
printf("Bytes send.");
iResult = shutdown(connectSocket, SD_SEND);
if(iResult == SOCKET_ERROR)
{
printf("shut down failed");
closesocket(connectSocket);
WSACleanup();
return 1;
}
do
{
iResult = recv(connectSocket, recvBuf, recvBufLen, 0);
if(iResult > 0)
{
printf("Bytes received:%d\n",iResult);
}
else if(iResult == 0)
{
printf("connect closed");
}
else
{
printf("Recv failed");
}
} while(iResult > 0);
closesocket(connectSocket);
WSACleanup();
return 0;
}
注意要加入链接库WS2_32.lib