Winsock Demo

Server:

/*filename: TcpSvr.cpp*/

#include <winsock2.h>
#include <stdio.h>

int main(){
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	
	wVersionRequested = MAKEWORD( 1, 1 );
	/************************************************************************/
	/* Step 1: initiates use of WS2_32.DLL by a process						*/
	/************************************************************************/
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		return 1;
	}
	
	/* Confirm that the WinSock DLL supports 2.2.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.2 in addition to 2.2, it will still return */
	/* 2.2 in wVersion since that is the version we      */
	/* requested.                                        */
	
	if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		WSACleanup( );
		return 1; 
	}	
	/* The WinSock DLL is acceptable. Proceed. */

	/************************************************************************/
	/* Step 2: 创建Socket                                                   */
	/************************************************************************/
	SOCKET sockSvr = socket(AF_INET,SOCK_STREAM,0);
	
	SOCKADDR_IN addrSvr;
	addrSvr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	addrSvr.sin_family = AF_INET;
	addrSvr.sin_port = htons(6000);
	/************************************************************************/
	/* Step 3: 绑定Socket                                                   */
	/************************************************************************/
	bind(sockSvr,(SOCKADDR*)&addrSvr,sizeof(SOCKADDR));
	/************************************************************************/
	/* Step 4: 监听Socket                                                   */
	/************************************************************************/
	listen(sockSvr,5);
	printf("开始监听...\n");

	SOCKADDR_IN addrClient;
	int len = sizeof(SOCKADDR);
	while(1){
		//接收Socket
		SOCKET sockConn = accept(sockSvr,(SOCKADDR*)&addrClient,&len);
		char sendBuf[100];
		char * strClientIp = inet_ntoa(addrClient.sin_addr);
		sprintf(sendBuf,"Welcome %s",strClientIp);
		//发送
		send(sockConn,sendBuf,strlen(sendBuf)+1,0);
		
		char recvBuf[100];
		//接收数据
		recv(sockConn,recvBuf,100,0);
		printf("[%s] say: %s\n",strClientIp, recvBuf);
		closesocket(sockConn);
	}
	
	return 0;
}

Client:

#include <winsock2.h>
#include <stdio.h>

int main(){
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	
	wVersionRequested = MAKEWORD( 1, 1 );
	/************************************************************************/
	/* Step 1: initiates use of WS2_32.DLL by a process						*/
	/************************************************************************/
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		return 1;
	}
	
	/* Confirm that the WinSock DLL supports 2.2.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.2 in addition to 2.2, it will still return */
	/* 2.2 in wVersion since that is the version we      */
	/* requested.                                        */
	
	if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		WSACleanup( );
		return 1; 
	}	
	/* The WinSock DLL is acceptable. Proceed. */
	
	/************************************************************************/
	/* Step 2: 创建Socket                                                   */
	/************************************************************************/
	SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
	
	SOCKADDR_IN addrSvr;
	//addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrSvr.sin_addr.S_un.S_addr = inet_addr("192.168.10.233");		//目标地址
	addrSvr.sin_family = AF_INET;
	addrSvr.sin_port = htons(6000);
	
	connect(sockClient,(SOCKADDR*)&addrSvr,sizeof(SOCKADDR));

	char recvBuf[100];
	recv(sockClient,recvBuf,100,0);
	printf("%s\n",recvBuf);

	char msg[]="This is xx!";
	send(sockClient,msg,strlen(msg)+1,0);
	closesocket(sockClient);
	WSACleanup();

	system("pause");
	return 0;
}

  

posted @ 2014-07-23 16:33  庚武  Views(272)  Comments(0)    收藏  举报