加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SGameSocket.cpp 3.51 KB
一键复制 编辑 原始数据 按行查看 历史
qykings 提交于 2015-01-30 15:56 . modified: SGameSocket.cpp
#include "SGameSocket.h"
SGameSocket* SGameSocket::_instance=new SGameSocket();
SGameSocket* SGameSocket::GetInstance()
{
return _instance;
}
SGameSocket::SGameSocket(void)
{
}
SGameSocket::~SGameSocket(void)
{
}
int SGameSocket::clearSocketList(SOCKET socketId)
{
SGameSocketList_Str *str=findSocket(socketId,_headSgameSocket_str);
if(str){
SGameSocketList_Str* last=str->last;
last->next = str->next;
delete str;
str = NULL;
return 0;
}
return -1;
}
SGameSocketList_Str* SGameSocket::findSocket(int socketId,SGameSocketList_Str *str)
{
if( str && str->socketId == socketId )
{
return str;
}
if(str->next !=nullptr)
{
findSocket(socketId,str->next);
}
return nullptr;
}
void *SGameSocket::rec_data(void* serConn)
{
SOCKET *tmpcon = (SOCKET*)serConn;
char receiveBuf[ MSG_LEN ]={0};//接收
while( 1 ){
memset(receiveBuf,0, MSG_LEN);//重置
int buflen = recv(*tmpcon,receiveBuf,sizeof(receiveBuf),0);
if(buflen <= 0)
{
// 这里表示对端的socket已正常关闭
std::cout<<"buflen:%s\n"<<buflen<<
"\nsocket已正常关闭 " <<std::endl;
SOCKET socketId = *tmpcon;
//_instance->clearSocketList(socketId);
break;
}
else if(buflen>0)
{
/** MsgData_Str msgData;
memcpy( msgData.msgid,"100001", sizeof("100001") );
memcpy( msgData.msgType, "002", sizeof("002") );
memcpy( msgData.msg, "msg struct test", sizeof("msg struct test"));
send(*tmpcon, (char*)&msgData,sizeof(msgData),0);
**/
// CBuffer recvStr;
/// memcpy( &recvStr,receiveBuf,sizeof(receiveBuf) );
CByteArrays byt;
byt.setData( receiveBuf );
MsgHandle::GetInstance()->decodeMsg( byt ,*tmpcon);
}
}
return NULL;
}
int SGameSocket::creat( const char *ipstr ,short portValue )
{
//创建套接字
WORD myVersionRequest;
WSADATA wsaData;
myVersionRequest=MAKEWORD(1,1);
int err;
err=WSAStartup(myVersionRequest,&wsaData);
if (!err){
printf("已打开套接字\n");
}else{
printf("ERROR:嵌套字未打开!");
return 1;
}
//进一步绑定套接字
SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);//创建了可识别套接字
//需要绑定的参数
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr( ipstr );;//ip地址
addr.sin_port=htons( portValue );//绑定端口
int ret = bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成
listen(serSocket,NUM_THREADS);//其中第二个参数代表能够接收的最多的连接数
//////////////////////////////////////////////////////////////////////////
//开始进行监听
//////////////////////////////////////////////////////////////////////////
SOCKADDR_IN clientsocket;
int len=sizeof(SOCKADDR);
int tmp=0;
_headSgameSocket_str = new SGameSocketList_Str;
SGameSocketList_Str* _currentSgameSocket_str=_headSgameSocket_str;
while (1)
{
SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听
if(_currentSgameSocket_str->socketId)
{
SGameSocketList_Str* temp = new SGameSocketList_Str;
temp->last=nullptr;
temp->next=nullptr;
temp->socketId =0;
_currentSgameSocket_str->next = temp;
temp->last = _currentSgameSocket_str;
temp->current = tmp;
_currentSgameSocket_str = temp;
}
_currentSgameSocket_str->socketId = serConn;
pthread_t ntid;
if(( pthread_create(&ntid,NULL,rec_data, &serConn) ) != 0)
{
printf("creat pthread fialed");
break;
}
tmp++;
}
return 1;
}
int SGameSocket::sendMsg(int socketId,CByteArrays* byt)
{
send(socketId, byt->getData(),byt->size(),0);
return 0;
}
int SGameSocket::destroy()
{
WSACleanup();//释放资源的操作
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化