Jump to content

C++ winsock problem

- - - - -

  • Please log in to reply
7 replies to this topic

#1
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Hi there,

I am trying to make a small program that will just connect to a port on a server. The thng is I am getting some linker errors and I have no clue how to make it work (I am noob to C and C++).

I am using Dev c++

Here is the code:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <winsock.h>


using namespace std;
int TryConnect(long hostname, int PortNo);
SOCKET s;

int main(int argc, char *argv[])
{
    TryConnect(inet_addr("192.168.2.2"), 80);
    system("PAUSE");
    return EXIT_SUCCESS;
}

int TryConnect(long hostname, int PortNo)
{
    cout << "Begin\n";
    WSADATA w; //Winsock startup info
    SOCKADDR_IN target; //Information about host
    
    int error = WSAStartup (0x0202, &w);   // Fill in WSA info
     
    if (error)
    { // there was an error
    cout << "Error\n";
      return 0;
    }
    if (w.wVersion != 0x0202)
    { // wrong WinSock version!
    cout << "wrong WinSock version!\n";
      WSACleanup (); // unload ws2_32.dll
      return 0;
    }
    
    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
    if (s == INVALID_SOCKET)
    {
          cout << "Error\n";
        return 0;
    }

    target.sin_family = AF_INET;           // address family Internet
    target.sin_port = htons (PortNo);        // set server’s port number
    target.sin_addr.s_addr = hostname;  // set server’s IP
     cout << "Connectiong\n";
    //Try connecting...
    if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) //Try binding
    { // error
    cout << "Error\n";
          return 0;
    }
    //WSAAsyncSelect (s, hwnd, 1045, FD_READ | FD_CONNECT | FD_CLOSE); 
        //Switch to Non-Blocking mode
        
    //SendMessage(hStatus, WM_SETTEXT, 0, (LPARAM)"Connected to Remote Host.");
    cout << "End\n";
    return 1; //OK
}
And here are the errors:

Quote

[Linker error] undefined reference to `inet_addr@4'
[Linker error] undefined reference to `WSAStartup@8'
[Linker error] undefined reference to `WSACleanup@0'
[Linker error] undefined reference to `socket@12'
[Linker error] undefined reference to `htons@4'
[Linker error] undefined reference to `connect@12'
ld returned 1 exit status
C:\Users\sakishrist\Desktop\network test\Makefile.win [Build Error] [Project1.exe] Error 1
Thanks in advance!

#2
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
You need to add the winsock library to the project. It's called winsock.lib i think, it's either that or wsock32.lib somthing like that.

#3
setjmp

setjmp

    Newbie

  • Members
  • PipPip
  • 21 posts
You need add ws2_32.lib. To add: #pragma comment(lib, "ws2_32.lib")

#4
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts

Quote

You need add ws2_32.lib. To add: #pragma comment(lib, "ws2_32.lib")
Thanks but that didn't work for me. I just put it under these lines:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <winsock.h>
What should I do?

#5
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts

abzero said:

You need to add the winsock library to the project. It's called winsock.lib i think, it's either that or wsock32.lib somthing like that.
Could you please tell me how I should do that?

Thanks in advance

#6
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
As I don't have Dev-C++ I can't tell you how to do it. You need to add the library to the project or linker settings; or the makefile if that's what is uses. Your best bet to google for "link to external library Dev-C++" which might yield some clues.

#7
sakishrist

sakishrist

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Icould not find either winsock.lib or wsock32.lib. I found libwsock32.a in my lib directory but I don't think that's the one you are talking about.

I just found out that an other project I have (one downloaded from the internet) has this line in the linker:

Quote

c:/Dev-Cpp/lib/libws2_32.a
What does this do and what does the one posted earlier do?

#8
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
libwsock32.a is probably the one you want. It's just named differently. I believe there are two versions of the library verison 2 which is named ws2_32 and the old version which would be wsock32. Try one then there other to see if it works.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users