www.pudn.com > chat5.zip > chat5.cpp, change:2012-11-14,size:3741b


// chat5.cpp : Defines the entry point for the console application. 
// 
#include "stdafx.h" 
#include<winsock2.h> 
#pragma comment (lib,"ws2_32.lib") 
#include<stdlib.h> 
#define maxsize 500 
int main(int argc, char* argv[]) 
{ 
   //定义变量 
   WSADATA  wsaData; 
   SOCKET  sServer;  //服务器socket,用于监听客户端请求 
   SOCKET  sClient ;  //客户端socket,用于实现与客户端的通信 
   char  buffer[maxsize];   
   int  r; 
 
  //初始化windows sockets 
	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0) 
	{ 
		printf("WSAStartup 无法初始化!"); 
		return 0; 
	} 
  //若初始化成功,则显示wsaDATA中的数据 
	printf("Version:%d.%d\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion)); 
    printf("HOGH Version:%d.%d",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion)); 
	printf("dESCRIPTION:%s\n",wsaData.szDescription); 
	printf("System Status:%s",wsaData.szSystemStatus); 
  
  //创建用于监听的socket 
	sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 
	if(INVALID_SOCKET == sServer) 
	{ 
		printf("socket failed !\n"); 
		WSACleanup(); 
		return -1; 
	} 
	//设置SOCKET为阻塞模式 
	int iMode = 1; 
	r=ioctlsocket(sServer,FIONBIO,(u_long FAR*)&iMode); 
	if(SOCKET_ERROR==r) 
	{    
		printf("ioctlsocket failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
	} 
  //设置服务器地址 
	SOCKADDR_IN addrser; 
	addrser.sin_family=AF_INET; 
	addrser.sin_port=htons(9990); 
	addrser.sin_addr.S_un.S_addr=htonl(INADDR_ANY); //监听的地址为0.0.0.0,任意的地址 
  //绑定socket server 到本地地址 
	r=bind(sServer,(SOCKADDR*)&addrser,sizeof(addrser)); 
	if(SOCKET_ERROR==r) 
	{ 
		printf("bind failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
	} 
	//在SOCKET SERVER上进行监听 
	r=listen(sServer,1); 
    if(SOCKET_ERROR==r) 
	{ 
		printf("bind failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
	} 
 
	//调用accept()函数等待来自客户端的请求 
	printf("TCP Server Start....\n"); 
	sockaddr_in addrClient; 
  int addrClientlen=sizeof(addrClient); 
   while(true) 
   { 
	sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);//sServer用来倾听,sClient用来连接 
 
	if(INVALID_SOCKET==sClient) 
	{     
		int err=WSAGetLastError(); 
		if(err=WSAEWOULDBLOCK) 
		{ 
			Sleep(100); 
			continue; 
		} 
		else 
		{ 
		printf("accept failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
		} 
	} 
	break; 
   } 
   
 
	while(true){ 
 
		ZeroMemory(buffer,maxsize); 
		r=recv(sClient,buffer,maxsize,0); 
	if(SOCKET_ERROR==r) 
	{ 
		int err=WSAGetLastError(); 
		if(err=WSAEWOULDBLOCK) 
		{ 
			Sleep(100); 
			continue; 
		} 
		else if(err==WSAETIMEDOUT || err == WSAENETDOWN) 
		{ 
		printf("recv failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
		} 
	} 
	printf("%s,Recv From Client [%d]:%s\n",inet_ntoa(addrClient.sin_addr),addrClient.sin_port,buffer); 
	if(strcmp(buffer,"quit")==0) 
	{ 
		r=send(sClient,"quit",strlen("quit"),0); 
		break; 
	} 
 
	else 
	{ 
		char msg[maxsize]; 
		ZeroMemory(msg,maxsize); 
		sprintf(msg,"Message received - %s",buffer); 
    while(true) 
	{ 
		r=send(sClient,msg,strlen(msg),0); 
		if(SOCKET_ERROR==r) 
	{ 
			int err=WSAGetLastError(); 
		if(err=WSAEWOULDBLOCK) 
		{ 
			Sleep(500); 
			continue; 
		} 
		else 
		{ 
		printf("bind failed!\n"); 
		closesocket(sServer); 
			closesocket(sClient); 
		WSACleanup(); 
		return -1; 
		} 
	} 
		break; 
	} 
	} 
} 
  closesocket(sServer); 
	closesocket(sClient); 
		WSACleanup(); 
		system("pause"); 
		return 0; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
 
 

2018无需申请注册送58体验金