关于bcb中实现P2P聊天的问题

beyonddream 2006-05-29 07:35:20
本人刚接触windows编程不久,用bcb做聊天程序,程序差不多完工,但运行后聊天就出现错误:can't change value while socket is active,我的程序是采用一个服务器端(一个TServerSocket),客户端(一对TServerSocket和TClientSocket,一对NMMSG和NMMSGSrv用于客户端间聊天和传送文件,还有一个TClientSocket 与服务器端交流信息),程序代码都写完了,编译通过了,但就是开始P2P聊天就报上面那个错了,因为对Socket编程不熟,经过思考,觉得p2p聊天那块该用多线程来实现,但那部分不大会啊,各位能否指点一下啊.急啊!指点一下解决方案,我希望是尽量减少我原来程序的修改量,期待中......
需要贴源码吗 ?源码要看的话,我再发啊
...全文
819 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyonddream 2006-06-19
  • 打赏
  • 举报
回复
毕设已经搞完,iwainfo(iwainfo)兄你的程序在构建上出现问题,在点对点聊天时信息发送常一方收不到另一方,传送文件也有类似的问题,获取名单的服务器处理也是有问题(这个我解决了,但是用TCP确实时间很慢),多人聊天的构建的思想是只能2人同时通信,这个不能多对多是我想实现的,呵呵,还是非常感谢你。以上仅为我发现的问题,希望交流。
到现在好少人来看贴了,我也觉得该节贴了,这个问题也许我的设计思想方面不足,我对这种网络编程还比较陌生,所以也许提的问题就有错,大概是没人再会看贴了,所以今天节贴。以后(不久的几天内了^_^),我若完成,将把代码发出来,供后人参考。(不能保证完成阿)
最后,谢谢帮助我和热心帮助的人,你们的意见我都很珍视,分也许对你们不算什么,我仅有此才能表点谢意,请接分
huzhangyou 2006-06-13
  • 打赏
  • 举报
回复
或者加我的QQ
www.libing.net.cn上面有
我最近在设计一个分布式服务器
并发量可能在千万
架构相当困难
或许能对你有一些帮助
huzhangyou 2006-06-13
  • 打赏
  • 举报
回复
这样吧,因为开公司了,这几天是在太忙了
只有在夜半的时候才有时间过来看看帖子

你去www.codeproject.com看看吧
注册一个
然后里面有你需要的所有东西
不过E文要好一些
huzhangyou 2006-06-13
  • 打赏
  • 举报
回复
呵呵 看来你没有认真读我的回复了
朋友 态度决定一切啊
送你一句话吧:

做事情要认真一些。

我的QQ
引用
或者加我的QQ
www.libing.net.cn上面有
beyonddream 2006-06-13
  • 打赏
  • 举报
回复
to huzhangyou(信仰):你QQ多少啊?开公司了啊,羡慕ing.多谢回帖提供方法.

我QQ14395035,愿意给点意见或者想交朋友的加我啊,本人现在还是编程的菜鸟,也即将踏进软件行业工作,心地善良(个人最注重的品质),我过几天就毕设答辩了,现在在搞论文修饰,呵呵,程序只能到时再说了.都怪平时玩游戏玩多了(不玩网游)^_^.毕设过关后,差不多该节贴给分了,谢各位热心人.
beyonddream 2006-06-12
  • 打赏
  • 举报
回复
to iwainfo(iwainfo):来信已收到,但能对我原来的问题给个建议或者相关问题好吗?就是我这种想发可行不,不可行又是为什么?还有最好怎么实现这种想法
beyonddream 2006-06-08
  • 打赏
  • 举报
回复
to meikeer(美可儿) :
呵呵 ,你的程序上次逛论坛就看到了,那时想的是点对点方式的实现,所以没认真看,准备看看,现在你的那个改过了吗 ?
beyonddream 2006-06-08
  • 打赏
  • 举报
回复
谢谢楼上2位,真是感谢你们的帮助,看到贴我就很高兴了 ,能得到帮助更是受宠了,我的邮箱是 cjhgorge@163.com
meikeer 2006-06-07
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/TopicView.asp?id=4745207

聊天的东东俺也写了一个,你可以看看
iwainfo 2006-06-07
  • 打赏
  • 举报
回复
类似的程序我也有几年没有写了,幸好原来的程序还在,可权当参考,如果你需要可以留下邮箱,我邮给你一份
beyonddream 2006-06-06
  • 打赏
  • 举报
回复
10060错误:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。(使用Tseversocket,和TClientSocket出现的错误,谁知道产生的原因可能会是哪些?)
当我把聊天窗口的TServerSokcet和TClientSocket换成NMMsg和NMMSGServ的时候没这个错误,发消息有的时候收到,有的时候收不到是怎么回事?
meikeer 2006-06-05
  • 打赏
  • 举报
