简体   繁体   中英

bind(…) always returning -1 using WinSock2 — C++

I'm creating a game in C++ for a university project which requires some feature of networking using Sockets. My module lecturer gave us example code of working (local machine) client/servers to show us how it works. He has the following code for setting up the socket, which works fine:

#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")

#define SERVERIP     "127.0.0.1"
#define SERVERPORT   5555

void main(){
    WSADATA w;
    int error = WSAStartup(0x0202, &w);
    if (error != 0)
    {
        die("WSAStartup failed");
    }
    if (w.wVersion != 0x0202)
    {
        die("Wrong WinSock version");
    }

    // Create a TCP socket that we'll use to listen for connections.
    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSocket == INVALID_SOCKET)
    {
        die("socket failed");
    }

    // Fill out a sockaddr_in structure to describe the address we'll listen on.
    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr(SERVERIP);
    // htons converts the port number to network byte order (big-endian).
    serverAddr.sin_port = htons(SERVERPORT);

    // Bind the server socket to that address.
    if (bind(serverSocket, (const sockaddr *) &serverAddr, sizeof(serverAddr)) != 0)
    {
        die("bind failed");
    }
}

However, when I replicate most of the code, bind(...) function keeps returning -1 , as opposed to the 0 in my lecturers example. Here is the relevant part of my code, using classes:

TCPSocket.h

#define SERVERIP     "127.0.0.1"
#define SERVERPORT   5555

#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

class TCPSocket {
public:
    TCPSocket();
    ~TCPSocket();

    void SetupServer(char* serverIP_, int serverPort_, int messageSize_);

protected:

private:

    SOCKET m_socket;
    sockaddr_in m_serverAddress;

    char* m_serverIP;
    int m_serverPort;
    int m_messageSize;
};

TCPSocket.cpp

TCPSocket::TCPSocket() {
    // Initialise WinSock Library, version 2.2
    WSADATA w;
    int error = WSAStartup(0x0202, &w);
    if(error != 0) {
        //error
        int i = 0;
    }
    if(w.wVersion != 0x0202) {
        //error
        int i = 0;
    }
}

void TCPSocket::SetupServer(char* serverIP_, int serverPort_, int messageSize_) {
    m_serverIP = serverIP_;
    m_serverPort = serverPort_;
    m_messageSize = messageSize_;

    // Create TCP socket
    m_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(m_socket == INVALID_SOCKET) {
        //error
        int i = 0;
    }

    m_serverAddress.sin_family = AF_INET;
    m_serverAddress.sin_addr.s_addr = inet_addr(SERVERIP);
    m_serverAddress.sin_port = htons(SERVERPORT);       // htons: port -> network byte order (big-endian)

    // Bind server socket to address
    int bindex = bind(m_socket, (const sockaddr *) &m_serverAddress, sizeof(m_serverAddress));
    if(bindex != 0) {
        //error
        int a = 0;
    }

}

As I said in a comment, I made a silly mistake outwith the scope of the included code: basically I was running the SetupServer() function within an update loop and the error was being caught on the second iteration of the update. Besides that, the code is perfectly functional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM