大家帮我看看这段代码啊,怎么通不过啊在VC6中?小弟我刚学SOCKET编程,在线等

xiewenjun1111 2003-10-16 02:48:48
// Server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "afxwin.h"
#include "process.h"
#include "winsock.h"
#include "iostream.h"


#define MAX 50

//////////////////////////////////////

UINT thread(LPVOID p);
int getcount(void);
void sendtoall(SOCKET s, char *buff);

//////////////////////////////////////

SOCKET msgsock[MAX];
SOCKET sock;
struct sockaddr_in serv;
int addlen;

int main(int argc, char* argv[])
{


WSADATA wsadata;

cout<<"Initing...."<<endl;
WSAStartup(0x0101,&wsadata);

sock = socket(AF_INET,SOCK_STREAM,NULL);
if(sock == INVALID_SOCKET)
{
cout<<"Initialize Field"<<endl;
return 0;
}
cout<<"Initialize Succeed"<<endl;

serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_family = AF_INET;
serv.sin_port = htons(5000);
addlen = sizeof(serv);

if( bind(sock,(sockaddr *)&serv,addlen) != 0 )
{
cout<<"bind socket error!"<<endl;
return 0;
}

if( listen(sock,5) != 0 )
{
cout<<"listen socket error!"<<endl;
return 0;
}

_beginthread(&thread,0,0);
return 0;
}



UINT thread(LPVOID p)
{
int loop = 1;
int msgcount = getcount();
int s;
char buff[100];

if(msgcount == -1)
loop = 0;

if( loop )
{
s = 1;
msgsock[msgcount] = accept(sock,(sockaddr *)&serv,&addlen);
if ( msgsock[msgcount] == INVALID_SOCKET)
{
cout<<"accept socket error!"<<endl;
}
else
{
_beginthread(&thread,0,0);
cout<<"connect succeed!"<<endl;
while( s != SOCKET_ERROR )
{
s = recv(msgsock[msgcount],buff,100,0);
if( s != SOCKET_ERROR )
sendtoall(msgsock[msgcount],buff);
}
send(msgsock[msgcount],"Disconnected",100,0);
closesocket(msgsock[msgcount]);

msgsock[msgcount] = NULL;

}


}
_endthread();
return 0;
}

int getcount(void)
{
for(int i=0; i<MAX; i++)
{
if(msgsock[i] == NULL)
return i;
}
return -1;

}


void sendtoall(SOCKET s, char *buff)
{
for(int i=0; i<MAX; i++)
{
if(msgsock[i] != NULL && msgsock[i] != s)
send(msgsock[i],buff,100,0);
}
}




我已经加了 wsock32.lib MSVCRT.LIB LIBCMT.LIB
怎么通不过啊
错误信息是:
D:\vc\SERVER\Server.cpp(60) : error C2065: '_beginthread' : undeclared identifier
D:\vc\SERVER\Server.cpp(103) : error C2065: '_endthread' : undeclared identifier
...全文
78 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
inlinefunction 2003-10-16
  • 打赏
  • 举报
回复
project->setting->c\c++->categatory(code generation)->user run time libary(debug Multitheaded)
我试了可以啊!!
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
不行啊,大哥们,能不能把修改后的代码(包括工程设置)贴出来啊
w_flyer 2003-10-16
  • 打赏
  • 举报
回复
project->setting->c\c++->categatory(code generation)->user run time libary(Multitheath)
sevencat 2003-10-16
  • 打赏
  • 举报
回复
jpcl,跟我以前一样。

在工程设置里面改吧。(不是有些预定义符号吗什么win32啦。)
不然直接加到stdafx.h里面可能也有效。
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
我的工程是WIN32控制台应用程序,是单线程的??????
哪怎么改成多线程的啊?????????????
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
能不能试好贴出来啊,谢谢了,急啊
w_flyer 2003-10-16
  • 打赏
  • 举报
回复
你的工程用的肯定是单线程的运行库,你要修改你工程的属性
ndy_w 2003-10-16
  • 打赏
  • 举报
回复
预定义 _MT
#include <process.h>
sevencat 2003-10-16
  • 打赏
  • 举报
回复
process.h里面好像这这样的
#ifdef _MT
_beginthreadex(..)
_endthread(..)
#endif
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
谁帮我修改上面的代码后贴上来啊,谢谢(要运行通过啊)
辉歌 2003-10-16
  • 打赏
  • 举报