回复
多线程我就不明白了,因为我重来没用过多线程的东东,我仅仅是看别人的代码,不会写啊
beyonddream 2006-06-05
  • 打赏
  • 举报
回复
to iwainfo(iwainfo) :谢谢回贴,其实我看过这段的贴子,贴名叫"我写的一个简单的聊天程序",用了其中的一些代码,不知道是不是你的原作,我后来想自己做出点东西,因而也没很仔细的想上面这段程序的具体构建,如今我是比较郁闷在我那个上面的想法是不是就设计不通
我的程序筐架就是在选择私聊对象后,希望弹出窗口(这个没问题)进行点对点的聊天,在这个窗口放置了一个TServerSocket和一个TClientSocket,于是问题就好多,请各位大虾帮帮小第
iwainfo 2006-06-05
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
//选择要传送的文件
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
if(NMStrm1->Connected)
NMStrm1->Disconnect();
if(OpenDialog1->Execute())
{
Edit1->Text=OpenDialog1->FileName;
BitBtn6->Enabled=true;
}
}
//---------------------------------------------------------------------------
//登陆到服务器
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(ClientSocket1->Active)
{
ClientSocket1->Active=false;
ClientSocket1->Close();
}
ListBox1->Items->Clear();
ListBox2->Items->Clear();
Memo1->Clear();
Memo3->Clear();
if(InputQuery("登陆到服务器","请输入你的昵称:",yourName)) //输入昵称
{
StatusBar1->SimpleText="正在连接服务器...";

ClientSocket1->Address=MaskEdit1->Text;
ClientSocket1->Host=Edit2->Text;
ClientSocket1->Port=4000;
ClientSocket1->Active=true;
BitBtn1->Enabled=true;
BitBtn3->Enabled=true;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListBox1->Items->Clear();
ListBox2->Items->Clear();
ListBox3->Items->Clear();
ClientSocket1->Active=false;
StatusBar1->SimpleText="连接中断...";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
WSACleanup();
ServerSocket1->Close();
ClientSocket1->Close();
ClientSocket2->Close();
NMStrm1->Cancel();
NMStrmServ1->Cancel();
Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N2Click(TObject *Sender)
{
TabbedNotebook1->PageIndex=3;
StatusBar1->SimpleText="传送文件...";
Edit1->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N1Click(TObject *Sender)
{
TabbedNotebook1->PageIndex=2;
StatusBar1->SimpleText="私聊空间...";
Memo4->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TabbedNotebook1Change(TObject *Sender, int NewTab,
bool &AllowChange)
{
if(NewTab==0) StatusBar1->SimpleText="连接设置...";
else if(NewTab==1) StatusBar1->SimpleText="网络会议...";
else if(NewTab==2) StatusBar1->SimpleText="私聊空间...";
else if(NewTab==3) StatusBar1->SimpleText="传送文件...";
else if(NewTab==4) StatusBar1->SimpleText="本机信息...";
else if(NewTab==5) StatusBar1->SimpleText="版本信息...";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ListBox1->Items->Clear();
ListBox2->Items->Clear();
ListBox3->Items->Clear();
ClientSocket1->Active=false;
StatusBar1->SimpleText="连接已中断...";
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString Data=Socket->ReceiveText(); //接收私聊对象发送的信息
Memo3->Lines->Add(Data);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientSocket1->Socket->SendText(yourName+"@logout@");
ClientSocket1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
Application->MessageBox("无法连接主机!网络不通或者服务端未启动!","连接失败!",MB_OK);
StatusBar1->SimpleText="连接服务器失败...";
Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ClientSocket2Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText="成功连接到"+StaticText1->Caption;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket2Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientSocket2->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket2Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
Application->MessageBox("无法连接到对对象!网络不通或对方未在线!","连接失败!",MB_OK);
StatusBar1->SimpleText="连接对象失败...";

}
//---------------------------------------------------------------------------



void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
if(Edit1->Text=="")
{
Application->MessageBox("请选择需要传送的文件!","...",MB_OK);
BitBtn6->Enabled=false;
}
else
{
BitBtn6->Enabled=true;
TFileStream *MyFStream;
MyFStream = new TFileStream(OpenDialog1->FileName, fmOpenRead);
try
{
NMStrm1->Host = HostName;
NMStrm1->FromName = Edit1->Text;
NMStrm1->PostIt(MyFStream);
}
catch(...)
{
}
MyFStream->Free();
}
}
//---------------------------------------------------------------------------

iwainfo 2006-06-05
  • 打赏
  • 举报
回复
客户端:client.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "iostream.h"
#include "io.h"
#include "time.h"
#include "stdio.h"
#include "alloc.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString yourName="yourName";
AnsiString IP="255.255.255.0";
AnsiString HostName="";
AnsiString NowlogName="";
AnsiString NowlogHost="";
AnsiString NowlogAddress="";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Memo1->Clear();
Memo2->Clear();
Memo3->Clear();
Memo4->Clear();
BitBtn6->Enabled=false;
Edit1->Text="";
Edit2->Text="dy-02";
StaticText1->Caption="";
StaticText5->Caption="";
StaticText6->Caption="";
StaticText7->Caption="";
Panel1->Caption="私聊对象:" ;
//---------------------------------------
//获取本地IP地址及主机名
WORD wVersionRequested;
WSADATA wsaData;
//Start up WinSock
wVersionRequested = MAKEWORD(1, 1);
WSAStartup(wVersionRequested, &wsaData);

hostent *p;
char s[128];
char *p2;
//Get the computer name
gethostname(s, 128);
p = gethostbyname(s);
StaticText5->Caption=p->h_name;
//Get the IpAddress
p2 = inet_ntoa(*((in_addr *)p->h_addr));
StaticText6->Caption=p2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
Memo3->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Memo1->Clear();
}
//---------------------------------------------------------------------------
//连接服务器
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText="成功连接到服务器"+ClientSocket1->Host;;
ClientSocket1->Socket->SendText(yourName+"@login@@");
StaticText7->Caption=DateToStr(Now())+" "+Time();
}
//---------------------------------------------------------------------------
//发送信息
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if(Memo2->Lines->Text=="")
Application->MessageBox("错误, 不能发空信息!","提示",MB_OK);
else
{
AnsiString Data;
Data=yourName+":"+Memo2->Lines->Text;
ClientSocket1->Socket->SendText(Data);
// Memo1->Lines->Add(Data);
Memo2->Lines->Clear();
Memo2->SetFocus();
}
}
//---------------------------------------------------------------------------
//接收服务器发送过来的信息
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
int i=0;
AnsiString ch="";
NowlogName="";
NowlogHost="";
NowlogAddress="";

