c++ winsocket (七)

一 问题 

  1  打开一个客户端不停地发包,资源监视器的网络IO 显示 1M/s的流量,再打开一个客户端,还是1M/s的流量,而且其中一个客户端会停止发包,过一会又会发包。

      回答: 因为 winsocket默认是阻塞的,accept() ,receive是阻塞函数,当receive没有收到 数据时,程序不会继续往下执行,当socket缓冲区的数据满了,会阻止客户端发送数据 ,会出现粘包。流量1M/s 是因为ui线程打印会消耗CPU。导致不能全力发送数据。

 

1 mysocket.h


const size_t IP_BUF_SIZE = 65;
#include "mythread.h"
class MySocket : public MyThread{
public:
 SOCKET	m_socket;
 SOCKET	m_con;
public:
	MySocket();
	~MySocket();
	void init();
	void mybind();
	void mylisten();
	SOCKET myaccept(); 
	void mysend(char * ch);
	void myrecv();
	void myclose();
	void run();
	void myconnect();
	

};

2 mythread.h


const size_t IP_BUF_SIZE = 65;
#include "mythread.h"
class MySocket : public MyThread{
public:
 SOCKET	m_socket;
 SOCKET	m_con;
public:
	MySocket();
	~MySocket();
	void init();
	void mybind();
	void mylisten();
	SOCKET myaccept(); 
	void mysend(char * ch);
	void myrecv();
	void myclose();
	void run();
	void myconnect();
	

};

3  client.cpp

#include 
#include 
#include "mysocket.h"
enum CMD{
  CMD_LOGIN,
  CMD_LOGOUT
} ;
struct HeaderData{
	int datalegth;
	int cmd;
	 
  
 
};
struct LoginData{
  HeaderData headerdata;
  char username[100];
  int password;
};
void main(){
	    LoginData  logindata;
		logindata.headerdata.datalegth =104;
		logindata.headerdata.cmd = CMD_LOGIN;
	MySocket * mysocket= new MySocket();
	mysocket->init();
	mysocket->myconnect();
	int i =0;
		//std::cout<<"请输入"<mysend((char *)&logindata);
		i=i+1;
	   // Sleep(5000);//每5秒
	}
	
	system("pause");
	
}

4  mymain.cpp

#include 
#include  
#include "mysocket.h"
void receive(void * socket){
  MySocket  * mysocket ;
  mysocket =(MySocket *)socket;
  mysocket->myrecv();
}
void main321(){
	MySocket * mysocket= new MySocket();
	MySocket * clientsocket = new MySocket();
	
	mysocket->mybind();
	mysocket->mylisten();
 
	while(1){
	clientsocket->m_socket = mysocket->myaccept();
	clientsocket->Start();
	//clientsocket->run();
	// _beginthread( receive, 0, clientsocket );
	// mysocket->myrecv(socket);
	clientsocket->myrecv();
	}
	mysocket->myclose();
	system("pause");
	
}

5.mysocket.cpp

#include 
#include 
#include 
#include 
#include "mysocket.h"
#pragma comment(lib, "ws2_32.lib") //socket编程需要引用该库
struct LoginData{
  char username[100];
  int password;
};
MySocket::MySocket(){
	this->init();
}
MySocket::~MySocket(){
}
 void MySocket::init(){
  WSADATA wsa_data; //WSADATA变量,包含windows socket执行的信息
  int sys_fun_result = 0; 
	// 初始化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);//创建套接字
 std::cout<<"Winsock初始化成功"<myrecv();
 }

6 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();
}


 

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