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
[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


Sign In
Create Account


Back to top