AnsiString Data=Socket->ReceiveText(); //接收用户发送的信息
AnsiString SubData1=Data.SubString(1,8); //分析信息
if(SubData1=="@login@@") //如果是用户登陆的信息
{
for(i=9;(ch=Data.SubString(i,1))!="@";i++)
NowlogName+=ch;
for( ;(ch=Data.SubString(i+1,1))!="@";i++)
NowlogHost+=ch;
NowlogAddress=Data.SubString(i+1,18);
ListBox1->Items->Add(NowlogName); //用户信息加入到用户列表
ListBox2->Items->Add(NowlogAddress);
ListBox3->Items->Add(NowlogHost);
AnsiString SysInfo="["+TimeToStr(Now())+"] "+NowlogName+" 加入了会议!";//显示系统信息
Memo1->Lines->Add(SysInfo);
}
else if(SubData1=="@logout@") //如果是用户退出会议
{
for(i=9;(ch=Data.SubString(i,1))!="@";i++)
NowlogName+=ch;
for( ;(ch=Data.SubString(i+1,1))!="@";i++)
NowlogHost+=ch;
NowlogAddress=Data.SubString(i+1,18);
int Index1=ListBox1->Items->IndexOf(NowlogName); //从用户列表中删除该用户信息
ListBox1->Items->Delete(Index1);
int Index2=ListBox2->Items->IndexOf(NowlogAddress);
ListBox2->Items->Delete(Index2);
int Index3=ListBox3->Items->IndexOf(NowlogHost);
ListBox3->Items->Delete(Index3);
Memo1->Lines->Add("["+TimeToStr(Now())+"] "+NowlogName+" 退出了会议!"); //显示系统信息
}
else
Memo1->Lines->Add(Data);

}
//---------------------------------------------------------------------------
//私聊空间或传送文件选定对象
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
if(ClientSocket2->Active)
{
ClientSocket2->Active=false;
ClientSocket2->Close();
}
int Index=ListBox1->ItemIndex;
IP=ListBox2->Items->Strings[Index];
HostName=ListBox3->Items->Strings[Index];
Panel1->Caption="私聊对象:"+ListBox1->Items->Strings[Index];
StaticText1->Caption=ListBox1->Items->Strings[Index];
ClientSocket2->Host=HostName;
ClientSocket2->Address=IP;
ClientSocket2->Port=2222;
ClientSocket2->Active=true;
BitBtn3->Enabled=true;

}
//---------------------------------------------------------------------------
//在私聊空间发送信息给选定用户
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
if(Memo4->Lines->Text=="")
Application->MessageBox("错误, 不能发空信息!","提示",MB_OK);
else
{
AnsiString Data;
Data=yourName+"对"+StaticText1->Caption+"说:"+Memo4->Lines->Text;
ClientSocket2->Socket->SendText(Data);
// Memo3->Lines->Add(Data);
Memo4->Lines->Clear();
Memo4->SetFocus();
}
}
//---------------------------------------------------------------------------

