码迷,mamicode.com
首页 > Windows程序 > 详细

Windows Socket编程

时间:2015-02-23 13:10:07      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

Server(多线程)

#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")

using namespace std;

#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
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<<"Exit"<<endl;
			break;
		}
		cout<<"收到的信息为:"<<RecvBuffer<<endl;
	}
	return 0;
}

int main()
{
	WSADATA Ws;
	SOCKET ServerSocket,ClientSocket;
	struct sockaddr_in LocalAddr,ClientAddr;
	int ret=0;
	int AddrLen=0;
	HANDLE hThread=NULL;

	//Init
	if((WSAStartup(MAKEWORD(2,2),&Ws)!=0))
	{
		cout<<"Init 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_un.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<<"Bind Socket Failed:"<<GetLastError()<<endl;
		return -1;
	}

	ret=listen(ServerSocket,10);

	if(ret!=0)
	{
		cout<<"Listen Socket Failed"<<GetLastError()<<endl;
		return -1;
	}
	
	cout<<"Server Start"<<endl;

	while(true)
	{
		AddrLen=sizeof(ClientAddr);
		ClientSocket=accept(ServerSocket,(struct sockaddr *)&ClientAddr,&AddrLen);
		if(ClientSocket==INVALID_SOCKET)
		{
			cout<<"Accept Failed:"<<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<<"Create Thread Failed"<<endl;
			break;
		}
		CloseHandle(hThread);
	}
	closesocket(ServerSocket);
	closesocket(ClientSocket);
	WSACleanup();
	return 0;
}


Client

#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")

using namespace std;

#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
int main()
{
	WSADATA Ws;
	SOCKET ClientSocket;
	struct sockaddr_in ServerAddr;
	int ret=0;
	int AddrLen=0;
	//HANDLE hThread=NULL;
	char SendBuffer[MAX_PATH];

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

	//Create
	ClientSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(ClientSocket==INVALID_SOCKET)
	{
		cout<<"Socket Failed:"<<GetLastError()<<endl;
		return -1;
	}
	ServerAddr.sin_family=AF_INET;
	ServerAddr.sin_addr.S_un.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<<"Connect Error:"<<GetLastError()<<endl;
		return -1;
	}
	else
	{
		cout<<"Connect Success"<<endl;
	}

	while(true)
	{
		cin.getline(SendBuffer,sizeof(SendBuffer));
		ret=send(ClientSocket,SendBuffer,(int)strlen(SendBuffer),0);
		if(ret==SOCKET_ERROR)
		{
			cout<<"Send Error:"<<GetLastError()<<endl;
			break;
		}
	}
	closesocket(ClientSocket);
	WSACleanup();
	return 0;
}



Windows Socket编程

标签:

原文地址:http://blog.csdn.net/tlzhatao/article/details/43916063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!