刚学windows网络编程,用vs2005编译不到代码,请高手进来帮忙解决!
#include <winsock2.h>
#include <stdio.h>
void main(void)
{
WSADATA wsaData;
SOCKET ReceivingSocket;
SOCKADDR_IN ReceiverAddr;
int Port = 5150;
char ReceiveBuf[1024];
int BufLength = 1024;
SOCKADDR_IN SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int Ret;
// Initialize Winsock version 2.2
if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf("ERROR: WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to receive datagrams on.
if ((ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
== INVALID_SOCKET)
{
printf("ERROR: socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}
// Setup a SOCKADDR_IN structure that will tell bind that we
// want to receive datagrams from all interfaces using port
// 5150.
ReceiverAddr.sin_family = AF_INET;
ReceiverAddr.sin_port = htons(Port);
ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Associate the address information with the socket using bind.
if (bind(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr))
== SOCKET_ERROR)
{
printf("ERROR: bind failed with error %d\n", WSAGetLastError());
closesocket(ReceivingSocket);
WSACleanup();
return;
}
printf("We are ready to receive 1 datagram from any interface on port %d...\n",
Port);
// At this point you can receive datagrams on your bound socket.
if ((Ret = recvfrom(ReceivingSocket, ReceiveBuf, BufLength, 0,
(SOCKADDR *)&SenderAddr, &SenderAddrSize)) == SOCKET_ERROR)
{
printf("ERROR: recvfrom failed with error %d\n", WSAGetLastError());
closesocket(ReceivingSocket);
WSACleanup();
return;
}
printf("We successfully received %d bytes from address %s:%d.\n", Ret,
inet_ntoa(SenderAddr.sin_addr), ntohs(SenderAddr.sin_port));
// When your application is finished receiving datagrams close
// the socket.
closesocket(ReceivingSocket);
// When your application is finished call WSACleanup.
WSACleanup();
}
编译上面代码时,便出现以下错误!
Compiling...
tcp.cpp
Compiling manifest to resources...
Linking...
tcp.obj : error LNK2019: unresolved external symbol __imp__inet_ntoa@4 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__ntohs@4 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__recvfrom@24 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function _main
tcp.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main
C:\Documents and Settings\桌面\新建文件夹\tep\Debug\tep.exe : fatal error LNK1120: 11 unresolved externals
请大家帮帮小弟!!!