iwainfo 2006-06-05
  • 打赏
  • 举报
回复
服务器端:server.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString NowlogName="";
AnsiString NowlogHost="";
AnsiString NowlogAddress="";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Memo1->Clear();
ListBox1->Clear();
ListBox2->Clear();
ListBox3->Clear();
ServerSocket1->Active=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
Memo1->Clear();
ListBox1->Clear();
ListBox2->Clear();
ListBox3->Clear();
AnsiString SysInfo;
SysInfo="["+TimeToStr(Now())+"]"+" 与服务器失去连接!请重新连接!";
int M=0;
M=ServerSocket1->Socket->ActiveConnections;
for(int i=0;i<M;i++)
{
ServerSocket1->Socket->Connections[i]->SendText(SysInfo);
}
ServerSocket1->Active=false;
ServerSocket1->Close();
Form1->Close();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
NowlogHost=Socket->RemoteHost;
NowlogAddress=Socket->RemoteAddress;
AnsiString Data=Socket->ReceiveText(); //接收用户发送的信息
AnsiString SubData1=Data.SubString(Data.Length()-7,8); //分析信息
AnsiString SubData2=Data.SubString(1,Data.Length()-8);
if(SubData1=="@login@@") //如果是用户登陆的信息
{
ListBox1->Items->Add(SubData2); //用户信息加入到用户列表
ListBox2->Items->Add(NowlogAddress);
ListBox3->Items->Add(NowlogHost);
AnsiString SysInfo="["+TimeToStr(Now())+"]"+SubData2+" 加入了会议!";//显示系统信息
Memo1->Lines->Add(SysInfo);
if(ServerSocket1->Socket->ActiveConnections)
for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
// ServerSocket1->Socket->Connections[i]->SendText(SubData2+"@login@@");
ServerSocket1->Socket->Connections[i]->SendText("@login@@"+SubData2+"@"+NowlogHost+"@"+NowlogAddress);

}
else if(SubData1=="@logout@") //如果是用户退出会议
{
int Index1=ListBox1->Items->IndexOf(SubData2); //从用户列表中删除该用户信息
ListBox1->Items->Delete(Index1);
int Index2=ListBox2->Items->IndexOf(NowlogAddress);
ListBox2->Items->Delete(Index2);
int Index3=ListBox3->Items->IndexOf(NowlogHost);
ListBox3->Items->Delete(Index3);
Memo1->Lines->Add("["+TimeToStr(Now())+"]"+SubData2+" 退出了会议!"); //显示系统信息
if(ServerSocket1->Socket->ActiveConnections)
for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
// ServerSocket1->Socket->Connections[i]->SendText(SubData2+"@logout@");
ServerSocket1->Socket->Connections[i]->SendText("@logout@"+SubData2+"@"+NowlogHost+"@"+NowlogAddress);
}
else
{
Data="["+TimeToStr(Now())+"]"+Data; //加入系统时间
Memo1->Lines->Add(Data);
//向所有用户转发信息
if(ServerSocket1->Socket->ActiveConnections)
for(int i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendText(Data);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1Change(TObject *Sender)
{
if(Memo1->Lines->Count>=100)
Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------

beyonddream 2006-06-04
  • 打赏
  • 举报
回复
每天都来看一下,已经没人给我回贴了吗 ,有些失望。问题是不是太简单了啊 ,但对我比较重要,后来人能帮的请不吝赐教啊
beyonddream 2006-06-04
  • 打赏
  • 举报
回复
楼上的,那个问题是我代码中窗口设置与程序对应错了,已经改了,但我现在担忧我这种不采用多线程能设计成这个样子嘛,给点意见好吗?楼上的,是不是上次在群里给你源程序的那个啊,谢谢你先了
meikeer 2006-06-04
  • 打赏
  • 举报
回复
can't change value while socket is active
不能在socket活动的时候改变值

那你在改变值如端口或IP地址的时候active属性设置为false,改变值后再改变true
beyonddream 2006-05-31
  • 打赏
  • 举报
回复
to huzhangyou(信仰):
刚灌了你的论坛,羡慕所有有个人网上小家的大家,那篇IOCP正在啃,但我只能了解个大概,到改我的程序还有好长的距离(感觉上),我的那个P2P也许我理解错误,我的理解是点对点的聊天,如有不妥,望指正.大哥是在读研究生吧,跟我们学校差不多,我交中期检查表时间也跟你查不多,我是本科.6.7号就交我的论文到系里了,论文现在都没写,今晚就开始,所以比较急了.


to 后来人
达人们能不能关于我的程序如何尽量作小的修改方面给点意见,时间紧逼,我是连多线程用到什么类都不大知道,这2天狠逛前人的帖子,发现有我这方面的解决方法,不过我比较菜点,望高手能稍详细的说说



加载更多回复(10)

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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