回复
建议看看《Windows环境下的多线程编程原理与应用》
sevencat 2003-10-16
  • 打赏
  • 举报
回复
_beginthread和_endthread属于标准的C函数,用起来会有很多问题,你最好是使用
AfxBeginThread
AfxendThread
这两个函数来替换
//=========================
这位就不要误导了。这两个函数是C库函数是没有问题的。另外两个是MFC的假如是MFC程序的话这个函数会将自己创建的线程通知theAPP,(好像是这样的)。

加上这句话吧。
#define _MT
#define _MT_
假如是VC2003直接改为多线程。
不信你看看#include <process.h>这个原文件。



辉歌 2003-10-16
  • 打赏
  • 举报
回复
_beginthread() 等等系列函数声明在process.h文件中,使用这些函数需要多线程版本的C运行时函数库,如链接libcmt.lib等。

AfxBegingTread 等是MFC包装后的线程系列函数。

CreateThread是Win32Api函数。

线程相关的 C标准 运行时库 和标准 C++库有所不同。
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
怎么CSDN上没有高手吗??????????????急啊55555555555555555555
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
用AfxBeginThread 、AfxendThread这两个函数
错误信息是;
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/Server.exe : fatal error LNK1120: 2 unresolved externals


LINK的时侯去掉了MSVCRT.LIB LIBCMT.LIB
代码如下:


// Server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "afxwin.h"
#include "process.h"
#include "winsock.h"
#include "iostream.h"


#define MAX 50

//////////////////////////////////////

UINT thread(LPVOID p);
int getcount(void);
void sendtoall(SOCKET s, char *buff);

//////////////////////////////////////

SOCKET msgsock[MAX];
SOCKET sock;
struct sockaddr_in serv;
int addlen;

int main(int argc, char* argv[])
{


WSADATA wsadata;

cout<<"Initing...."<<endl;
WSAStartup(0x0101,&wsadata);

sock = socket(AF_INET,SOCK_STREAM,NULL);
if(sock == INVALID_SOCKET)
{
cout<<"Initialize Field"<<endl;
return 0;
}
cout<<"Initialize Succeed"<<endl;

serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_family = AF_INET;
serv.sin_port = htons(5000);
addlen = sizeof(serv);

if( bind(sock,(sockaddr *)&serv,addlen) != 0 )
{
cout<<"bind socket error!"<<endl;
return 0;
}

if( listen(sock,5) != 0 )
{
cout<<"listen socket error!"<<endl;
return 0;
}

AfxBeginThread(&thread,0);
return 0;
}



UINT thread(LPVOID p)
{
int loop = 1;
int msgcount = getcount();
int s;
char buff[100];

if(msgcount == -1)
loop = 0;

if( loop )
{
s = 1;
msgsock[msgcount] = accept(sock,(sockaddr *)&serv,&addlen);
if ( msgsock[msgcount] == INVALID_SOCKET)
{
cout<<"accept socket error!"<<endl;
}
else
{
AfxBeginThread(&thread,0);
cout<<"connect succeed!"<<endl;
while( s != SOCKET_ERROR )
{
s = recv(msgsock[msgcount],buff,100,0);
if( s != SOCKET_ERROR )
sendtoall(msgsock[msgcount],buff);
}
send(msgsock[msgcount],"Disconnected",100,0);
closesocket(msgsock[msgcount]);

msgsock[msgcount] = NULL;

}


}
AfxEndThread(0);
return 0;
}

int getcount(void)
{
for(int i=0; i<MAX; i++)
{
if(msgsock[i] == NULL)
return i;
}
return -1;

}


void sendtoall(SOCKET s, char *buff)
{
for(int i=0; i<MAX; i++)
{
if(msgsock[i] != NULL && msgsock[i] != s)
send(msgsock[i],buff,100,0);
}
}
xiewenjun1111 2003-10-16
  • 打赏
  • 举报
回复
用AfxBeginThread 、AfxendThread这两个函数也不行,我试过了啊
mfc168 2003-10-16
  • 打赏
  • 举报
回复
#include <process.h>

_beginthread和_endthread属于标准的C函数,用起来会有很多问题,你最好是使用
AfxBeginThread
AfxendThread
这两个函数来替换

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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