我正在尝试制作一个简单的 UDP 套接字类来在 c++ 程序和 java 程序之间进行一些对话,所以我正在尝试制作一个处理所有 UDP 传输的套接字类,但我无法编译该程序,因为我得到大约 8 个 lnk2019 错误,我什至不知道它们是什么意思。我主要在 Java 中工作,只有在不得不大声笑的时候才使用 c++。我有 3 个文件的套接字头和代码。此外,Udp 套接字代码来自 rFactor-Nesim,因此套接字代码不是由我编写的。
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
和主要
#include "Socket/UdpSocket.hpp"
#include <iostream>
int Main(){
UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469);
testSocket->Open();
system("pause");
return 0;
}
任何帮助都会很棒。我对 C++ 不是很擅长,但我已经做了一点
控制台输出:
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