服务器端程序问题

lovelywenli 2006-05-28 05:06:44
#include <vcl.h>
#pragma hdrstop

#include "Mainform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
this->Memo1->Text="";
this->RichEdit1->Text="";
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{//监听
this->ServerSocket->Port=StrToInt(this->Edit1->Text);
this->ServerSocket->Active=true;
this->StatusBar1->SimpleText="正在监听客户端连接状态......";
}
//---------------------------------------------------------------------------
bind
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{//断开
this->ServerSocket->Close();
this->StatusBar1->SimpleText="已经断开与客户端的连接";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{//发送
this->ServerSocket->Socket->Connections[0]->SendText(this->RichEdit1->Text);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ServerOnAccept(TObject *Sender,
TCustomWinSocket *Socket)
{
this->RichEdit1->Clear();
this->StatusBar1->SimpleText="连接到:"+this->ServerSocket->Socket->RemoteAddress;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ServerOnClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
this->Memo1->Lines->Add(Socket->ReceiveText());
}
能看出来上面的代码错在哪吗,为什么和客户端连不上呢
...全文
255 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshu888 2006-05-29
  • 打赏
  • 举报
回复
程序 好像没什么问题,可能是连接配置有问题。
铖邑 2006-05-29
  • 打赏
  • 举报
回复
在命令行提示中输入“telnet 127.0.0.1 1024”
lovelywenli 2006-05-29
  • 打赏
  • 举报
回复
telnet的用法我不会,但是查了一下好象不太好用
lovelywenli 2006-05-29
  • 打赏
  • 举报
回复

怎么连呢
铖邑 2006-05-29
  • 打赏
  • 举报
回复
127.0.0.1只能在本地使用
lovelywenli 2006-05-29
  • 打赏
  • 举报
回复
serveraddress设置的是127.0.0.1
铖邑 2006-05-29
  • 打赏
  • 举报
回复
光从上面的代码,看不出什么问题。你用telnet连接一下服务器看看,telnet 127.0.0.1 1024
lovelywenli 2006-05-29
  • 打赏
  • 举报
回复
端口应该没有问题,都是一样的设置1024
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Mainform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
this->Memo1->Text="";
this->RichEdit1->Text="";
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{//连接
this->ClientSocket1->Port=StrToInt(this->Edit1->Text);
this->ClientSocket1->Host=this->Edit2->Text;
this->ClientSocket1->Active=true;
this->StatusBar1->SimpleText="连接服务器......";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{//断开
this->ClientSocket1->Close();
this->StatusBar1->SimpleText="已经断开与服务器的连接";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{//发送
this->ClientSocket1->Socket->SendText(this->RichEdit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientOnConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
this->StatusBar1->SimpleText="连接到:"+this->Edit2->Text;
RichEdit1->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientOnRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo1->Lines->Add(this->ClientSocket1->Socket->ReceiveText());
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientOnError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
this->StatusBar1->SimpleText="无法连接到:"+this->ClientSocket1->Socket->RemoteHost;
}
//---------------------------------------------------------------------------

这个是客户端的程序
就是查不出问题
铖邑 2006-05-29
  • 打赏
  • 举报
回复
端口有没有设置好?
飞哥 2006-05-28
  • 打赏
  • 举报
回复
没错
可能是client端有问题
比如serveraddress
什么的
pp616 2006-05-28
  • 打赏
  • 举报
回复
看不出什么问题。
ServerSocket->Active=true;
后就应该可以正常接受连接了。

1,317

社区成员

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

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