winsocket的C/S模型程序_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > winsocket的C/S模型程序

winsocket的C/S模型程序

 2013/12/18 21:09:13  羊咩咩  程序员俱乐部  我要评论(0)
  • 摘要:因为这学期要交winsocket的通信程序,一个字,坑。因为上课就没有教的。不过有神器在手,陆楠同学的《计算机网络实训和编程》,so,还不是手到擒来!环境:win7+vs2010项目:C++项目,有预编译头文件先看效果:服务端:客户端:连接后:不过感觉怪怪的。看看client的源文件://TCPEx_Client.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include<winsock2.h>#include<iostream>
  • 标签:程序 socket

因为这学期要交winsocket的通信程序,一个字,坑。因为上课就没有教的。不过有神器在手,陆楠同学的《计算机网络实训和编程》,so,还不是手到擒来!

?

环境:win7+vs2010

项目:C++项目,有预编译头文件

?

先看效果:

服务端

?
客户端:



?

连接后:



??
不过感觉怪怪的。


?看看client的源文件:

class="cpp">// TCPEx_Client.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <winsock2.h>
#include  <iostream> 
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
	const int DEFAULT_PORT = 5000; 
	WORD wVersionRequested;
	WSADATA wsaData;
	int err,iLen;
	if (argc < 2) {
		cout<<"Command:TcpEx_Client ServerIP"<<endl;
		return 0;  } 
	wVersionRequested=MAKEWORD( 2, 2 );
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		cout<<"加载WinSock失败!";
		return 0;  }
	SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);   //创建用于连接的套接字
	if (sockClient == INVALID_SOCKET)  {
		cout<<"socket() fail:"<<WSAGetLastError()<<endl;
		return 0;  }
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrSrv.sin_port = htons(DEFAULT_PORT); 
	err = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//请求连接服务器进程
	if ( err == INVALID_SOCKET ) {
		cout<<"connect() fail:"<<WSAGetLastError()<<endl;
		return 0;  
}
	char sendBuf[1024],hostname[100];
	if (gethostname(hostname,100) != 0)  strcpy_s(hostname,"None");   //获取主机名称
	strcpy_s(sendBuf,hostname);
	strcat_s(sendBuf," have conneted to you!");
	err = send(sockClient,sendBuf,int(strlen(sendBuf))+1,0); //发送数据
	if (err == SOCKET_ERROR)  {
		cout<<"send() fail:"<<WSAGetLastError()<<endl;
		return 0; 
}
	char recvBuf[1024];
	iLen = recv(sockClient,recvBuf,1024,0); //接收数据
	if (iLen == 0)  return 0;
	else if (iLen == SOCKET_ERROR) {
		cout<<"recv() fail:"<<WSAGetLastError()<<endl;
		return 0;  }
	else {
		recvBuf[iLen] = '\0';
		cout<<recvBuf<<endl;  
}
	closesocket(sockClient); //关闭套接字
	WSACleanup();
}

?

再看Sever:

// TCPSrvEx.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <winsock2.h>
#include  <iostream> 
#include <string.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[]) {
	const int DEFAULT_PORT = 5000; 
	WORD wVersionRequested;
	WSADATA wsaData;
	int err,iLen;
	wVersionRequested=MAKEWORD( 2, 2 );
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		cout<<"加载WinSock失败!";
		return 0;
	}
	SOCKET sockSrv = socket(AF_INET,SOCK_STREAM,0); //创建服务器监听套接字
	if (sockSrv == INVALID_SOCKET) {
		cout<<"socket() fail:"<<WSAGetLastError()<<endl;
		return 0;
	}
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//自动分配地址,字节顺序转换
	addrSrv.sin_port = htons(DEFAULT_PORT);//字节顺序转换
	err = bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));  //绑定本地主机IP和端口
	if ( err != 0 ) {
		cout<<"bind() fail:"<<WSAGetLastError()<<endl;
		return 0;
	}
	err = listen(sockSrv,5);	  //监听
	if ( err != 0 ) {
		cout<<"listen() fail:"<<WSAGetLastError()<<endl;
		return 0;
	}
	cout<<"Server waiting...:"<<endl;
	SOCKADDR_IN addrClient;
	int len = sizeof(SOCKADDR);
	while(1) {
		SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&len);//接收客户连接请求
		if (sockConn == INVALID_SOCKET) 	{
			cout<<"accept() fail:"<<WSAGetLastError()<<endl;
			break; }
		char sendBuf[1024],hostname[100];
		if (gethostname(hostname,100) != 0) //获取主机名称
			strcpy_s(hostname,"None");
		sprintf_s(sendBuf,"welcome %s connected to %s!",inet_ntoa(addrClient.sin_addr),hostname);
		err = send(sockConn,sendBuf,int(strlen(sendBuf))+1,0); //发送数据
		if (err == SOCKET_ERROR) {
			cout<<"send() fail:"<<WSAGetLastError()<<endl;
			break;  
}
		char recvBuf[1024] = "\0";
		iLen = recv(sockConn,recvBuf,1024,0);//接收数据
		if (iLen == SOCKET_ERROR)  {
			cout<<"recv() fail:"<<WSAGetLastError()<<endl;
			break;  }
		recvBuf[iLen] = '\0';
		cout<<recvBuf<<endl;
		closesocket(sockConn); //关闭套接字
	}
	closesocket(sockSrv);//关闭服务器监听套接字
	WSACleanup();
	return 0;
}

