c++ winsocket select(八)

1 带宽破2M,去掉打印语句

2 select 可以实现无阻赛

二 代码

1 mysocket.h

#include 
#include
#include"mythread.h"
const int RECEIVE_BUF_SIZE = 10240;
const int DATA_BUF_SIZE = 102400;
class MySocket : public MyThread{


public :
  SOCKET m_socket;
  SOCKET m_clientsocket;
  std::vector v_clients;
	MySocket(){
		this->InitSocket();
	};
	~MySocket(){
	};
	void InitSocket();
	void Bind();
	void Listen();
	void Accept();
	void SendMessage(void * p , int len);
    int ReceiveMessage(void * p, int len);
	void Connect();
	int MySelect(void * p , int len);
	void run();
};

2 mythread.h

#ifndef MYHEAD_H

#define MYHEAD_H
class MyThread  {
public:
	MyThread(){
	};
	~MyThread(){
	};
	void Start();
	 
	 static void   Callback(void * );
	virtual void run()=0;
};
#endif

3 client.cpp

#include 
#include "mysocket.h"
struct LoginInfo{
  char username[100];
  int password;
} ;
void main11(){
LoginInfo logininfo;
//strcpy(logininfo.username,"xiongwei");
//logininfo.password=123456;
MySocket * clientsocket = new MySocket();
clientsocket->Connect();

int i=0;
while(1){
/*printf("请输入用户名:");
scanf("%s",logininfo.username);
printf("请输入密码:");
scanf("%d",&logininfo.password);*/
	strcpy(logininfo.username,"xiongwei");
logininfo.password=1;
//printf("用户名:%s,密码:%d",(char *)&logininfo, *((int*)((char *)(&logininfo)+100)));
		clientsocket->SendMessage(&logininfo,sizeof(LoginInfo));
		//i=i+1;
//Sleep(100);
}
system("pause");
}

4 mysocket.cpp

#include 
#include 
#include 
#include "mysocket.h"
#include "mythread.h"
#pragma comment(lib, "ws2_32.lib") //socket编程需要引用该库

void MySocket::InitSocket(){
int sys_fun_result = 0;
//WSADATA变量,包含windows socket执行的信息
WSADATA wsa_data;
// 初始化winsock动态库(ws2_32.dll),MAKEWORD(2, 2)用于请求使用winsock2.2版本
sys_fun_result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (sys_fun_result != 0) {
       std::cout << "WSAStartup() function failed: " << sys_fun_result << "\n";
       system("pause");
        return ;
    }
m_socket=socket(PF_INET,SOCK_STREAM,0);//创建套接字
if(SOCKET_ERROR ==m_socket){
  std::cout << "create socket function failed:"<Start();
	 //printf("用户名= ,密码=");
	  // char * sendBuf = (char *)malloc(len);//用堆空间
	   int sys_fun_result =0;
	   //memcpy(sendBuf,p,len);
	   sys_fun_result= send(m_clientsocket,(char *)p,len,0);
	  // free(sendBuf);
	   if(sys_fun_result==SOCKET_ERROR){
	   std::cout << "SendMessage() function failed with error: " << WSAGetLastError() << "\n";
	 }
    }
	   int MySocket::ReceiveMessage(void * p , int len){
		   int sys_fun_result =0;
		// char * receiveBuf  = (char *)malloc( RECEIVE_BUF_SIZE);
	
	   //  memset(receiveBuf,0,sizeof(receiveBuf));
		 sys_fun_result=recv(m_clientsocket,(char *)p,len,0);
		 // printf("sys_fun_result = %d, 用户名= %s,密码= %d",sys_fun_result,(char *)p, *((int *)((char *)p+100)));
		 return sys_fun_result;
	   }

	   void MySocket::Connect(){
	     int sys_fun_result = 0;
    SOCKADDR_IN clientsock_in;
    clientsock_in.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    clientsock_in.sin_family=AF_INET;
    clientsock_in.sin_port=htons(16001);
	sys_fun_result = connect(m_socket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));
 
	if (sys_fun_result == SOCKET_ERROR) {
          std::cout << "connect() function failed with error: " << WSAGetLastError() << "\n";
         // WSACleanup();
         // system("pause");
         // return ;
      }


	   }

	   int MySocket::MySelect(void * p , int len){
		      int sys_fun_result = 0;
			  int receive_len = -1;
		   fd_set fdread;
		    FD_ZERO(&fdread);
      FD_SET(m_socket, &fdread);
	  timeval t={0,0};
	 // for(size_t n=v_clients.size()-1;n>=0;n--){
	  for(int n=(int)v_clients.size()-1;n>=0;n--){
	    FD_SET(v_clients[n],&fdread);
	  }
        sys_fun_result = select(0, &fdread, NULL, NULL,&t);
        if(SOCKET_ERROR == sys_fun_result)
        {
            printf("select error:%d.\n", WSAGetLastError());
        }
		  if(sys_fun_result > 0)
        {
		
			
            if(FD_ISSET(m_socket, &fdread)) //判断m_socket是否留在 fdread集合中,即判断是否有客户端发起连接
            {
				FD_CLR(m_socket,&fdread);//删除
               this->Accept();
			   v_clients.push_back(m_clientsocket);
	
			  // FD_SET(m_clientsocket,&fdread);
			}
			for(size_t n= 0;nReceiveMessage( p,len);
	      }
        
	
	
        }
		  return receive_len;
    }
	   

