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
Tempstrchar;
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 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
Tempstrchar;
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.