?

好吧,其实我只是把书本上的例子实现了一遍。

?

看看老师给的例子:

client:

// Client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<WINSOCK2.H>
#include<iostream.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")


void main(int argc, char* argv[])
{
	SOCKET sock ;

	SOCKADDR_IN dest_sin ;

	WSADATA wsaData ;//版本参数

	int port = 344 ;

	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		cout<<"WinSock startup Error!" ;
		return ;        
	}

	sock = socket(AF_INET, SOCK_STREAM, 0) ;

	if (sock == INVALID_SOCKET) {
		cout<<"socket() failed" ;
		closesocket(sock) ;
		return ;
	}

	dest_sin.sin_family = AF_INET ;
	dest_sin.sin_port = htons(port) ;
	dest_sin.sin_addr.s_addr = inet_addr("127.0.0.1") ;

	connect(sock, (sockaddr *)&dest_sin, sizeof(dest_sin)) ;

////连续发十个数给服务器

		char szName[20] ;
		for(int i=0 ; i<20 ; i++)
		{
			sprintf(szName, " %d", i) ;
	        send(sock, szName, 20, 0) ;
		}
		

	  // send(sock, " ",0,0) ;

	   closesocket(sock) ;
		
			
		if (WSACleanup() == SOCKET_ERROR) {
			cout<<"WSACleanup() failed with error "<<WSAGetLastError() ;
		}
   
}

?

Sever:

// Server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<WINSOCK2.H>
#include<iostream.h>
#pragma comment(lib,"ws2_32.lib")


void main(int argc, char* argv[])
{
	SOCKET sockServer ;
	SOCKET sockAccept ;

	SOCKADDR_IN local_sin ;
	SOCKADDR_IN acc_sin ;

	WSADATA wsaData ;

	int port = 344 ;

	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		cout<<"WinSock startup Error!" ;
		return ;
         
	}

	sockServer = socket(AF_INET, SOCK_STREAM, 0) ;
	if (sockServer == INVALID_SOCKET) {
		cout<<"socket() failed" ;
		closesocket(sockServer) ;
		return ;
	}

	local_sin.sin_port = htons(port) ;
	local_sin.sin_family = AF_INET ;
	local_sin.sin_addr.s_addr = INADDR_ANY ;

	if (bind(sockServer, (sockaddr FAR*)&local_sin, sizeof(local_sin)) == SOCKET_ERROR) {
		cout<<"bind(sock) failed "<<WSAGetLastError()<<"is the error!" ;
		return ;
	}

	cout<<"Server begin..."<<endl ;

	if (listen(sockServer, 4)) {
		cout<<"listen(sockserver) failed "<<WSAGetLastError()<<" is the error!" ;
		return ;
	}

	int acc_sin_len = sizeof(acc_sin) ;

	sockAccept = accept(sockServer, (sockaddr FAR*)&acc_sin, (int FAR*)&acc_sin_len) ;

	if (sockAccept<0) {
		cout<<"accept() failed "<<WSAGetLastError()<<" is the error!" ;
		return ;
	}



	char szName[20] ;
//////////////////////////////////////////////////////////////////////////
//等待接收数据
	while (1) {
		int rec = recv(sockAccept, szName, 20, 0) ;
		if( rec == 0) 
		{
			cout<<"Server stop.."<<endl ;
			break ;
		}
		else
			cout<<szName<<endl ;
		
	}

	closesocket(sockServer) ;
	closesocket(sockAccept) ;

	if (WSACleanup() == SOCKET_ERROR) {
		cout<<"WSACleanup() failed with error "<<WSAGetLastError() ;
	}
	

	


	
}

?

?

  • 大小: 14.5 KB
  • 大小: 6.1 KB
  • 大小: 7.3 KB
  • 查看图片附件
发表评论
用户名: 匿名