Select網路模型

2021-07-05 16:53:37 字數 2993 閱讀 5568

mysockt.h

#pragma once

#define maxnum 64

#include#pragma comment(lib,"ws2_32.lib")

#define um_user wm_user+1 //向視窗發訊息

enum nettype

;class cmysocket

;mysocket.cpp

#include "stdafx.h"

#include "mysocket.h"

cmysocket::cmysocket(void)

cmysocket::~cmysocket(void)

void cmysocket::stopsevere()

pthis->socketwaiter[pthis->m_num]=accept(pthis->socketlisten,null,null);

if(invalid_socket==pthis->socketwaiter[pthis->m_num])

pthis->m_num++;

tchar szbuf[1024]=_t("伺服器鏈結成功");

sendmessage(pthis->m_hparrentwnd,um_user,(wparam)szbuf,1024);

//pthis->m_lstbox.addstring(szbuf);

} setevent(pthis->m_hthreadacceptcheck);

return 0;

}dword winapi cmysocket::threadrecv(lpvoid lpvoid)

char szbuf[1024]=;

tchar tzbuf[1024]=;

for(int i=0;im_num;i++)

}} }

setevent(pthis->m_hthreadrecvcheck);

return 0;

}bool cmysocket::startsevere(hwnd m_hwnd,short nport,cstring nip)

if ( lobyte( wsadata.wversion ) != 2 || hibyte( wsadata.wversion ) != 2 )

//1.建立socket與外界通訊的介面

socketlisten=socket(af_inet,sock_stream,ipproto_tcp);

if(invalid_socket==socketlisten)

sockaddr_in addrsock;

addrsock.sin_family=af_inet;

addrsock.sin_port=htons(nport);

char szbuf[1024];

#ifdef _unicode

widechartomultibyte(cp_acp,0,nip,-1,szbuf,1024,0,0);

#else

strcpy_s(szbuf,1024,nip);

#endif

addrsock.sin_addr.s_addr=inet_addr(szbuf);

int nsize=sizeof(sockaddr_in);

if(socket_error==bind(socketlisten,(const struct sockaddr*)&addrsock,nsize))

監聽if(socket_error==listen(socketlisten,10))

m_heventquit=::createevent(null,true,false,null);

m_hthreadacceptcheck=::createevent(null,true,false,null);

m_hthreadrecvcheck=::createevent(null,true,false,null);

m_hthreadaccept=::createthread(null,0,&cmysocket::threadaccept,this,0,null);

m_hthreadrecv=::createthread(null,0,&cmysocket::threadrecv,this,0,null);

if(!m_heventquit||!m_hthreadacceptcheck||!m_hthreadrecvcheck||!m_hthreadaccept||!m_hthreadrecv)

return 1;

}bool cmysocket::senddata(tchar *tzbuf,int nsize)

char szbuf[1024]=;

#ifdef _unicode

widechartomultibyte(cp_acp,0,tzbuf,-1,szbuf,1024,0,0);

#else

strcpy_s(szbuf,1024,tzbuf);

#endif

for(int i=0;ireturn true;

}bool cmysocket::selectsocket(socket socketwaiter,nettype type)

timeval tv;

tv.tv_sec=0;

tv.tv_usec=100;

//定義乙個集合

fd_set fdsets;

//清空集合

fd_zero(&fdsets);

//將socketwaiter加入到集合

fd_set(socketwaiter,&fdsets);

//將集合交由select管理

if(type==netaccept||type==netrecv)

else if(type==netsend)

//校驗socketwaiter是否在集合

if(fd_isset(socketwaiter,&fdsets))

else

return true;

}

Select 網路模型

maxfd 最大描述符加1 fd set操作集合 include include include include intselect int maxfdp,fd set readset,fd set writeset,fd set exceptset,struct timeval timeout m...

網路程式設計 select模型

select 機制中提供一fd set的資料結構,實際上是一long型別的陣列,每乙個陣列元素都能與一開啟的檔案控制代碼 不管是socket控制代碼,還是其他檔案或命名管道或裝置控制代碼 建立聯絡,建立聯絡的工作由程式設計師完成,當呼叫select 時,由核心根據io狀態修改fe set的內容,由此...

windows網路程式設計 select模型

select模型與一般模型的區別是selete模型使用fd set資料型別和select函式在核心裡維護一張存有連線上的socket表,使用select輪循處理這些socket,實現同時與多個socket連線。基本工作原理 建立socket,繫結,監聽,開個死迴圈,當發現有socket請求連線後把該...