Socket实现通信

1.创建socket套接字

structaddrinfohints, *res =NULL, *aip =NULL;

hints.ai_family=AF_UNSPEC;

if(bIsUDP)

{

hints.ai_socktype=SOCK_DGRAM;

hints.ai_protocol=IPPROTO_UDP;

}

else

{

hints.ai_socktype=SOCK_STREAM;

hints.ai_protocol=IPPROTO_TCP;

}

将ip转化为地址结构体

in_addr addr = {0};

intret =inet_pton(AF_INET, pHost, &addr);

//创建socket

if(ret >0) {

hSocket =socket(AF_INET, hints.ai_socktype, hints.ai_protocol);

if(-1== hSocket) {

returnhSocket;

}

//绑定服务器地址

sockaddr_inbind_addr = {0};

bind_addr.sin_family=AF_INET;

bind_addr.sin_addr= addr;

nRet=::bind(hSocket,(conststructsockaddr*)&bind_addr,sizeof(bind_addr));


//设置socket接受/发送参数

structlinger ling;

ling.l_linger=0;

ling.l_onoff=1;

nRel =setsockopt(hSocket,SOL_SOCKET,SO_LINGER, (char*)&ling,sizeof(ling));

//设置接受和发送buffer大小

intnRcvBufSize =512*1024;

nRel=setsockopt(hSocket,SOL_SOCKET,SO_RCVBUF,(char*)&nRcvBufSize,sizeof(nRcvBufSize));

intnSendBufSize =512*1024;

nRel=setsockopt(hSocket,SOL_SOCKET,SO_SNDBUF, (char*)&nSendBufSize,sizeof(nSendBufSize));

//设置socket是否是阻塞模式 设置0为非阻塞  1为阻塞

SetSocketBlock(hSocket,0);

//连接

sockaddr_inskAddr = {0};

skAddr.sin_family=AF_INET;

skAddr.sin_addr= addr;

skAddr.sin_port=htons(nPort);

nConnectRet=connect(hSocket, (sockaddr*)&skAddr,sizeof(skAddr));

//设置select超时等socket事件

fd_setfdwrite;

fd_setfdread;

fd_setfdexcept;

FD_ZERO(&fdwrite);

FD_ZERO(&fdread);

FD_ZERO(&fdexcept);

FD_SET(fd,&fdwrite);

FD_SET(fd,&fdread);

FD_SET(fd,&fdexcept);

__int64t1 =GetTickCount();

__int64t2 = nTimeOut *1000;

structtimevaltimeout;

timeout.tv_sec=0;

timeout.tv_usec=500*1000;

while(1)

{

intret =select(fd+1,&fdread,&fdwrite,&fdexcept,&timeout);

if(ret == -1)

{

return-3000002;

}

if(ret >0)

{

if(FD_ISSET(fd,&fdread))

{

interror;

socklen_tlen =sizeof(error);

ret =getsockopt(fd,SOL_SOCKET,SO_ERROR, &error, &len);

fLog(FC_LOG_Major,"CCSInf: socketCanReadWrite, fdread ret=%d", ret);

if(ret <0)

return-1;

return0;

}

elseif(FD_ISSET(fd,&fdwrite))

{

interror;

socklen_tlen =sizeof(error);

ret =getsockopt(fd,SOL_SOCKET,SO_ERROR, &error, &len);

if(ret <0)

return-1;

return0;

}

elseif(FD_ISSET(fd,&fdexcept))

{

fLog(FC_LOG_Major,"CCSInf: socketCanReadWrite, fdexcept ret=%d", -3000003);

return-3000003;

}

}

if((GetTickCount() - t1 > t2))break;

Sleep(10);

}


//ssl设置写

nHaveWrite=0;

while((nHaveWrite

{

if(m_sockfd!=-1)

{

/*设置select超时事件*/

}

else

{

return-2110001;

}

if(nErrorCode!=0)

{

close(m_sockfd);

m_sockfd=-1;

return-2110002;

}

char*pWritePos=pMsg+nHaveWrite;

intret = -1;

ret =SSL_write(m_pssl, (void*)pWritePos, nLen - nHaveWrite);

if(ret<=0)

{

interr =SSL_get_error(m_pssl,ret);

Reset();

return-2110003;

}

nHaveWrite+=ret;


}

//收数据

std::stringbuf(10240,0);

intnTotal=buf.size();

intnRecvTotal =0;

out.clear();

do

{

if(m_sockfd!=-1)

{

/*设置select超时事件*/

}

else

{

return-2110001;

}

if(nErrorCode!=0)

{

close(m_sockfd);

m_sockfd=-1;

return-2110002;

}

intret = -1;

ret =SSL_read(m_pssl, &buf[0], nTotal - nRecvTotal);

if(0== ret)break;

if(ret >0)

{

buf[ret] =0;

nRecvTotal += ret;

if(nRecvTotal >= nTotal) {

out.append(buf.c_str());

buf.clear();

buf.resize(10*1024);

nTotal += buf.size();

continue;

}

out.append(buf.begin(), buf.begin() + ret);

if(out.rfind("") !=std::string::npos)break;

}else{

}

}while((nRecvTotal < nTotal) && (bExit ==false));

你可能感兴趣的:(Socket实现通信)