socket组件数据收发问题

zhyuanbaron 2004-05-07 07:46:08
我用Timer组件编一个定时发送数据的程序,在每次服务器端接收之后回传相同的数据,但是客户端只能读到第一个回传数据,其他的都不经过read函数,这是为什么阿?
是Client还是Server的事啊???
...全文
77 12 点赞 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
庄鱼 2004-05-14
很想帮忙,可是缺乏环境,只能从语法上分析了。
void __fastcall TTransForm::ServerSocketClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
if(IsServer)
{
ServerSocket->Socket->Connections[0]->SendText(s1);
}
}
//----------- 上述函数你想做什么?
void __fastcall TTransForm::ServerSocketClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//----------- 你是在制造错觉吗?
void __fastcall TTransForm::FormCreate(TObject *Sender)
{
MyList = new TStringList();
int i;
s1=new char[50*1024];
s2="a";
s1[0]='\0';
for(i=1;i<=50*1024;i++) //指针溢出 for(i=1;i<51199;i++)//不要让计算机老是计算
{
s1=strcat(s1,s2);
//-------------------- 这里要干吗?
}
s1[i]=0;
}
//-------------------- 本函数溢出了
void __fastcall TTransForm::ClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
GetSystemTime(×2); // ????
//============= TDateTime time2= Now();
String sTemp=""; //------- 不确定空间存储
char SUB[20];
WORD sub;
sub=(((times2.wHour - times1.wHour) * 60 + (times2.wMinute - times1.wMinute)) * 60 +(times2.wSecond - times1.wSecond)) * 1000 + times2.wMilliseconds - times1.wMilliseconds;
sub=sub/2;
//============ sub = time2.Int()/2;
ltoa(sub,SUB,10);
sTemp=sTemp+"延时"+SUB+"毫秒";
//============ char *str = new char[30];
//============ AnsiString tmp = AnsiString(str); tmp.sprintf("延时%d毫秒",sub);
// Memo2->Lines->Add(Socket->ReceiveText());
Memo2->Lines->Add(sTemp);
MyList->Add(sTemp);
//============ Memo2->Lines->Append(tmp);MyList->Add(new AnsiString(tmp));
/*
AnsiString time2;
char *time3;
time2=TimeToStr(Time());
time3=time2.c_str();
// s1=strcat(s1,time3);
Memo2->Lines->Add(time3);
*/
}
//------------------- 怪怪的用法!
void __fastcall TTransForm::SendMessageClick(TObject *Sender)
{
int i;
Timer1->Interval=200;
// Timer1->Enabled=true;
for(i=0;i<5;i++)
{
GetSystemTime(×1);
ClientSocket->Socket->SendText(s1);
// Sleep(300);
}
}
//---------------- 到底想做什么?
程序脉络清晰不仅仅是风格问题,在很大程度上能帮助自己知道在做什么,如何避免错误。但是在这个程序里,过多的全局变量使得程序变得很晦涩,难以理解。
  • 打赏
  • 举报
回复
yesry 2004-05-13
IsServer是什么?
  • 打赏
  • 举报
回复
csym 2004-05-11
tcp发包得时候可能会把你几次发得包组合在一起,也就是说你受到的包其实是几个包
  • 打赏
  • 举报
回复
zhyuanbaron 2004-05-08
恳请各位大虾帮忙啊,要多少分只管说,小弟感激不尽!!!
  • 打赏
  • 举报
