要求:程序开始运行以后,一直通过UDP协议发送55AA55AA55AA。一直等到对方收到以后,
就主动向我的程序通过TCP协议建立连接。我监听到以后,就关闭UDP,
利用TCP和对方进行数据的上传和下载。
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp,Winsock, ExtCtrls, NMUDP;
const
CMax=10; //客户端最大连接数
type
client_record=record
CHandle: integer; //客户端套接字句柄
CSocket:TCustomWinSocket; //客户端套接字
CName:string; //客户端计算机名称
CAddress:string; //客户端计算机IP地址
CUsed: boolean; //客户端联机标志
end;
type
TForm1 = class(TForm)
Memo1: TMemo;
ServerSocket1: TServerSocket;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
NMUDP1: TNMUDP;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1GetSocket(Sender: TObject; Socket: Integer;
var ClientSocket: TServerClientWinSocket);
procedure ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
procedure Timer1Timer(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
server:TSOCKET;
public
{ Public declarations }
session: array[0..CMax] of client_record; //客户端连接数组
Sessions: integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if (edit1.Text='') or (edit2.Text='') then
begin
showmessage('输入完整的服务器的地址信息!!!');
end
else
begin
Timer1.Enabled:=true;
ServerSocket1.Active:=false;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
SendBuf:array [1..8] of byte; //发送下行报文(读CDT配置)
j:integer;
sendstr:string;
begin
SendBuf[1]:=byte($0A);
SendBuf[2]:=byte($81);
SendBuf[3]:=byte($28);
SendBuf[4]:=byte($00);
SendBuf[5]:=byte($FE);
SendBuf[6]:=byte($C0);
SendBuf[7]:=byte($00);
SendBuf[8]:=byte($00);
for j:=1 to 8 do
begin
sendstr:=sendstr+' '+inttohex(SendBuf[j],2);
end;
if not serversocket1.Socket.Connected then
begin
serversocket1.Socket.Connect(server);
end;
ServerSocket1.Socket.SendText(sendstr);
memo1.Lines.Add('发送'+sendstr);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
j:integer;
Rcvstr:string;
RcvBuf:array [1..8] of byte;
begin
ServerSocket1.Socket.ReceiveBuf(RcvBuf,8);
for j:=1 to 8 do
begin
Rcvstr:=Rcvstr+' '+inttohex(RcvBuf[j],2);
end;
memo1.Lines.Add('接受'+Rcvstr);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ServerSocket1.Close;
end;
procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('等待建立TCP连接...');
end;
procedure TForm1.ServerSocket1GetSocket(Sender: TObject; Socket: Integer;
var ClientSocket: TServerClientWinSocket);
begin
memo1.Lines.Add('正在建立连接...');
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
memo1.Lines.Add('与'+Socket.RemoteHost +'链接发生错误!'+ inttostr(ErrorCode));
ErrorCode := 0;
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin
for i:=0 to sessions do
begin
if session.CHandle =Socket.SocketHandle then
begin
session.CHandle :=0;
session.CUsed := False;
Break;
end;
end;
memo1.Lines.Add('与 '+Socket.RemoteHost + '链接已经断开');
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j:integer;
begin
j:=-1;
for i:=0 to sessions do
begin
//在原有的客户端连接数组中有中断的客户端连接
if not session.CUsed then
begin
session.CHandle := Socket.SocketHandle ;//客户端套接字句柄
session.CSocket := Socket; //客户端套接字
session.CName := Socket.RemoteHost ; //客户端计算机名称
session.CAddress := Socket.RemoteAddress ;//客户端计算机IP
session.CUsed := True; //连接数组当前位置已经占用
Break;
end;
j:=i;
end;
if j=sessions then
begin
inc(sessions);
session[j].CHandle := Socket.SocketHandle ;
session[j].CSocket := Socket;
session[j].CName := Socket.RemoteHost ;
session[j].CAddress := Socket.RemoteAddress ;
session[j].CUsed := True;
end;
memo1.Lines.Add('与 '+Socket.RemoteHost + ' 已经建立连接');
//NMUDP1.Free;
TIMER1.Enabled:=false;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin
//将从客户端读取的信息添加到Memo1中
Memo1.Lines.Add('接受数据'+Socket.ReceiveText);
for i:=0 to sessions do
begin
//取得匹配的客户端
if session.CHandle = Socket.SocketHandle then
begin
session.CSocket.SendText('回复客户端'+session.CAddress+' ==> '+Edit1.Text);
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:integer;
MyStream: TStringStream;
TmpStr: String;
sendbuff:array[0..5] of byte;
begin
TmpStr:='';
NMUDP1.RemoteHost := edit1.text;
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.LocalPort :=strtoint(edit2.Text);
NMUDP1.RemotePort :=strtoint(edit2.Text);
sendbuff[0]:=byte($55);
sendbuff[1]:=byte($AA);
sendbuff[2]:=byte($55);
sendbuff[3]:=byte($AA);
sendbuff[4]:=byte($55);
sendbuff[5]:=byte($AA);
for i:=0 to 5 do
begin
TmpStr:=TmpStr+inttohex(sendbuff,2);
end;
MyStream := TStringStream.Create(TmpStr);
try
MyStream.Write(TmpStr[1], Length(TmpStr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
begin
NMUDP1.RemoteHost:=Edit1.Text;
Handled := TRUE;
end;
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
memo1.Lines.Add('UDP connecting...') ;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: Tstringstream;
TmpStr: String;
begin
FromIP:=NMUDP1.RemoteHost;
Port:=NMUDP1.RemotePort;
MyStream := Tstringstream.Create(TmpStr);
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
memo1.Lines.add('接受数据:来自'+FromIP+'/'+inttostr(Port)+' : '+TmpStr);
ServerSocket1.Active:=true;
finally
MyStream.Free;
Timer1.Enabled:=false;
end;
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
Timer1.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled:=false;
serversocket1.Active:=false;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
NMUDP1.Free;
end;
end.