怎么让这3个winsock同时工作??(5分)

  • 主题发起人 主题发起人 ali_yan
  • 开始时间 开始时间
A

ali_yan

Unregistered / Unconfirmed
GUEST, unregistred user!
现在我的程序界面上有3个winsock,我有6个地址IP,用这3个winsock依次去连接这6个IP地址的客户端?
也就是最初开始时winsock1连IP1,winsock2连Ip2,winsock3连IP3然后等winsock1,winsock2,winsock3谁先结束通信后再接着连ip4,再有结束后连接IP5再结束后连接IP6都结束了。。提示结束??

这应该怎么来写??

或者有3个winsock,2个地址只接用winsock1连ip1,winsock2连ip2,通信结束后提示结束??

是不是必须用多线程。。。
可是我从来没写过多线程请大家帮帮我吧??

 
用多线程吧!
这是我写的程序的一部分:
-------------------
unit U_fileserver;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Psock, NMSTRM, DSIcon, NMUDP, Spin,inifiles, Menus;

type
TForm1 = class(TForm)
DSIcon1: TDSIcon;
UDPLIS: TNMUDP;
Edit1: TEdit;
Sport: TSpinEdit;
Slisport: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Open_b: TButton;
Open_f: TOpenDialog;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
procedure UDPLISDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Open_bClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
procedure setdefault;
public
{ Public declarations }
end;

type
Tmysocket = class(Tthread)
private
{ Private declarations }
MyStrm: TNMStrm;
Myudp: TNMUDP;
Myhost: string;
Myport,Mylisport: integer;
Myfile: string;
Myvdate: Tdatetime;
protected
procedure Execute;override;
public
{ Public declarations }
Constructor Create(vhost,vfile: string;vport,vlisport: integer;Vdate: Tdatetime);
end;

type
TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);

function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): TDateTime;

var
Form1: TForm1;
Verdate: Tdatetime;

implementation

{$R *.DFM}

{ Tmysocket }

constructor Tmysocket.Create(vhost, vfile: string; vport,vlisport: integer;Vdate: Tdatetime);
begin
inherited Create(false);
Myhost := vhost;
Myport := vport;
Myfile := vfile;
Mylisport := vlisport;
Myvdate := vdate;
end;

procedure Tmysocket.Execute;
var
MyFStream: TFileStream;
buff: Array [1..3] of Char;
begin
FreeOnTerminate:=True;
Myudp := TNMUDP.Create(nil);
try
Myudp.RemoteHost := Myhost;
Myudp.RemotePort := Mylisport;
if Myvdate = Verdate then
begin
buff := 'not';
Myudp.SendBuffer(buff,length('not'));
Myudp.Free;
Exit;
end
else
begin
buff := 'yes';
Myudp.SendBuffer(buff,length('yes'));
Myudp.Free;
end;
except
Myudp.Free;
end;
try
MyStrm := TNMStrm.create(nil);
MyStrm.host := Myhost;
MyStrm.port := Myport;
MyStrm.FromName := datetimetostr(Verdate);
MyFStream := TFileStream.Create(Myfile, fmShareDenyNone);
try
if not Terminated then
MyStrm.PostIt(MyFStream);
finally
MyFStream.Free;
end;
finally
MyStrm.free;
end;
end;
------------------------------------------------------------
procedure TForm1.UDPLISDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
datestr: string;
begin
MyStream := TMemoryStream.Create;
try
UDPLIS.ReadStream(MyStream);
SetLength(dateStr,NumberBytes);
MyStream.Read(dateStr[1],NumberBytes);
finally
MyStream.Free;
end;
Verdate := GetFileDateTime(edit1.text,fttLastWrite);
TMysocket.Create(FromIp,edit1.text,Sport.Value,Port,strtodatetime(datestr));
end;
 
你是用的UDP吧
 

Similar threads

S
回复
0
查看
745
SUNSTONE的Delphi笔记
S
S
回复
0
查看
754
SUNSTONE的Delphi笔记
S
后退
顶部