//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"); }