请教BCB中BeginThread的用法,最好举例

fromv 2003-06-28 03:29:49
BCB中的帮助太少了,看了还是不明白。那位帮忙?
...全文
253 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
perl2003 2003-06-28
  • 打赏
  • 举报
回复
继承TThread
pp616 2003-06-28
  • 打赏
  • 举报
回复
//别人的代码。忘了到底是谁的了。你可以参考一下。

#include <vcl.h>
#include <dstring.h>
#include <stdio.h>
#include <winsock.h>
#include <process.h>

void TCP_Echo(void *pfd);


int main()
{
struct sockaddr_in fsin, sin;
SOCKET msock, ssock;
struct hostent *hostname;
unsigned short port = 10914;
int alen;
char string[25];


WSADATA wsadata;


if (WSAStartup(WSVERS, &wsadata) != 0)
{
printf( "调用winsock.dll失败! ");
return -1;
}

memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr =inet_addr(""); //
sin.sin_port = htons(port);

msock = socket(PF_INET, SOCK_STREAM, 0);
if (msock == INVALID_SOCKET)
{
printf( "create socket error\n ");
return -1;
}

if (bind(msock, (struct sockaddr *)&sin, sizeof(sin))==SOCKET_ERROR)
{
printf( "bind error\n ");
return -1;
}

if (listen(msock, 5)==SOCKET_ERROR)
{
printf( "listen error\n ");
return -1;
}
//hostname=gethostbyaddr(inet_ntoa(sin.sin_addr),8,0);
//printf( "%s\n ",hostname- >h_name);
printf( "本地地址:%s\n ",inet_ntoa(sin.sin_addr));//
printf( "服务器监听端口:%d\n ",ntohs(sin.sin_port));


while(1)
{
alen = sizeof(struct sockaddr);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock == INVALID_SOCKET)
{
printf( "accept error\n ");
return -1;
}

printf( "有客户连接自 %s\r\n ", inet_ntoa(fsin.sin_addr));
memset(string,0x00,25);
itoa(fsin.sin_port, string, 10);
printf( "有客户连接自 %s\r\n ",string );

if(_beginthread(TCP_Echo, STKSIZE, (void *)&ssock)<0)
{
printf( "启动新线程失败!\n ");
return -1;
}
}
}
//线程要调用的函数:处理客户发送来的数据
void TCP_Echo(void *pfd)
{
char buf[4096];
int cc = 0;
SOCKET *fd=(SOCKET *)pfd;
memset(buf, 0, sizeof(buf));
strcpy(buf, "Enter 'Q' to exit\r\n ");
if (send(*fd, buf, strlen(buf), 0)==SOCKET_ERROR)
{
printf( "echo send error\n ");
return ;
}

memset(buf, 0, sizeof(buf));
cc = recv(*fd, buf, sizeof(buf), 0);
printf( "客户发送的数据:%s\n ", buf);

while(cc!=SOCKET_ERROR && cc > 0)
{
if (send(*fd, buf, cc, 0)==SOCKET_ERROR)
{
printf( "echo send error\n ");
break;
}
/* if (send(fd, "\r\n ", 2, 0)==SOCKET_ERROR)
{
printf( "echo send error\n ");
break;
}*/
memset(buf, 0, sizeof(buf));
cc = recv(*fd, buf, sizeof(buf), 0);
printf( "客户发送的数据:%s\n ", buf);
if (buf[0]=='Q')
{

_endthread();
// return 0;
break;
}
}
if (cc==SOCKET_ERROR)
printf( "echo recv error\n ");

closesocket(*fd);
return ;
}
albeta 2003-06-28
  • 打赏
  • 举报
回复
从Tthread继承一个子类,就可以了,难道要这么复杂么?
HUANG_JH 2003-06-28
  • 打赏
  • 举报
回复
typedef unsigned ( WINAPI *PBEGINTHREADEX_THREADFUNC )( LPVOID lpThreadparameter ) ;
typedef unsigned *PBEGINTHREADEX_THREADID;
bool m_bDisplayThreadTerminate;
DWORD dwDisplayThreadId ;
HANDLE m_hDisplayThreadHandle;

dwDisplayThreadId = 0;


m_hDisplayThreadHandle = (HANDLE)_beginthreadex(NULL, 0, ( PBEGINTHREADEX_THREADFUNC )DisplayRoutine, ( LPVOID )this, 0, (PBEGINTHREADEX_THREADID)&dwDisplayThreadId );
if(m_hDisplayThreadHandle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("无法建立接收数据线程!", "警告", MB_OK | MB_ICONWARNING);
return ;
}
yuanhen 2003-06-28
  • 打赏
  • 举报
回复

BeginThread和API CreateThread差不多!

yuanhen 2003-06-28
  • 打赏
  • 举报
回复

为什么不用TThread。
yydy 2003-06-28
  • 打赏
  • 举报
回复
学习

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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