回复
zhyuanbaron 2004-05-08
客户端程序
//------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma link "ScktComp"
#pragma resource "*.dfm"
TTransForm *TransForm;
//---------------------------------------------------------------------------
__fastcall TTransForm::TTransForm(TComponent* Owner)
: TForm(Owner)
{
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::FileConnectItemClick(TObject *Sender)
{
if (ClientSocket->Active)
{
ClientSocket->Active = false;
}
if (InputQuery("Computer to connect to", "Address Name:", Server))
{
if (Server.Length() > 0)
{
ClientSocket->Host = Server;
ClientSocket->Active = true;
FileListenItem->Checked = false;
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::Exit1Click(TObject *Sender)
{
delete []s1;
delete MyList;
ClientSocket->Close();
Close();
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::FormCreate(TObject *Sender)
{
MyList = new TStringList();
int i;
s1=new char[50*1024];
s2="a";
s1[0]='\0';
for(i=1;i<=50*1024;i++)
{
s1=strcat(s1,s2);
}
s1[i]=0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ClientSocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteHost;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::Disconnect1Click(TObject *Sender)
{
ClientSocket->Active = false;
StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
GetSystemTime(×2);
String sTemp="";
char SUB[20];
WORD sub;
sub=(((times2.wHour - times1.wHour) * 60 + (times2.wMinute - times1.wMinute)) * 60 +(times2.wSecond - times1.wSecond)) * 1000 + times2.wMilliseconds - times1.wMilliseconds;
sub=sub/2;
ltoa(sub,SUB,10);
sTemp=sTemp+"延时"+SUB+"毫秒";
// Memo2->Lines->Add(Socket->ReceiveText());
Memo2->Lines->Add(sTemp);
MyList->Add(sTemp);
/*
AnsiString time2;
char *time3;
time2=TimeToStr(Time());
time3=time2.c_str();
// s1=strcat(s1,time3);
Memo2->Lines->Add(time3);
*/
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ClientSocketError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
Memo2->Lines->Add("Error connecting to:" + Server);
ErrorCode = 0;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::SendMessageClick(TObject *Sender)
{
int i;
Timer1->Interval=200;
// Timer1->Enabled=true;
for(i=0;i<5;i++)
{
GetSystemTime(×1);
ClientSocket->Socket->SendText(s1);
// Sleep(300);
}
}
//---------------------------------------------------------------------------
void __fastcall TTransForm::DisplayClick(TObject *Sender)
{
Memo2->Lines->AddStrings(MyList);
}
//---------------------------------------------------------------------------

void __fastcall TTransForm::Timer1Timer(TObject *Sender)
{
GetSystemTime(×1);
ClientSocket->Socket->SendText(s1);
//Memo2->Lines->AddStrings(MyList);
}
//---------------------------------------------------------------------------
  • 打赏
  • 举报
回复
zhyuanbaron 2004-05-08
服务器端程序
//------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma link "ScktComp"
#pragma resource "*.dfm"
TTransForm *TransForm;
//---------------------------------------------------------------------------
__fastcall TTransForm::TTransForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TTransForm::FileListenItemClick(TObject *Sender)
{
FileListenItem->Checked = !FileListenItem->Checked;
if (FileListenItem->Checked)
{
ClientSocket->Active = false;
ServerSocket->Active = true;
StatusBar1->Panels->Items[0]->Text = "Listening...";
}else
{
if (ServerSocket->Active)
{
ServerSocket->Active = false;
}
StatusBar1->Panels->Items[0]->Text = "";
}
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::Exit1Click(TObject *Sender)
{
delete []s1;
ServerSocket->Close();
ClientSocket->Close();
Close();
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::FormCreate(TObject *Sender)
{
FileListenItemClick(NULL);
int i;
s1=new char[50*1024];
s2="a";
s1[0]='\0';
for(i=1;i<=50*1024;i++)
{
s1=strcat(s1,s2);
}
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::Disconnect1Click(TObject *Sender)
{
ClientSocket->Active = false;
ServerSocket->Active = true;
StatusBar1->Panels->Items[0]->Text = "Listening...";
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ServerSocketClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
if(IsServer)
{
ServerSocket->Socket->Connections[0]->SendText(s1);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ServerSocketAccept(TObject *Sender,
TCustomWinSocket *Socket)
{
IsServer = true;
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteAddress;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ServerSocketClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Clear();
}


//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TTransForm::ServerSocketClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Listening...";
}

//---------------------------------------------------------------------------


  • 打赏
  • 举报
回复
庄鱼 2004-05-08
把你的程序贴出来,看看是不是socket的read方法出了问题。
  • 打赏
  • 举报
回复
zhyuanbaron 2004-05-08
我把timer去掉,用循环来做这个程序,还是一样的!楼上说socket被关掉是怎么回事啊?初始化又要注意什么问题啊?
  • 打赏
  • 举报
回复
pp616 2004-05-07
看看代码啊。
  • 打赏
  • 举报
回复
叶子哟 2004-05-07
代码?
是长连接,还是一边一断呀!注意初始化的问题
  • 打赏
  • 举报
回复
llin998 2004-05-07
你好好检查一下程序,一定是一个非常简单的错误,你检查一下是不是timer只触发一次就被关掉了,还是socket被关掉了,一定是一个非常愚蠢的错误!
  • 打赏
  • 举报
回复
yayowd 2004-05-07
看看是不是时间间隔设置的太短了啊

这么简单的过程应该不会有问题啊??
  • 打赏
  • 举报
回复
相关推荐
发帖
网络及通讯开发
加入

1304

社区成员

C++ Builder 网络及通讯开发
申请成为版主
帖子事件
创建了帖子
2004-05-07 07:46
社区公告
暂无公告