Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Cant connect to server with d c++


ghost's Avatar
0 0

I've never did much c++ programing before, so i am trying to setup a socket connection with a server, so i can send data to it (chat server)

this is what I'm trying, but i keep getting these errors, it just relates to the networking part:

ERRORS:

[Linker error] undefined reference to `WSAStartup@8' 
[Linker error] undefined reference to `socket@12'
[Linker error] undefined reference to `gethostbyname@4' 
[Linker error] undefined reference to `htons@4'
[Linker error] undefined reference to `connect@12'   
ld returned 1 exit status 

CODE TRIED:

     WSADATA wsaData;
      SOCKET hSocket;
      SOCKADDR_IN sIn;
      LPHOSTENT serverName;

      char sndBuffer[256];
      char recvBuffer[256];
  

     WSAStartup(MAKEWORD(2, 2), &wsaData);

    hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    serverName = gethostbyname("www.spinchat.com");

     sIn.sin_family = AF_INET;
     sIn.sin_addr = *((LPIN_ADDR)*serverName->h_addr_list);
     sIn.sin_port = htons(3001); 

     if(connect(hSocket, (sockaddr*)&sIn, sizeof(struct sockaddr)) == SOCKET_ERROR)
     cout << "Error: Connection failed." << endl;
        
        // connected

Please help…:(


ghost's Avatar
0 0

im using dev - c++ latest version, by the way


ghost's Avatar
0 0

simple problem if i am right. You ahve to link the winsock lib to ur prog. This can be done by going into the options menu and finding the link option(or the equivilent on your compiler check your manual) the file should look something like this:winsock.lib

I hope that helped :)


ghost's Avatar
0 0

z3ro is partly right, you can either edit your linker options and include ws2_32.lib or just add #pragma comment(lib, "ws2_32.lib") to your code.

Cheers, ~T


ghost's Avatar
0 0

Thanks for your helpful replies, I'm sure what you said is right, but I'm not there yet. I added the #pragma comment inclusion, then the same error cam up again, lol. I did what Zero said and went to the linker options, there was one option:

Link an objective program: NO

I then changed it to YES, tehn a weird error came up when I compiled:

Cannot find -lobjc
ID returned exit status 1

At least all those errors have gone, but now it says it can't find the -lobjc, whatever that is. :|


ghost's Avatar
0 0

No idea what you're trying to do.. What compiler are you using?

Cheers, ~T


ghost's Avatar
0 0

dusing the default compiler with dev- c++ (v. 4.9.9.2)