L
landylcq
Unregistered / Unconfirmed
GUEST, unregistred user!
procedure Tform1.refresh_memo3;
var i:integer;
begin
memo3.lines.clear;
memo3.lines.add(membername+'['+serversocket1.socket.localhost+'](主席)');
for i:=0 to serversocket1.socket.activeconnections-1 do begin
memo3.lines.add(serversocket1.Socket.Connections.remotehost+'(成员)');
end;
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
{ if isserver then begin
toolbutton1.enabled:=true;
//toolbutton1.down:=true;
toolbutton3.enabled:=false;
toolbutton5.enabled:=false;
end
else begin
toolbutton1.enabled:=false;
toolbutton3.enabled:=true;
toolbutton5.enabled:=true;
end;
if toolbutton1.down then begin
clientsocket1.active:=false;
serversocket1.active:=true;
statusbar1.panels[2].text:='本机是服务器,正在侦听...';
end else begin
if serversocket1.active then
serversocket1.active:=false;
statusbar1.panels[2].text:='本机是客户进程';
end; }
end;
procedure TForm1.ToolButton3Click(Sender: TObject);
begin
{if toolbutton3.down then begin
if clientsocket1.active then
clientsocket1.active:=false;
with Clientsocket1 do begin
Host:=Server;
try
active:=true;
statusbar1.panels[2].text:='本机是客户进程,连接成功';
except
showmessage('连接到服务器失败');
end;
end;
end;}
end;
procedure TForm1.ToolButton5Click(Sender: TObject);
begin
{ if toolbutton5.Down then begin
try
clientsocket1.active:=false;
statusbar1.Panels[2].text:='本机是客户进程,断开连接';
except
showmessage('断开连接失败');
end;
end;}
end;
procedure TForm1.ToolButton9Click(Sender: TObject);
begin
serversocket1.close;
clientsocket1.close;
close;
end;
procedure TForm1.StatusBar2DrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
var oldcolor:tcolor;
oldstyle:tbrushstyle;
begin
if panel.index=3 then begin
with statusbar1.Canvas do begin
oldstyle:=brush.Style;
brush.style:=bsclear;
oldcolor:=font.Color;
font.Color:=clyellow;
font.Name:='隶书';
font.Height:=20;
imagelist1.draw(
statusbar1.Canvas,
rect.Left+5,
rect.top+1,
8
);
font.color:=clblue;
textout(
rect.left+40,
rect.Top+5,
panel.Text
);
font.color:=clyellow;
textout(
rect.Left+42,
rect.top+7,
panel.Text);
font.Color:=oldcolor;
brush.style:=oldstyle;
end;
end;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var i:integer;
begin
if key=VK_Return then
if isserver then
for i:=0 to serversocket1.Socket.ActiveConnections-1 do begin
serversocket1.Socket.connections.SendText('[讨论]'
+memo1.Lines[memo1.Lines.count-1]+'('+membername+')')
end
else
clientsocket1.socket.sendtext('[讨论]'+memo1.lines[memo1.Lines.count-1]
+'('+membername+')');
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
refresh_memo3;
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.clear;
memo2.Lines.clear;
memo3.lines.clear;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var ss1,ss2:string;
l1,i:integer;
begin
ss1:=socket.ReceiveText;
l1:=length(ss1);
ss2:=copy(ss1,7,l1-6);
memo2.lines.add(ss2);
for i:=0 to serversocket1.Socket.ActiveConnections-1 do begin
if (serversocket1.Socket.Connections<>socket) then
serversocket1.Socket.connections.SendText('[讨论]'+ss2)
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.Panels[2].text:='本机是客户进程,已连接到'+socket.RemoteHost;
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
refresh_memo3;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
showmessage('连接服务器失败');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var cmd,ss1,ss2,ss3:string;
l1,k:integer;
begin
ss1:=socket.ReceiveText;
l1:=length(ss1);
cmd:=copy(ss1,2,4);
ss2:=copy(ss1,7,l1-6);
if cmd='讨论' then
memo2.Lines.add(ss2)
else if cmd='刷新' then
memo3.Lines.Clear
else begin
k:=pos('[信息]',ss2);
while(k>0) do begin
ss3:=copy(ss2,1,k-1);
ss2:=copy(ss2,k+6,length(ss2)-k-5);
memo3.lines.add(ss3);
k:=pos('[信息]',ss2);
end;
memo3.lines.add(ss2);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i,k:integer;
begin
if isserver then begin
refresh_memo3;
for i:=0 to serversocket1.Socket.activeconnections-1 do
serversocket1.Socket.Connections.SendText('[刷新]开始');
for k:=1 to memo3.Lines.count do
for i:=0 to serversocket1.socket.activeconnections-1 do
serversocket1.socket.connections.sendtext('[信息]'+memo3.lines[k-1]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var ss:integer;
p1,p2:string;
begin
ss:=paramcount;
if ss<3 then
application.Terminate
else begin
p1:=lowercase(paramstr(1));
p2:=paramstr(2);
membername:=paramstr(3);
if p1='server' then
isserver:=true
else
isserver:=false;
server:=p2;
end;
caption:=caption+'['+membername+']';
end;
procedure TForm1.N5Click(Sender: TObject);
begin
close;
end;
procedure TForm1.listen1Click(Sender: TObject);
begin
if isserver then begin
listen1.enabled:=true;
toolbutton1.Enabled:=true;
listen1.checked:=true;
toolbutton1.down:=true;
toolbutton3.enabled:=false;
toolbutton5.enabled:=false;
connect1.Enabled:=false;
disconnect1.Enabled:=false;
end
else begin
listen1.Enabled:=false;
toolbutton1.enabled:=false;
toolbutton3.enabled:=true;
toolbutton5.enabled:=true;
connect1.Enabled:=true;
disconnect1.Enabled:=true;
end;
if listen1.checked then begin
clientsocket1.active:=false;
serversocket1.active:=true;
statusbar1.panels[2].text:='本机是服务器,正在侦听...';
end else begin
if serversocket1.active then
serversocket1.active:=false;
statusbar1.panels[2].text:='本机是客户进程';
end;
end;
procedure TForm1.connect1Click(Sender: TObject);
begin
if (sender is Tmenuitem) then begin
connect1.Checked:=not connect1.Checked;
toolbutton3.Down:=connect1.Checked;
end
else
connect1.Checked:=toolbutton3.down;
if connect1.checked then begin
if clientsocket1.active then
clientsocket1.active:=false;
with Clientsocket1 do begin
Host:=Server;
try
active:=true;
statusbar1.panels[2].text:='本机是客户进程,连接成功';
except
showmessage('连接到服务器失败');
end;
end;
end;
end;
procedure TForm1.disconnect1Click(Sender: TObject);
begin
if (sender is Tmenuitem) then begin
disconnect1.checked:=not disconnect1.checked;
toolbutton5.Down:=disconnect1.checked
end else
disconnect1.checked:=toolbutton5.Down;
if disconnect1.checked then begin
try
clientsocket1.active:=false;
statusbar1.Panels[2].text:='本机是客户进程,断开连接';
except
showmessage('断开连接失败');
end;
end;
end;
end.
然后 我运行 cmd
project1 server/client servername membername
就是实现不了 提示-服务器连接失败 还有就是 侦听 连接 等功能实现不了。
不懂是什么原因?
请高手指点 谢谢!!!!
var i:integer;
begin
memo3.lines.clear;
memo3.lines.add(membername+'['+serversocket1.socket.localhost+'](主席)');
for i:=0 to serversocket1.socket.activeconnections-1 do begin
memo3.lines.add(serversocket1.Socket.Connections.remotehost+'(成员)');
end;
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
{ if isserver then begin
toolbutton1.enabled:=true;
//toolbutton1.down:=true;
toolbutton3.enabled:=false;
toolbutton5.enabled:=false;
end
else begin
toolbutton1.enabled:=false;
toolbutton3.enabled:=true;
toolbutton5.enabled:=true;
end;
if toolbutton1.down then begin
clientsocket1.active:=false;
serversocket1.active:=true;
statusbar1.panels[2].text:='本机是服务器,正在侦听...';
end else begin
if serversocket1.active then
serversocket1.active:=false;
statusbar1.panels[2].text:='本机是客户进程';
end; }
end;
procedure TForm1.ToolButton3Click(Sender: TObject);
begin
{if toolbutton3.down then begin
if clientsocket1.active then
clientsocket1.active:=false;
with Clientsocket1 do begin
Host:=Server;
try
active:=true;
statusbar1.panels[2].text:='本机是客户进程,连接成功';
except
showmessage('连接到服务器失败');
end;
end;
end;}
end;
procedure TForm1.ToolButton5Click(Sender: TObject);
begin
{ if toolbutton5.Down then begin
try
clientsocket1.active:=false;
statusbar1.Panels[2].text:='本机是客户进程,断开连接';
except
showmessage('断开连接失败');
end;
end;}
end;
procedure TForm1.ToolButton9Click(Sender: TObject);
begin
serversocket1.close;
clientsocket1.close;
close;
end;
procedure TForm1.StatusBar2DrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
var oldcolor:tcolor;
oldstyle:tbrushstyle;
begin
if panel.index=3 then begin
with statusbar1.Canvas do begin
oldstyle:=brush.Style;
brush.style:=bsclear;
oldcolor:=font.Color;
font.Color:=clyellow;
font.Name:='隶书';
font.Height:=20;
imagelist1.draw(
statusbar1.Canvas,
rect.Left+5,
rect.top+1,
8
);
font.color:=clblue;
textout(
rect.left+40,
rect.Top+5,
panel.Text
);
font.color:=clyellow;
textout(
rect.Left+42,
rect.top+7,
panel.Text);
font.Color:=oldcolor;
brush.style:=oldstyle;
end;
end;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var i:integer;
begin
if key=VK_Return then
if isserver then
for i:=0 to serversocket1.Socket.ActiveConnections-1 do begin
serversocket1.Socket.connections.SendText('[讨论]'
+memo1.Lines[memo1.Lines.count-1]+'('+membername+')')
end
else
clientsocket1.socket.sendtext('[讨论]'+memo1.lines[memo1.Lines.count-1]
+'('+membername+')');
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
refresh_memo3;
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.clear;
memo2.Lines.clear;
memo3.lines.clear;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var ss1,ss2:string;
l1,i:integer;
begin
ss1:=socket.ReceiveText;
l1:=length(ss1);
ss2:=copy(ss1,7,l1-6);
memo2.lines.add(ss2);
for i:=0 to serversocket1.Socket.ActiveConnections-1 do begin
if (serversocket1.Socket.Connections<>socket) then
serversocket1.Socket.connections.SendText('[讨论]'+ss2)
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.Panels[2].text:='本机是客户进程,已连接到'+socket.RemoteHost;
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
refresh_memo3;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
showmessage('连接服务器失败');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var cmd,ss1,ss2,ss3:string;
l1,k:integer;
begin
ss1:=socket.ReceiveText;
l1:=length(ss1);
cmd:=copy(ss1,2,4);
ss2:=copy(ss1,7,l1-6);
if cmd='讨论' then
memo2.Lines.add(ss2)
else if cmd='刷新' then
memo3.Lines.Clear
else begin
k:=pos('[信息]',ss2);
while(k>0) do begin
ss3:=copy(ss2,1,k-1);
ss2:=copy(ss2,k+6,length(ss2)-k-5);
memo3.lines.add(ss3);
k:=pos('[信息]',ss2);
end;
memo3.lines.add(ss2);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i,k:integer;
begin
if isserver then begin
refresh_memo3;
for i:=0 to serversocket1.Socket.activeconnections-1 do
serversocket1.Socket.Connections.SendText('[刷新]开始');
for k:=1 to memo3.Lines.count do
for i:=0 to serversocket1.socket.activeconnections-1 do
serversocket1.socket.connections.sendtext('[信息]'+memo3.lines[k-1]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var ss:integer;
p1,p2:string;
begin
ss:=paramcount;
if ss<3 then
application.Terminate
else begin
p1:=lowercase(paramstr(1));
p2:=paramstr(2);
membername:=paramstr(3);
if p1='server' then
isserver:=true
else
isserver:=false;
server:=p2;
end;
caption:=caption+'['+membername+']';
end;
procedure TForm1.N5Click(Sender: TObject);
begin
close;
end;
procedure TForm1.listen1Click(Sender: TObject);
begin
if isserver then begin
listen1.enabled:=true;
toolbutton1.Enabled:=true;
listen1.checked:=true;
toolbutton1.down:=true;
toolbutton3.enabled:=false;
toolbutton5.enabled:=false;
connect1.Enabled:=false;
disconnect1.Enabled:=false;
end
else begin
listen1.Enabled:=false;
toolbutton1.enabled:=false;
toolbutton3.enabled:=true;
toolbutton5.enabled:=true;
connect1.Enabled:=true;
disconnect1.Enabled:=true;
end;
if listen1.checked then begin
clientsocket1.active:=false;
serversocket1.active:=true;
statusbar1.panels[2].text:='本机是服务器,正在侦听...';
end else begin
if serversocket1.active then
serversocket1.active:=false;
statusbar1.panels[2].text:='本机是客户进程';
end;
end;
procedure TForm1.connect1Click(Sender: TObject);
begin
if (sender is Tmenuitem) then begin
connect1.Checked:=not connect1.Checked;
toolbutton3.Down:=connect1.Checked;
end
else
connect1.Checked:=toolbutton3.down;
if connect1.checked then begin
if clientsocket1.active then
clientsocket1.active:=false;
with Clientsocket1 do begin
Host:=Server;
try
active:=true;
statusbar1.panels[2].text:='本机是客户进程,连接成功';
except
showmessage('连接到服务器失败');
end;
end;
end;
end;
procedure TForm1.disconnect1Click(Sender: TObject);
begin
if (sender is Tmenuitem) then begin
disconnect1.checked:=not disconnect1.checked;
toolbutton5.Down:=disconnect1.checked
end else
disconnect1.checked:=toolbutton5.Down;
if disconnect1.checked then begin
try
clientsocket1.active:=false;
statusbar1.Panels[2].text:='本机是客户进程,断开连接';
except
showmessage('断开连接失败');
end;
end;
end;
end.
然后 我运行 cmd
project1 server/client servername membername
就是实现不了 提示-服务器连接失败 还有就是 侦听 连接 等功能实现不了。
不懂是什么原因?
请高手指点 谢谢!!!!