查看: 2162|回复: 1

windows 网络编程 注意点

[复制链接]

该用户从未签到

发表于 2008-6-6 16:24:46 | 显示全部楼层 |阅读模式
分享到:
server1View.cpp
Linking...
server1View.obj : error LNK2001: unresolved external symbol __imp__listen@8
server1View.obj : error LNK2001: unresolved external symbol __imp__bind@12
server1View.obj : error LNK2001: unresolved external symbol __imp__socket@12
server1View.obj : error LNK2001: unresolved external symbol __imp__htons@4
server1View.obj : error LNK2001: unresolved external symbol __imp__htonl@4
Debug/server1.exe : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.
Creating browse info file...

server1.exe - 6 error(s), 0 warning(s)


用vc6 编控制台程序的时候 当涉及到网络方面,譬如http或socket操作,这个时候你发现编译不通过类似上面的错误,其实很简单,缺了一个库文件

有俩种方法可以解决这个问题
1.在project->setting中,打开link选项卡,加上ws2_32.lib
2.在文件中加上#progmma comment(lib, "ws2_32.lib")


下面是我在网络上看到的一些源码:

因为要测试一个GPRS的UDP上传功能,于是在Windows下写了这个UDP测试小程序.

Server 端程序源代码:
#include
#include

void main()
{
SOCKET socket1;

//InitWinsock();
WSADATA wsaData;
int iErrorCode;
if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL
{
printf("Winsock无法初始化!\n");
WSACleanup();
return;
}

printf("服务器开始创建SOCKET。\n");
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(27015); ///监听端口
local.sin_addr.s_addr=INADDR_ANY; ///本机

socket1=socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof(local));
while (1)
{
char buffer[1024]="\0";
printf("waiting for message from others-------------\n");
if (recvfrom(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
{
printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
////给cilent发信息
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&from,fromlen);

}
//Sleep(1000);
}
closesocket(socket1);
}



Client端源代码:



#include
#include

void main()
{
SOCKET socket1;
//InitWinsock();
WSADATA wsaData;
int iErrorCode;
if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL
{
printf("Winsock无法初始化!\n");
WSACleanup();
return;
}

printf("服务器开始创建SOCKET。\n");
struct sockaddr_in server;
int len =sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(27015); ///server的监听端口
server.sin_addr.s_addr=inet_addr("221.223.16.32"); ///server的地址

socket1=socket(AF_INET,SOCK_DGRAM,0);
while (1)
{
char buffer[1024]="\0";
printf("input message\n");
scanf("%s",buffer);
// printf("%s",buffer);
if (strcmp(buffer,"bye")==0)
{
printf("退出!\n");
Sleep(100);
closesocket(socket1);
break;
}

if (sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&server,len)!=SOCKET_ERROR)
{
//printf("sending..\n");
Sleep(100);
if (recvfrom(socket1,buffer,sizeof(buffer),0,(struct sockaddr*)&server,&len) != SOCKET_ERROR)
printf("rece from server:%s\n",buffer);
}
}
closesocket(socket1);
}
回复

使用道具 举报

该用户从未签到

发表于 2008-8-13 11:28:50 | 显示全部楼层

RE:windows 网络编程 注意点

运行下试试
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条



手机版|小黑屋|与非网

GMT+8, 2024-11-15 23:03 , Processed in 0.118529 second(s), 17 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.