该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
实现Client和Server相互通话
Client:
#include "stdafx.h"
#include
#include
#include
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
char RecvBuffer[MAX_PATH];
int main(int argc, char * argv[])
{
WSADATA ws;
SOCKET ClientSocket;
struct sockaddr_in ServerAddr;
int Ret = 0;
int AddrLen = 0;
HANDLE hThread = NULL;
char SendBuffer[MAX_PATH];
//初始化Windows端口
if ( WSAStartup(MAKEWORD(2, 2), &ws) !=0 )
{
cout << "初始化Windows端口失败::" << GetLastError() << endl;
return -1;
}
//创建端口
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( ClientSocket == INVALID_SOCKET )
{
cout << "建立Socket失败::" << GetLastError() << endl;
return -1;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
ServerAddr.sin_port = htons(PORT);
memset(ServerAddr.sin_zero, 0x00, 8);
Ret=connect(ClientSocket, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr));
if ( Ret == SOCKET_ERROR )
{
cout << "连接服务器失败::" << GetLastError() << endl;
return -1;
}
else
{
cout << "连接成功!" << endl;
}
while ( true )
{
cin.getline(SendBuffer, sizeof(SendBuffer));
Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
if ( Ret == SOCKET_ERROR )
{
cout << "发送信息失败::" << GetLastError() << endl;
break;
}
}
closesocket(ClientSocket);
WSACleanup();
return 0;
}
Server:
#include "stdafx.h"
#include "windows.h"
#include
#include
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
struct sockaddr_in LocalAddr, ClientAddr;
DWORD WINAPI ClientThread(LPVOID lpParameter)
{
SOCKET ClientSocket = (SOCKET)lpParameter;
int Ret = 0;
char RecvBuffer[MAX_PATH];
while ( true )
{
memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
Ret = recv(ClientSocket, RecvBuffer, MAX_PATH, 0);
if ( Ret == 0 || Ret == SOCKET_ERROR)
{
cout << "客户端退出!" << endl ;
break;
}
cout << "接收到" << inet_ntoa(ClientAddr.sin_addr)<< ":" << ClientAddr.sin_port <
}
return 0;
}
int main(int argc, char * argv[])
{
WSADATA ws;
SOCKET ServerSocket, ClientSocket;
//struct sockaddr_in LocalAddr, ClientAddr;
int Ret = 0;
int AddrLen = 0;
HANDLE hThread = NULL;
//初始化Windows端口
if (WSAStartup(MAKEWORD(2,2), &ws) != 0 )
{
cout << "初始化Windows Socket失败::" << GetLastError() << endl;
return -1;
}
//创建端口
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ServerSocket == INVALID_SOCKET)
{
cout << "建立socket端口失败::" << GetLastError() << endl;
return -1;
}
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
LocalAddr.sin_port = htons(PORT);
memset(LocalAddr.sin_zero, 0x00, 8);
//绑定端口
Ret = bind(ServerSocket, (struct sockaddr * )&LocalAddr, sizeof(LocalAddr));
if ( Ret != 0 )
{
cout << "绑定端口失败::" << GetLastError() << endl;
return -1;
}
Ret = listen(ServerSocket, 10);
if ( Ret !=0 )
{
cout << "监听端口失败::" << GetLastError() << endl;
return -1;
}
cout << "服务端已启动" << endl;
while(true)
{
AddrLen = sizeof(ClientAddr);
ClientSocket = accept(ServerSocket, (struct sockaddr *)&ClientAddr, &AddrLen);
if ( ClientSocket == INVALID_SOCKET)
{
cout << "Accept失败::" << GetLastError() << endl;
break;
}
cout << "客户端连接::" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << endl;
hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)ClientSocket, 0, NULL);
if ( hThread == NULL )
{
cout << "建立线程失败!" << endl;
break;
}
CloseHandle(hThread);
}
closesocket(ServerSocket);
closesocket(ClientSocket);
WSACleanup();
return 0;
}