5 mythread.cpp

#include
#include
#include "mythread.h"
void MyThread::Start( ){

	 _beginthread( Callback , 0, this );
};
void  MyThread::Callback(void * p){
	      MyThread * obj = (MyThread *) p;
		  obj->run();
}

6 server.cpp

#include 

#include "mysocket.h"

struct LoginInfo{
  char username[100];
  int password;
};
int readdatabuf(void * p,void * databuf,int copy_startpos,int len){
	int pos_tail = 0;
	 memcpy ((char *)databuf+copy_startpos,p,len);
	if(len>=sizeof(LoginInfo))
	 {
	  printf("len = %d ,用户名= %s,密码= %d\n",len,(char *)(databuf), *((int *)((char *)databuf+100)));
		// printf("len = %d ,用户名= %s,密码= %d\n",len,(char *)(databuf), *((int *)((char *)databuf+100)));
	   pos_tail = sizeof(LoginInfo);
	    memcpy(databuf,(char *)databuf+pos_tail,DATA_BUF_SIZE-(pos_tail));
		copy_startpos =len - pos_tail;
	 }
	 else{
		  printf("error = %d\n",len);
		  pos_tail = 0;
		  //return copy_startpos;
	 }
	 return copy_startpos;
}
void main(){
	void * receivebuf ; 
	 void * databuf ;
	receivebuf = malloc(RECEIVE_BUF_SIZE);
	

	memset(receivebuf,0,RECEIVE_BUF_SIZE);
	 databuf = malloc(DATA_BUF_SIZE);
	 memset(databuf,0,DATA_BUF_SIZE);
	MySocket * serversocket = new MySocket();
	
	int len;
	int last_pos =0 ;
	serversocket->Bind();
	serversocket->Listen();
	while(1){
	//serversocket->Accept();
	
	len= serversocket->MySelect(receivebuf,RECEIVE_BUF_SIZE);
	if(len==-1){
		//free(receivebuf);
	  continue;
	}
	last_pos= readdatabuf(receivebuf,databuf,last_pos,len);
 //  printf("len = %d ,用户名= %s,密码= %d\n",len,(char *)(receivebuf), *((int *)((char *)receivebuf+100)));
	//Sleep(5000);
	}
	free(receivebuf);
	system("pause");
}

 

你可能感兴趣的:(c++)