Reputation: 18068
I have installed Visual Studio 2013 Ultimate. I tried this code to send anything by UDP(it's 15th or maybe latter code I try) and I get a bunch of weird errors. What do I lack in the project?
#include "stdafx.h"
#include <iostream>
#include <winsock2.h>
using namespace std;
int main()
{
//init
int server_length;
int port = 123;
const int STRLEN = 256;
char recMessage[STRLEN];
char sendMessage[STRLEN];
char *sendMes = "SERVER READY";
WSADATA wsaData;
SOCKET mySocket;
SOCKET myBackup;
SOCKET acceptSocket;
sockaddr_in myAddress;
//create socket
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
{
cerr << "Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}
mySocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mySocket == INVALID_SOCKET)
{
cerr << "Socket Initialization: Error creating socket" << endl;
system("pause");
WSACleanup();
exit(11);
}
myBackup = mySocket;
//bind
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr("0.0.0.0");
myAddress.sin_port = htons(port);
if (bind(mySocket, (SOCKADDR*)&myAddress, sizeof(myAddress)) == SOCKET_ERROR)
{
cerr << "ServerSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(14);
}
cout << endl;
while (1)
{
server_length = sizeof(struct sockaddr_in);
recvfrom(mySocket, recMessage, STRLEN, 0, (SOCKADDR*)&myAddress, &server_length);
cout << recMessage << endl;
sendto(mySocket, sendMes, strlen(sendMes), 0, (SOCKADDR*)&myAddress, server_length);
}
return 0;
}
Error 11 error LNK1120: 8 unresolved externals c:\users\r\documents\visual studio 2013\Projects\ConsoleApplication17\Debug\ConsoleApplication17.exe 1 1 ConsoleApplication17
Error 3 error LNK2019: unresolved external symbol __imp__bind@12 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 4 error LNK2019: unresolved external symbol __imp__htons@4 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 5 error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 6 error LNK2019: unresolved external symbol __imp__recvfrom@24 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 7 error LNK2019: unresolved external symbol __imp__sendto@24 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 8 error LNK2019: unresolved external symbol __imp__socket@12 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 10 error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Error 9 error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main c:\Users\R\documents\visual studio 2013\Projects\ConsoleApplication17\ConsoleApplication17\ConsoleApplication17.obj ConsoleApplication17
Warning 2 warning C4101: 'acceptSocket' : unreferenced local variable c:\users\r\documents\visual studio 2013\projects\consoleapplication17\consoleapplication17\consoleapplication17.cpp 21 1 ConsoleApplication17
Warning 1 warning C4101: 'sendMessage' : unreferenced local variable c:\users\r\documents\visual studio 2013\projects\consoleapplication17\consoleapplication17\consoleapplication17.cpp 16 1 ConsoleApplication17
Upvotes: 0
Views: 2308
Reputation: 610
stdafx.h needs to be included before anything else or the winsock2.h and iostream includes will be ignored.
stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change.
Compatible compilers (for example, Visual C++ 6.0 and newer) will precompile this file to reduce overall compile times. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
(http://en.wikipedia.org/wiki/Precompiled_header)
EDIT: You need to include the library for winsock called Ws2_32.lib, you can do so by adding #pragma comment(lib, "Ws2_32.lib"). You can add libraries in the IDE if you want it to work on multiple IDEs.
(http://msdn.microsoft.com/en-us/library/windows/desktop/ms737629%28v=vs.85%29.aspx)
Upvotes: 3