X
xkdcc
Unregistered / Unconfirmed
GUEST, unregistred user!
源代码如下(窗体上有三个按钮:ListenButton,ConnectButton,DisconnectButton;一个ServerSocket和一个ClientSocket)
procedure TForm1.ListenButtonClick(Sender: TObject);
begin
if ServerSocket.Active then
begin
ServerSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Port to listen','Port:',ListenPort) then
ServerSocket.Port:=StrtoInt(ListenPort);
ClientSocket.Active := False;
ServerSocket.Active := True;
StatusBar1.Panels[0].Text := 'Listening @'+ListenPort+' ...';
end;
procedure TForm1.ConnectButtonClick(Sender: TObject);
begin
if ClientSocket.Active then
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Computer to connect to','Address Name:',Server) then
if InputQuery('Port to connect to','Port:',ConnectPort) then
if Length(Server)>0 then
with ClientSocketdo
begin
Host := Server;
Port := StrtoInt(ConnectPort);
Active := True;
end;
end;
procedure TForm1.DisconnectButtonClick(Sender: TObject);
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
procedure TForm1.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
StatusBar1.Panels[0].Text := 'Accept connection from:' + Socket.RemoteAddress;
end;
procedure TForm1.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Listening ...';
end;
procedure TForm1.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ShowMessage('ErrorCode:' + InttoStr(ErrorCode));
end;
procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Connected to:' + Socket.RemoteHost;
end;
procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:' + Server);
ErrorCode :=0;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
if IsServer then
ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
else
ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.count - 1]);
end;
procedure TForm1.ListenButtonClick(Sender: TObject);
begin
if ServerSocket.Active then
begin
ServerSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Port to listen','Port:',ListenPort) then
ServerSocket.Port:=StrtoInt(ListenPort);
ClientSocket.Active := False;
ServerSocket.Active := True;
StatusBar1.Panels[0].Text := 'Listening @'+ListenPort+' ...';
end;
procedure TForm1.ConnectButtonClick(Sender: TObject);
begin
if ClientSocket.Active then
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
if InputQuery('Computer to connect to','Address Name:',Server) then
if InputQuery('Port to connect to','Port:',ConnectPort) then
if Length(Server)>0 then
with ClientSocketdo
begin
Host := Server;
Port := StrtoInt(ConnectPort);
Active := True;
end;
end;
procedure TForm1.DisconnectButtonClick(Sender: TObject);
begin
ClientSocket.Active := False;
StatusBar1.Panels[0].Text := '';
end;
procedure TForm1.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
StatusBar1.Panels[0].Text := 'Accept connection from:' + Socket.RemoteAddress;
end;
procedure TForm1.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Listening ...';
end;
procedure TForm1.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ShowMessage('ErrorCode:' + InttoStr(ErrorCode));
end;
procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := 'Connected to:' + Socket.RemoteHost;
end;
procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to:' + Server);
ErrorCode :=0;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
if IsServer then
ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
else
ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.count - 1]);
end;