socket 互相通信

//Server.cpp
#pragma comment(lib,"ws2_32.lib")

#include <iostream>
#include <Windows.h>

using namespace std;

#define  PORT 4000
#define  IP_ADDRESS "10.10.106.143"

DWORD WINAPI RecvThread(LPVOID lpParameter)
{
	SOCKET CientSocket = (SOCKET)lpParameter;
	int Ret = 0;
	char RecvBuffer[MAX_PATH];

	while ( true )
	{
		memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
		Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<"Client quit"<<endl;
			break;
		}
		cout<< endl <<"recv from client are:"<<RecvBuffer<<endl;
		// send
		char SendBuffer[MAX_PATH];
		memset(SendBuffer, 0x00, sizeof(SendBuffer));
		cin.getline(SendBuffer, sizeof(SendBuffer));
		Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<"Client quit"<<endl;
			break;
		}
	}

	return 0;
}

int main(int argc, char* argv[])
{
	WSADATA  Ws;
	SOCKET ServerSocket, CientSocket;
	struct sockaddr_in LocalAddr, ClientAddr;
	int Ret = 0;
	int AddrLen = 0;
	HANDLE hThreadRecv = NULL, hThreadSend= NULL;

	//Init Windows Socket
	if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
	{
		cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
		return -1;
	}

	//Create Socket
	ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if ( ServerSocket == INVALID_SOCKET )
	{
		cout<<"Create Socket Failed::"<<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);

	//Bind Socket
	Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
	if ( Ret != 0 )
	{
		cout<<"Bind Socket Failed::"<<GetLastError()<<endl;
		return -1;
	}

	Ret = listen(ServerSocket, 10);
	if ( Ret != 0 )
	{
		cout<<"listen Socket Failed::"<<GetLastError()<<endl;
		return -1;
	}

	cout<<"server starts:"<<endl;


	while ( true )
	{
		AddrLen = sizeof(ClientAddr);
		CientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
		if ( CientSocket == INVALID_SOCKET )
		{
			cout<<"Accept Failed::"<<GetLastError()<<endl;
			break;
		}

		cout<<"client connected::"<<inet_ntoa(ClientAddr.sin_addr)<<":"<<ClientAddr.sin_port<<endl;
		// recv
		hThreadRecv = CreateThread(NULL, 0, RecvThread, (LPVOID)CientSocket, 0, NULL);
		if ( hThreadRecv == NULL )
		{
			cout<<"Create Recv Thread Failed!"<<endl;
			break;
		}
		CloseHandle(hThreadRecv);
	}
	

	
	closesocket(ServerSocket);
	closesocket(CientSocket);
	WSACleanup();

	return 0;
}

//Client.cpp
#pragma comment(lib,"ws2_32.lib")

#include <iostream>
#include <Windows.h>

using namespace std;

#define  PORT 4000
#define  IP_ADDRESS "10.10.106.143"


DWORD WINAPI RecvThread(LPVOID lpParameter)
{
	SOCKET CientSocket = (SOCKET)lpParameter;
	int Ret = 0;
	char RecvBuffer[MAX_PATH];

	while ( true )
	{
		memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
		Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<"Client quit"<<endl;
			break;
		}
		cout<<"recv from client are:"<<RecvBuffer<<endl;
	}

	return 0;
}

DWORD WINAPI SendThread(LPVOID lpParameter)
{
	SOCKET CientSocket = (SOCKET)lpParameter;
	int Ret = 0;
	char SendBuffer[MAX_PATH];

	while ( true )
	{
		cin.getline(SendBuffer, sizeof(SendBuffer));
		Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<"Send to Client failed."<<endl;
			break;
		}
		//cout<<"Send from client are:"<<RecvBuffer<<endl;
	}

	return 0;
}

int main(int argc, char* argv[])
{
	WSADATA  Ws;
	SOCKET CientSocket;
	struct sockaddr_in ServerAddr;
	int Ret = 0;
	int AddrLen = 0;
	HANDLE hThreadRecv = NULL, hThreadSend= NULL;
	char SendBuffer[MAX_PATH];

	//Init Windows Socket
	if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
	{
		cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl;
		return -1;
	}

	//Create Socket
	CientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if ( CientSocket == INVALID_SOCKET )
	{
		cout<<"Create Socket Failed::"<<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(CientSocket,(struct sockaddr*)&ServerAddr, sizeof(ServerAddr));
	if ( Ret == SOCKET_ERROR )
	{
		cout<<"Connect Error::"<<GetLastError()<<endl;
		return -1;
	}
	else
	{
		cout<<"Succeed connected!"<<endl;
	}
	while (true)
	{
		//send
		char SendBuffer[MAX_PATH];
		cin.getline(SendBuffer, sizeof(SendBuffer));
		Ret = send(CientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<"Send to Client failed."<<endl;
			break;
		}
		//recv
		char RecvBuffer[MAX_PATH];
		memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
		Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
		if ( Ret == 0 || Ret == SOCKET_ERROR ) 
		{
			cout<<endl << "Client quit"<<endl;
			break;
		}
		cout<<endl << "Recv from server are:"<< RecvBuffer <<endl;
	}

	
	closesocket(CientSocket);
	WSACleanup();

	return 0;
}

参考: http://www.cnblogs.com/wmj/archive/2010/03/10/1682355.html

你可能感兴趣的:(windows,struct,socket,null,Path,winapi)