刚学windows网络编程,用vs2005编译不到代码,请高手进来帮忙解决!

hewittlee 2006-02-01 02:00:54
#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

请大家帮帮小弟!!!
...全文
291 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu_vane 2006-02-02
  • 打赏
  • 举报
回复
在Project的Properties中,Configuration Properties下的Linker下的Input中的Additional Dependencies中写上ws2_32.lib就行了。
melody110 2006-02-01
  • 打赏
  • 举报
回复
在编译选项里的linker里可以加上
hewittlee 2006-02-01
  • 打赏
  • 举报
回复
可不可以不写#pragma comment(lib, "WS2_32.lib")?有没有其他办法?
rigel2001 2006-02-01
  • 打赏
  • 举报
回复
#pragma comment(lib, "WS2_32.lib")
hewittlee 2006-02-01
  • 打赏
  • 举报
回复
怎样加入Ws2_32.lib?
OSPF_FAN 2006-02-01
  • 打赏
  • 举报
回复
明白否?
OSPF_FAN 2006-02-01
  • 打赏
  • 举报
回复
你没有连接Ws2_32.lib
在你的编译器环境中连接时加入Ws2_32.lib文件

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