struct regular regularinfo;bool Server::initServer(ZTcpSocket* socket)
{
server.bind("172.11.2.50",1111);
server.listen(5);
if(server.accept(socket))
        {
                return true;
        }
}
bool simfun::initsim(ZTcpSocket* socket)
{   

while(1)
{   

if(socket->readable(1000) == 0)
continue;
int ret=socket->read(buff,sizeof(regular));
if (ret<=0)
{
continue; }
memcpy(&regularinfo,buff,sizeof(regularinfo));
if (((regularinfo.BEG==beg)&&(regularinfo.END==end))&&((regularinfo.CODE==C_RCU_NA_3)||(regularinfo.CODE==C_RFB_NA_3)))
{   
if (regularinfo.CODE==C_RCU_NA_3)
{
pd=0;
cout<<"pd=0";
}
else
{
pd=1;
cout<<"pd=1";
}
memset(buff,0,sizeof(buff));
regularinfo.CODE=M_CON_NA_3;
memcpy(buff,&regularinfo,sizeof(regularinfo));
ret=socket->write(buff,sizeof(regularinfo));
cout<<"已发送"<<ret<<endl;
break;
}
memset(buff,0,sizeof(buff)); }
memset(buff,0,sizeof(buff));
return true;

}bool simfun::initsimu(ZTcpSocket* socket)
{
if (!initsim(socket))
{
cout<<"初始化过程-主站通信单元发生错误";
}
cout<<"初始化过程-主站通信单元成功"<<endl;
while(1)
{   
此处标红=》int ret1=socket->read(buff,sizeof(regular));
cout<<ret1<<endl;
if (ret1<=0)
{   
switch (WSAGetLastError())
{
case WSANOTINITIALISED: printf("not initialized\n"); break;
case WSASYSNOTREADY: printf("sub sys not ready\n"); break;
case WSAHOST_NOT_FOUND: printf("name server not found\n");  break;
case WSATRY_AGAIN:  printf("server fail\n");  break;
case WSANO_RECOVERY:  printf("no recovery\n");   break;
case WSAEINPROGRESS:  printf("socket blocked by other prog\n"); break;
case WSANO_DATA:   printf("no data record\n");  break;
case WSAEINTR:   printf("blocking call canciled\n");  break;
case WSAEPROCLIM: printf("limit exceeded\n");
case WSAEFAULT:  printf("lpWSAData in startup not valid\n");
default: printf("unknown error id = %d\n",WSAGetLastError()); break;
};
continue;
}
       
           ....................省略
        }
main.cpp
void main()
{
Server s;
simfun sim;
sim.socket = new ZTcpSocket;
s.initServer(sim.socket);
sim.initsimu(sim.socket);
........
       省略



}小弟不才 写了个模拟器 在标红的地方读不到东西了 返回值是-1 getlasterror返回的是10038
各路大神新手求解 只是写了个很简单的单连接 为什么socket就断了呢 

解决方案 »

  1.   

    补充一下 前面都是收发正常 socket可用 到了标红处 就10038了
      

  2.   

    你这就是个无效的socket,没有accept,也没有connect,能recv的话,你才是大神
      

  3.   

    你这就是个无效的socket,没有accept,也没有connect,能recv到数据话,你才是大神
      

  4.   

    面向连接的Socket Server的简单实现
      

  5.   

    accept要写while循环里面
      

  6.   

    WSAENOTSOCK 
    (10038) 
    Socket operation on non-socket. 
    An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.