I am trying to make a simple UDP socket class to talk between a C ++ program and a java program, so I am trying to create a socket class that handles all UDP transmission, but I cannot force the program to compile because I get about 8 lnk2019 errors, and I have no idea what they mean. I mainly work in Java and only use C ++ when I need lol. I have 3 files socket header and code. Also the Udp Socket code is from rFactor-Nesim, so the socket code is not written by me.
UdpSocket.cpp
#include "UdpSocket.hpp" #include <stdio.h> UdpSocket::UdpSocket(const char* host, int port) : mHost(host), mPort(port) { } UdpSocket::~UdpSocket(void) { } void UdpSocket::Open() { if(WSAStartup(MAKEWORD(2, 0), &mWinsockData) != 0) fprintf(stderr, "WSAStartup() failed"); if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) perror("socket() failed"); memset(&mSocketAddress, 0, sizeof(mSocketAddress)); mSocketAddress.sin_family = AF_INET; mSocketAddress.sin_addr.s_addr = inet_addr(mHost); mSocketAddress.sin_port = htons(mPort); } void UdpSocket::Close() { closesocket(mSocket); WSACleanup(); } void UdpSocket::Send(char* str, size_t length) { size_t result = sendto(mSocket, str, length, 0, (struct sockaddr *) &mSocketAddress, sizeof(mSocketAddress)); if(result != length) perror("sendto() sent incorrect number of bytes"); }
UdpSocket.hpp
#ifndef UDPSOCKET_HPP #define UDPSOCKET_HPP #include <WinSock.h> class UdpSocket { public: UdpSocket(const char* host, int port); ~UdpSocket(void); void Send(char* str, size_t length); void Open(); void Close(); private: const char* mHost; int mPort; int mSocket; struct sockaddr_in mSocketAddress; WSADATA mWinsockData; }; #endif // UDPSOCKET_HPP
and main
#include "Socket/UdpSocket.hpp" #include <iostream> int Main(){ UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469); testSocket->Open(); system("pause"); return 0; }
Any help would be great. I am not very strong with C ++, but I did a little work
Ouput Console:
Error 1 error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" ( ?Open@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 2 error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" ( ?Open@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 3 error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: void __thiscall UdpSocket::Open(void)" ( ?Open@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 4 error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: void __thiscall UdpSocket::Open(void)" ( ?Open@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 5 error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "public: void __thiscall UdpSocket::Close(void)" ( ?Close@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 6 error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: void __thiscall UdpSocket::Close(void)" ( ?Close@UdpSocket @@QAEXXZ) UdpSocket.obj SocketTest Error 7 error LNK2019: unresolved external symbol __imp__sendto@24 referenced in function "public: void __thiscall UdpSocket::Send(char *,unsigned int)" ( ?Send@UdpSocket @@ QAEXPADI@Z ) UdpSocket.obj SocketTest Error 8 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib SocketTest Error 9 fatal error LNK1120: 8 unresolved externals C:\Users\Brendan\Documents\Visual Studio 2008\Projects\SocketTest\Debug\SocketTest.exe SocketTest