标题:急!!!!! winSock服务器程序
只看楼主
酷儿
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-10-15
 问题点数:0 回复次数:6 
急!!!!! winSock服务器程序
#include<PROCESS.H>
#include<windows.h>
#include<winsock.h>
#include<sys/types.h>
#include<fcntl.h>
#include<wsipx.h>
#include<stdio.h>

#define SERV_TCP_PORT 90
#define SERV_HOST_ADDR "10.60.46.2"

int sockfd;


DWORD ClientThread(void *pVoid)
{
 int nRet;
 char szBuf[1024];
 
 memset(szBuf,0,sizeof(szBuf));     /*将szBuf全部清零*/

 nRet=recv((SOCKET)pVoid,szBuf,sizeof(szBuf),0);

 if(nRet == INVALID_SOCKET){
   printf("recv()");
   closesocket(sockfd);
   closesocket((SOCKET)pVoid);
   return 0;
 }
 printf("\nData received:%\n",szBuf);
 strcpy(szBuf,"From the Server");
 nRet=send((SOCKET)pVoid,szBuf,strlen(szBuf),0);
 closesocket((SOCKET)pVoid);
 return 0;
}

int main()
{
 int clilen;
 int pHandle=-1;
 struct sockaddr_in serv_addr;
 SOCKET       socketClient;
 DWORD        ThreadAddr;
 HANDLE       dwClientThread;
 SOCKADDR_IN  SockAddr;

 WORD    wVersionRequested = MAKEWORD(1,1);
 WSADATA wsaData;
 if(WSAStartup(wVersionRequested,&wsaData)){
   printf("WSAStartup failed %s\n", WSAGetLastError());
   return -1;
 }
 
 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
   printf("server:can't open stream socker\n");
 
 memset((char *)&serv_addr,0,sizeof(struct sockaddr_in));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 serv_addr.sin_port=htons(SERV_TCP_PORT);

 if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
   printf("server:can't bind local address");
 listen(sockfd,5);
 
 while(1){
      clilen = sizeof(SOCKADDR_IN);
      socketClient = accept(sockfd,(LPSOCKADDR)&SockAddr,&clilen);
      if(socketClient == INVALID_SOCKET){
        printf("accpet faild!\n");
        break;
      }
      printf("Connection accepted on socket:%d from:%s\n",socketClient,inet_ntoa(SockAddr.sin_addr));
      dwClientThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&ClientThread,(void *)socketClient,0,&ThreadAddr);
 
      if(!dwClientThread)
        printf("Cannot start client thread..");

      CloseHandle((HANDLE)dwClientThread);
 }
 WSACleanup();
 return 0;
}



这是一个服务器程序我在编译运行时出现下面的错误,请问是什么原因? 初学网络编程请各位高手帮忙!!!!!!!!!!!!

Compiling...
winsock.cpp
D:\vc\MSDev98\MyProjects\winsock\winsock.cpp(10) : warning C4005: 'SERV_HOST_ADDR' : macro redefinition
        d:\vc\msdev98\myprojects\winsock\winsock.cpp(0) : see previous definition of 'SERV_HOST_ADDR'
Linking...
winsock.obj : error LNK2001: unresolved external symbol _send@16
winsock.obj : error LNK2001: unresolved external symbol _closesocket@4
winsock.obj : error LNK2001: unresolved external symbol _recv@16
winsock.obj : error LNK2001: unresolved external symbol _WSACleanup@0
winsock.obj : error LNK2001: unresolved external symbol _inet_ntoa@4
winsock.obj : error LNK2001: unresolved external symbol _accept@12
winsock.obj : error LNK2001: unresolved external symbol _listen@8
winsock.obj : error LNK2001: unresolved external symbol _bind@12
winsock.obj : error LNK2001: unresolved external symbol _htons@4
winsock.obj : error LNK2001: unresolved external symbol _htonl@4
winsock.obj : error LNK2001: unresolved external symbol _socket@12
winsock.obj : error LNK2001: unresolved external symbol _WSAGetLastError@0
winsock.obj : error LNK2001: unresolved external symbol _WSAStartup@8
Debug/winsock.exe : fatal error LNK1120: 13 unresolved externals
执行 link.exe 时出错.

winsock.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: 服务器 winSock 程序 
2008-05-08 20:30
酷儿
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-10-15
得分:0 
各位帮下忙啊!!
2008-05-08 20:40
lixuanalex
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2008-4-23
得分:0 
你的工程设置里面没有加上WS2_32.LIB文件。
在project菜单里选择set项打开project setting对话框,选择link选项卡,在对象/模块库中添加“WS2_32.LIB”,注意添加时要用空格格开两个文件名。
2008-05-09 08:42
酷儿
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-10-15
得分:0 
太感谢了!!!
能给我具体讲一下那是做什么的吗?
2008-05-09 19:39
lixuanalex
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2008-4-23
得分:0 
一个静态库文件
封装里套接字所用的原语和相关操作。我只知道这些。其他的你可以从网上查查!
2008-05-09 22:52
酷儿
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-10-15
得分:0 
谢了
2008-05-10 11:38
niejianfeng
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-5-26
得分:0 
#pragma comment(lib,"ws2_32.lib")
2008-05-26 03:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-213222-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.205381 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved