socket请求怎么写( 二 )


Cused记录当前客户端是否与服务器端保持连接 。下面对组件ServerSocket和ClientSocket的属性设置简单说明 。
ServerSocket的属性: · Port,是通信的端口,必须设置 。在本例中设置为1025; · ServerTypt,服务器端读写信息类型,设置为stNonBlocking表示异步读写信息,本例中采用这种方式 。
· ThreadCacheSize,客户端的最大连接数,就是服务器端最多允许多少客户端同时连接 。本例采用默认值10 。
其它属性采用默认设置即可 。ClientSocket的属性: · Port,是通信的端口,必须与服务器端的设置相同 。
在本例中设置为1025; · ClientType,客户端读写信息类型,应该与服务器端的设置相同,为stNonBlocking表示异步读写信息 。· Host,客户端要连接的服务器的IP地址 。
必须设置,当然也可以在代码中动态设置 。其它属性采用默认设置即可 。
程序源代码: · 服务器端源码(uServerMain.pas):unit uServerMain;interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons; const CMax=10; //客户端最大连接数 type client_record=record CHandle: integer; //客户端套接字句柄 CSocket:TCustomWinSocket; //客户端套接字 CName:string; //客户端计算机名称 CAddress:string; //客户端计算机IP地址 CUsed: boolean; //客户端联机标志 end; type TfrmServerMain = class(TForm) ServerSocket: TServerSocket; ControlBar1: TControlBar; ToolBar1: TToolBar; tbConnect: TToolButton; tbClose: TToolButton; tbDisconnected: TToolButton; Edit1: TEdit; Memo1: TMemo; StatusBar: TStatusBar; procedure tbConnectClick(Sender: TObject); procedure tbDisconnectedClick(Sender: TObject); procedure (Sender: TObject;Socket: TCustomWinSocket); procedure ServerSocketListen(Sender: TObject;Socket: TCustomWinSocket); procedure (Sender: TObject;Socket: TCustomWinSocket); procedure (Sender: TObject;Socket: TCustomWinSocket); procedure tbCloseClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ServerSocketGetSocket(Sender: TObject; Socket: Integer; var ClientSocket: ); procedure (Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); private { Private declarations } public { Public declarations } session: array[0..CMax] of client_record; //客户端连接数组 Sessions: integer; //客户端连接数 end; var frmServerMain: TfrmServerMain; implementation {$R *.DFM} //打开套接字连接,并使套接字进入监听状态 procedure TfrmServerMain.tbConnectClick(Sender: TObject); begin ServerSocket.Open ; end; //关闭套接字连接,不再监听客户端的请求 procedure TfrmServerMain.tbDisconnectedClick(Sender: TObject); begin ServerSocket.Close; StatusBar.Panels[0].Text :='服务器套接字连接已经关闭,无法接受客户端的连接请求.'; end; //从客户端读取信息 procedure TfrmServerMain.(Sender: TObject;Socket: TCustomWinSocket); var i:integer; begin //将从客户端读取的信息添加到Memo1中 Memo1.Lines.Add(Socket.ReceiveText); for i:=0 to sessions do begin //取得匹配的客户端 if session[i].CHandle = Socket.SocketHandle then begin session[i].CSocket.SendText('回复客户端'+session[i].CAddress+' ==> '+Edit1.Text); end; end; end; //服务器端套接字进入监听状态,以便监听客户端的连接 procedure TfrmServerMain.ServerSocketListen(Sender: TObject;Socket: TCustomWinSocket); begin StatusBar.Panels[0].Text :='等待客户端连接 。'; end; //当客户端连接到服务器端以后 procedure TfrmServerMain 。
6.编写一个TCP Socket程序我看盗的时候只有21个小时了,不知道是不是还能赶的上拿分!下边给出DELPHI的源代码!--------client---------------------------unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button1: TButton; IdTCPClient1: TIdTCPClient; procedure Button1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginif idtcpclient1.Connected then idtcpclient1.Disconnect ;tryidtcpclient1.Host :=edit1.Text ;idtcpclient1.Port :=strtoint(edit2.Text );idtcpclient1.Connect;exceptshowmessage('连接失败');end;idtcpclient1.WriteLn(edit3.Text );edit3.Text :=idtcpclient1.ReadLn; end;procedure TForm1.FormDestroy(Sender: TObject);beginif idtcpclient1.Connected then idtcpclient1.Disconnect ;end;end.----------------------------------------------------------server--------------------------------unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;type TForm1 = class(TForm) IdTCPServer1: TIdTCPServer; Button1: TButton; Edit1: TEdit; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure IdTCPServer1Execute(AThread: TIdPeerThread); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1 : TForm1; i:integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin if IdTCPServer1.Active then IdTCPServer1.Active := false; try IdTCPServer1.DefaultPort := strtoint(Edit1.Text); IdTCPServer1.Active := true; except showmessage('启动失败'); end;end;procedure TForm1.FormDestroy(Sender: TObject);begin if IdTCPServer1.Active then IdTCPServer1.Active := false;end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);var s : string;begin Memo1.Lines.Add(athread.Connection.ReadLn(s) ); ATHREAD.Connection.WriteLn(inttostr(i)); i:=i+1;end;procedure TForm1.FormCreate(Sender: TObject);begin i:=1;end;end.----------------------------------------------winXP +delphi7编译通过 。