急!!为什么我的服务器不能向客户端发送信息呢? ( 积分: 100 )

  • 主题发起人 主题发起人 jfsaka
  • 开始时间 开始时间
J

jfsaka

Unregistered / Unconfirmed
GUEST, unregistred user!
这阵子在做网络技术的课程设计,是编写一个C/S程序,客户端已经写完,经测试可以实现收发信息的功能,可是服务器的程序不知道哪里出现了问题,不能向客户端发送信息,查了好多资料,自己无法解决了,麻烦各位帮忙看看哪里出了问题,服务器程序如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, Menus;

type
TfrmServer = class(TForm)
SReceive: TMemo;
SSent: TEdit;
Sent: TButton;
SInfor: TGroupBox;
Open: TButton;
Disconnet: TButton;
ServerSocket: TServerSocket;
Exit: TButton;
procedure OpenClick(Sender: TObject);
procedure DisconnetClick(Sender: TObject);
procedure ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure SSentKeyPress(Sender: TObject; var Key: Char);
procedure SentClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type User=record
UserName:string;
IP:string;
end;


var
frmServer: TfrmServer;
strIP,strHost:string;
implementation

{$R *.dfm}
//打开服务器
procedure TfrmServer.OpenClick(Sender: TObject);
begin
ServerSocket.Open;

end;

//关闭服务器
procedure TfrmServer.DisconnetClick(Sender: TObject);
begin
ServerSocket.Close;
end;

//接收客户端的连接并发送欢迎信息
procedure TfrmServer.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
var
Tempstr:pchar;
begin
Tempstr:=pchar('欢迎登陆“校友通”服务器 '#13#10);
Socket.SendText(Tempstr);
TempStr:=pchar('****************************************'#13#10);
Socket.SendText(Tempstr);
TempStr:=pchar('我们可以彼此聊天!');
Socket.SendText(Tempstr);
end;

//读取客户端的系统信息
procedure TfrmServer.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
strIP:=Socket.RemoteAddress;
strHost:=Socket.RemoteHost;
SReceive.Lines.Add('当前登陆用户');
SReceive.Lines.Add(#9'IP地址:'+strIP);
SReceive.Lines.Add(#9'主机名:'+strHost);
end;

//读取客户端发送的信息
procedure TfrmServer.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
SReceive.Lines.Add(strHost+'说'+Socket.ReceiveText);
end;

//客户端断开时显示信息
procedure TfrmServer.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
SReceive.Lines.Add('用户离线');
end;

//向客户端发送信息
procedure TfrmServer.SSentKeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
begin
ServerSocket.Socket.SendText(SSent.Text);
SReceive.Lines.Add(SSent.Text);
end;
end;

//点击发送按钮发送信息
procedure TfrmServer.SentClick(Sender: TObject);
begin
ServerSocket.Socket.SendText(SSent.Text);
SReceive.Lines.Add(SSent.Text);
end;

//退出程序
procedure TfrmServer.ExitClick(Sender: TObject);
begin
ServerSocket.Close;
frmServer.Close;
end;

end.
 
这里贴出客户端的程序,可能对上面的问题有用
unit Client;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls, Menus;

type
TfrmClient = class(TForm)
MainMenu1: TMainMenu;
Cennect: TMenuItem;
DisConnect: TMenuItem;
About: TMenuItem;
Exit: TMenuItem;
SSent: TMemo;
btSent: TButton;
SesInfor: TGroupBox;
Label1: TLabel;
edIP: TEdit;
Label2: TLabel;
EdPort: TEdit;
ClientSocket: TClientSocket;
edSent: TEdit;
procedure CennectClick(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);

procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure btSentClick(Sender: TObject);
procedure AboutClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
procedure edSentKeyPress(Sender: TObject; var Key: Char);
procedure DisConnectClick(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;
var
frmClient: TfrmClient;

implementation
uses unit2;

{$R *.dfm}

//激活客户端
procedure TfrmClient.CennectClick(Sender: TObject);
begin
ClientSocket.Address:=edIP.Text; //设定IP
ClientSocket.Port:=StrToInt(edPort.Text); //设定端口
ClientSocket.Active:=true;
ClientSocket.Open;
end;

//建立连接
procedure TfrmClient.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
MessageDlg('已经成功连接上服务器!',mtinformation,[mbYes],0);
end;

//在对话框按回车发送信息
procedure TfrmClient.edSentKeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
begin
if not ClientSocket.Active then
ClientSocket.Active:=true;;
ClientSocket.Socket.SendText(edSent.Text);
edSent.SelectAll;
key:=#0;
end;
end;

//按发送按钮发送信息
procedure TfrmClient.btSentClick(Sender: TObject);
begin
ClientSocket.Socket.SendText(edSent.Text);
end;

//获取服务器的信息以及显示客户端本身发送的信息
procedure TfrmClient.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
SSent.Lines.Add(ClientSocket.Socket.ReceiveText);
end;

//关闭客户端
procedure TfrmClient.DisConnectClick(Sender: TObject);
begin
MessageDlg('已经成功关闭客户端!',mtinformation,[mbYes],0);
ClientSocket.Active:=false;
end;

//断开连接
procedure TfrmClient.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ClientSocket.Socket.Close;
MessageDlg('已经成功断开上服务器!',mtinformation,[mbYes],0);
edSent.Clear;
end;
//获取软件信息
procedure TfrmClient.AboutClick(Sender: TObject);
begin
Form2.Show;
end;

//退出程序
procedure TfrmClient.ExitClick(Sender: TObject);
begin
frmClient.Close;
end;
end.
 
后退
顶部