`
zjh0588
  • 浏览: 65131 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Dos-Socket同步例子(C++)

    博客分类:
  • c++
阅读更多
//SocketObject.h
#include <winsock.h>
#include <iostream.h>

class SocketObject
{
public:
	SOCKET skSocket;
	SocketObject();
	~SocketObject();
	int Bind(int iPort);
	void Disconnect();
	int Listen(void);
	bool Accept(SocketObject &skAcceptSocket);
	bool Connect(char* szServerAddress,int iPort);
	int Send(char *szBuffer,int iBufLen, int iFlags);
	int Recv(char *szBuffer,int iBufLen,int iFlags);
protected:
private:
};

//SocketObject.cpp
#include "SocketObject.h"
#include <stdio.h>
SocketObject::SocketObject(){
	WSADATA wsaData;
	WORD wVersionRequested;

	wVersionRequested = MAKEWORD(2,0);
	skSocket = INVALID_SOCKET;
	int iStatus = WSAStartup(wVersionRequested,&wsaData); //0代表成功,非0:错误编号
	printf("%d\n",iStatus);
	skSocket = socket(AF_INET,SOCK_STREAM,0);
}

SocketObject::~SocketObject(){
	Disconnect();
}

void SocketObject::Disconnect(){
	if(skSocket!= INVALID_SOCKET){
		closesocket(skSocket);
		skSocket = INVALID_SOCKET;
	}
}

int SocketObject::Bind(int iPort){
	sockaddr_in saServerAddress;
	if (skSocket == INVALID_SOCKET)
	{
		return false;
		cout<<"**ERROR** Could Not Create Socket"<<endl;
	}
	memset(&saServerAddress,0,sizeof(sockaddr_in));
	saServerAddress.sin_family = AF_INET;
	saServerAddress.sin_addr.s_addr = htonl(INADDR_ANY);
	saServerAddress.sin_port = htons(iPort);
	if (bind(skSocket,(sockaddr*)&saServerAddress,sizeof(sockaddr))==SOCKET_ERROR)
	{
		Disconnect();
		cout<<"flase"<<endl;
		return false;
	}
	else
		return true;
}

int SocketObject::Listen(void){
	return listen(skSocket,32);
}

bool SocketObject::Accept(SocketObject &skAcceptSocket){
	sockaddr_in saClientAddress;
	int iClientSize = sizeof(sockaddr_in);
	skAcceptSocket.skSocket = accept(skSocket,(struct sockaddr*)&saClientAddress,&iClientSize);
	if(skAcceptSocket.skSocket == INVALID_SOCKET){
		return false;
	}else{
		return true;
	}	
}

bool SocketObject::Connect(char* szServerAddress,int iPort){
	struct sockaddr_in serv_addr;
	LPHOSTENT lpHost;
	int err;
	skSocket = socket(AF_INET,SOCK_STREAM,0);
	if (skSocket == INVALID_SOCKET)
	{
		return false;
	}
	memset(&serv_addr,0,sizeof(sockaddr_in));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr(szServerAddress);
	if(serv_addr.sin_addr.s_addr == INADDR_NONE){
		lpHost = gethostbyname(szServerAddress);
		if (lpHost!=NULL)
		{
			serv_addr.sin_addr.s_addr = ((LPIN_ADDR)lpHost->h_addr)->s_addr;
		}else{
			return false;
		}
	}
	serv_addr.sin_port = htons(iPort);
	err = connect(skSocket,(struct sockaddr*)&serv_addr,sizeof(sockaddr));
	if(err == SOCKET_ERROR){
		Disconnect();
		return false;
	}
	return true;
}

int SocketObject::Send(char *szBuffer,int iBufLen, int iFlags){
	return send(skSocket,szBuffer,iBufLen,iFlags);
}

int SocketObject::Recv(char *szBuffer,int iBufLen,int iFlags){
	return recv(skSocket,szBuffer,iBufLen,iFlags);
}


//Server.cpp
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include "..\\SocketObject\SocketObject.h"

void vServerConnection(int iListenPort){
	SocketObject ServerSocketObject;
	SocketObject ClientSocketObject;
	char DataPacket[128];
	int iBytesReceived = 0;
	int iBytesSent = 0;
	char str[128];
	bool bFlags = false; 	

	cout<<"<Server> Attempting to listen on Port "<<iListenPort<<endl;
	
	if (ServerSocketObject.Bind(iListenPort))
	{
		cout<<"<Server> Listening"<<endl;
		ServerSocketObject.Listen();
		ServerSocketObject.Accept(ClientSocketObject);
		cout<<"<Server> Client Connected to Port "<<iListenPort<<endl;

		while (1)
		{
			while (bFlags)
			{
				cout<<"<Server> You Can Send Msg."<<endl;
				scanf("%s",&str);
				strcpy(DataPacket,str);
				iBytesSent = ClientSocketObject.Send(DataPacket,128,0);
				if (iBytesSent == -1)
				{
					cout<<"<Server> Server No Longer Online!"<<endl;
						break;
				}else{
					bFlags = false;
				}
		    	cout<<"<Server> Transmitted "<<iBytesSent<<" Bytes"<<endl;	
			}
			cout<<"<Server> Waiting msg!"<<endl;
			iBytesReceived = ClientSocketObject.Recv(DataPacket,128,0);
			cout<<"<Server> Received "<<iBytesReceived<<" Bytes"<<endl;
			if (iBytesReceived == -1)
			{
				cout<<"<Server> Client No Longer Online!"<<endl;
					break;
			}else{
				bFlags = true;
			}
			cout<<"<Server> Data Received = "<<DataPacket<<endl;	
		}
		
		ClientSocketObject.Disconnect();
		cout<<"<Server> Client Disconnected"<<endl;
	} 
	else
	{
		cout<<"<Server> Failed to Listen"<<endl;
	}
}

void main(void){
	cout<<"Welcom to SimpleChatServer!"<<endl;
	vServerConnection(1000);
	system("pause");
}


//Client.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include "..\\SocketObject\SocketObject.h"

void vClientConnection(char *szServerIP,int iServerListenPort){
	SocketObject ClientSocketObject;
	char DataPacket[128];
	int iBytesSent = 0;
	int iBytesReceived = 0;
	char str[128];
	bool bFlags = true;

	cout<<"<Client> Connecting to "<<szServerIP<<",Port"<<iServerListenPort<<endl;
	if(ClientSocketObject.Connect(szServerIP,iServerListenPort)){
		cout<<"<Client> Connected"<<endl;
		while (1)
		{	
			while (bFlags)
			{
				cout<<"<Clinet> You Can Send Msg."<<endl;
				scanf("%s",&str);
				strcpy(DataPacket,str);
				iBytesSent = ClientSocketObject.Send(DataPacket,128,0);
				if (iBytesSent == -1)
				{
					cout<<"<Clinet> Client No Longer Online!"<<endl;
					break;
				}else{
					bFlags = false;
				}
				cout<<"<Client> Transmitted "<<iBytesSent<<" Bytes"<<endl;
			}
			cout<<"<Clinet> Waiting msg!"<<endl;
			iBytesReceived = ClientSocketObject.Recv(DataPacket,128,0);
			if (iBytesReceived == -1)
			{
				cout<<"<Clinet> Server No Longer Online!"<<endl;
				break;
			}else{
				bFlags = true;
				}
			cout<<"<Client> Received "<<iBytesReceived<<" Bytes"<<endl;
			cout<<"<Client> Data Received = "<<DataPacket<<endl;	
		}
		ClientSocketObject.Disconnect();
	    cout<<"<Client> Disconnected From Server"<<endl;	
	}else{
		cout<<"<Client> Failed to Connect"<<endl;
		}
}

void main(void){
	cout<<"Welcom to SimpleChatClient!"<<endl;
	vClientConnection("127.0.0.1",1000);
	system("pause");
}
分享到:
评论

相关推荐

    Visual C++2005教程图形教学

    - 通过一个聊天程序的例子展示了如何使用Socket进行网络通信编程。 综上所述,《Visual C++2005教程图形教学》涵盖了从基础到进阶的Windows应用程序开发内容,不仅介绍了.NET Framework的基本使用方法,还深入探讨...

    深入浅出Win32多线程程序设计.pdf

    理解多线程及其相关的同步机制是理解现代操作系统的关键之一。掌握了Win32多线程程序设计之后,不仅能够更好地理解Win32系统本身,还能为学习其他操作系统的多任务管理奠定坚实的基础。例如,虽然很多程序员没有接触...

Global site tag (gtag.js) - Google Analytics