📄 谁有c++的多线程编程资料,源代码?? c++ builder - 网络及通讯开发 - 社区 community_csdn_net.txt
字号:
谁有c++的多线程编程资料,源代码?? C++ Builder / 网络及通讯开发 - 社区 community.csdn.net谁有c++的多线程编程资料,源代码??楼主sensing(想到韩国去工作+留学!)2002-10-30
11:35:03 在 C++ Builder / 网络及通讯开发 提问
问题点数:100、回复次数:10
Top
1 楼warton(新群:软件创业13734424)回复于 2002-10-30 12:07:56 得分 80看我的代码,socket api,多线程:
#define WSVERS MAKEWORD(2, 0)
#define STKSIZE 16536
#include <stdio.h >
#include <winsock2.h >
#include <process.h >
int TCP_Echo(SOCKET,struct sockaddr_in);
int main()
{
struct sockaddr_in fsin, sin;
SOCKET msock, ssock;
struct hostent *hostname;
unsigned short port = 10000;
int alen;
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( "192.168.4.142 "); //
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));
if (_beginthread(TCP_Echo, STKSIZE,
(void*)ssock) <0)
{
printf( "启动新线程失败!\n
");
return -1;
}
}
}
//线程要调用的函数:处理客户发送来的数据
int TCP_Echo(SOCKET fd,struct sockaddr_in sin)
{
char buf[4096];
int cc = 0;
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 -1;
}
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 0;
}
Top
2 楼warton(新群:软件创业13734424)回复于 2002-10-30 12:10:19 得分 20关键部分:
开始线程
if (_beginthread(TCP_Echo, STKSIZE, (void*)ssock) <0)
{
printf( "启动新线程失败!\n
");
return -1;
}
_endthread();
//
return 0;
break;
Top
3 楼cuilin2002(青出于蓝而胜于蓝)回复于 2002-10-30 13:36:25 得分 0 创建线程可以用
CreateThread(NULL,0,TCP_Echo,(LPVOID)ssock,0,&dwThreadId)
Top
4 楼hotxu(hotxu)回复于 2002-11-13 18:04:54 得分 0 pub.chinafsdu.net user:pub
password:pub
win32多线程程序设计配套光盘.rar
Top
5 楼flowercity(菜农)回复于 2002-11-15 12:01:48 得分 0
//******************线程单元文件××××××××××××××××××××××××××××
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "main.h"
//包含主窗体
#pragma package(smart_init)
extern HANDLE hComm;
//主窗体创建的HANDLE
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL
can only be
// used in a method called using Synchronize, for
example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Unit1::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TRead232::TRead232(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TRead232::Execute()
{
//---- Place thread code here ----
while(!Terminated) //只要线程不中止,就执行ReadData()函数
Synchronize(ReadData);
}
//---------------------------------------------------------------------------
void __fastcall TRead232::ReadData()
{
String Temp;
char inbuff[1024]; // 缓存区大小
DWORD nBytesRead,dwEvent,dwError;
COMSTAT cs;
if(hComm==INVALID_HANDLE_VALUE) //端口是否打开
return;
ClearCommError(hComm,&dwError,&cs); //端口状态
if(cs.cbInQue>sizeof(inbuff))
{
PurgeComm(hComm,PURGE_RXCLEAR);
return;
}
ReadFile(hComm,inbuff,cs.cbInQue,&nBytesRead,NULL); // 读取数据
inbuff[cs.cbInQue]='\0';
mainForm->Memo2->Text=mainForm->Memo2->Text+inbuff;
}
//---------------------------------------------------------------------------
//***************************线程头文件×××××××××××××××××××××××××××××××××××
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TRead232 : public TThread
{
private:
void __fastcall TRead232::ReadData(); //定义一个读取数据的函数
protected:
void __fastcall Execute();
public:
__fastcall TRead232(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
//***********************************主窗体调用线程××××××××××××××××××××××××××××××××
//---------------------------------------------------------------------------
//××××××××××××××××××××××××××××××××××××头文件
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TmainForm : public TForm
{
__published: // IDE-managed Components
TRadioGroup *RadioGroup1;
TButton *Button1;
TButton *Button2;
TLabel *Label1;
TLabel *Label2;
TMemo *Memo1;
TMemo *Memo2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Memo1KeyPress(TObject *Sender, char
&Key);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
TThread *Read232; //声名一个TReadThread类型的对象
public: // User declarations
__fastcall TmainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TmainForm *mainForm;
//---------------------------------------------------------------------------
#endif
//××××××××××××××××××××××××××××××××单元文件××××××××××××××××××××××××××
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h" //包含线程文件
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TmainForm *mainForm;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -