无法接受传入连接winsock2

时间:2015-03-17 12:08:24

标签: c++ winsock2

我的代码在控制台应用程序中运行良好。现在我尝试使用wxDev将其嵌入GUI。顺序是:

  1. virtual char* start()=0;
  2. virtual char* version()=0;
  3. virtual char* creat()=0;
  4. virtual char* bin()=0;
  5. virtual char* lis()=0;
  6. virtual char* acc()=0;
  7. 从0到4,所有功能都运行良好, 当acc()接受函数被调用时,它不会响应。我的代码:

    #include <winsock2.h>
    #include <winsock.h>
    int inn=100;
    char error[]={":::::I M HERE::::::"};
    char chaa[]={"Inside LIKE FUNCTION.\n"};
    char st[]={"Starting...\n"};
    char ve[]={"Compatable Verstion.\n"};
    char cr[]={"Socket Created.\n"};
    char bi[]={"Binding Done.\n"};
    char li[]={"Listeng..........\n"};
    char ac[]={"Connected..........\n"};
    int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
    SOCKET socket(int af, int type, int protocol);
    int connect(SOCKET s, const struct sockaddr *name, int namelen);
    int bind(SOCKET s, const struct sockaddr *name, int namelen);
    int listen(SOCKET s, int backlog);
    
    class Server 
    {
    public:
    SOCKET sockh;
    SOCKET socka;
    WSADATA wsadata;
    sockaddr_in sockAddr, client;
    public:
    
    char* like(){return chaa;}
    int kiss(){return inn++;}
    virtual char* start()=0;
    virtual char* version()=0;
    virtual char* creat()=0;
    virtual char* bin()=0;
    virtual char* lis()=0;
    //virtual char* acc()=0; 
    };
    
    class startup: public Server
    {
    public:
    char* start(){
    if(WSAStartup(MAKEWORD(2,0), &wsadata)==0)
        return st; 
    }
    };
    class ver:public startup{
    public:
        char* version(){
            if(LOBYTE(wsadata.wVersion)>=2)
                return ve;
            } 
    };
    class created: public ver   {
    public:
        char* creat(){
            if(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)!=INVALID_SOCKET)
            return cr;
            }
    };
    class binded: public created{
    public:
    // Bind socket to port 80
        char* bin()
        {
    //        sockaddr_in sockAddr;
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(7777);
    sockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    if (bind(sockh, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
            return bi;
            } 
    };
    class listenn: public binded
        {
      public:
        char* lis(){
            if(listen(sockh, SOMAXCONN)!=0)
                {
                         return li;                  
                            }
                }
     };
     class accep: public listenn{
    public:
    char* acc(){
    int siz=sizeof(client);
    socka=accept(sockh,reinterpret_cast<sockaddr*>(&sockAddr),&siz);
    if(socka!=INVALID_SOCKET)
    return ac;
    else return error;
    }
    };
    

    我也试过避免在同一个班级中返回陈述。

    SOCKET sockh;
    SOCKET socka;
    WSADATA wsadata;
    sockaddr_in sockAddr, client;
    
    if(WSAStartup(MAKEWORD(2,0), &wsadata)==0)
    {
     if(LOBYTE(wsadata.wVersion)>=2)
     {
      if(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)!=INVALID_SOCKET)
      {
    
    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(7777);
    sockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    if (bind(sockh, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)        
        {
               if(listen(sockh, SOMAXCONN)!=0)
                {
                    //waiting for incoming connection
                  int siz=sizeof(client);
                    socka=accept(sockh,reinterpret_cast<sockaddr*>     (&sockAddr),&siz);
                        if(socka!=INVALID_SOCKET)
                        {
                            return "Connected...";
                            }
                    }            
                  }
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您永远不会设置变量sockh。您应该将socket的返回值分配给它:

if((sockh = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)
   return cr;

此外,您无法正确检查返回值。 <{1}}和bind成功返回listen

修改

因为它似乎难以理解,所以整个代码按预期工作:

0

顺便说一句,如果人们调查他们给予帮助的时间,应该尝试遵循他们的建议并接受所要求的帮助。