B
baiduan
Unregistered / Unconfirmed
GUEST, unregistred user!
代码: server unit;
unit G_unit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls,StdCtrls,idwinsock2;
{主线程}
type
TmainThread =class (Tthread)
private
mainFMhandle:Thandle;
{ Private declarations }
public
procedure Execute;override;
constructor create(CreateSuspended: Boolean;HD:Thandle);
end;
{工作线程}
TworkThread=class(Tthread)
private
mainFMhandle:Thandle;
clientSock:Tsocket;
public
function setClientSock(c:Tsocket):integer;
procedure Execute;override;
constructor create(CreateSuspended: Boolean;HD:Thandle);
end;
type
threadSet=record
count:integer;
list:array of TworkThread;
end;
{管理线程}
type
TmangerThread=class(Tthread)
private
mainFMhandle:Thandle;
workThreadlist:ThreadSet;
public
constructor create(createSuspended:boolean;HD:thandle;ls:ThreadSet);
procedure Execute;override;
end;
function insertFDset(fds:Tfdset;sock:Tsocket):integer;
function clearSocket(FDs:Tfdset;csock:Tsocket):integer;
var
CsockNum:integer;
Sockset:Tfdset;
sock:Tsocket;
ghmutex:Thandle;
implementation
function clearSocket(FDs:Tfdset;csock:Tsocket):integer;
var
i:integer;
dresultword;
begin
result:=-1;
while true do
begin
dresult:=WaitForSingleObject(ghmutex,INFINITE);
if dresult=WAIT_OBJECT_0 then
begin
for i:=0 to fd_setsize-1 do
begin
if fds.fd_array=csock then
begin
fd_clr(csock,fds);
closesocket(csock);
result:=i+1;
exit;
end;
end;
end;
end;
end;
function insertFDset(fds:Tfdset;sock:Tsocket):integer;
var
i:integer;
dresultword;
begin
result:=-1;
while true do
begin
dresult:=WaitForSingleObject(ghmutex,INFINITE);
if dresult=WAIT_OBJECT_0 then
begin
for i:=0 to fd_setsize-1 do
begin
if fds.fd_array=invalid_socket then
begin
fd_set(sock,fds);
result:=i+1;
exit;
end;
end;
end;
end;
end;
{ TmainThread }
//=======================================================
//主线程负责接受socket,如果连接数64则关闭连接
//=======================================================
constructor TmainThread.create(CreateSuspended: Boolean; HD: Thandle);
begin
self.mainFMhandle:=hd;
inherited create(createSuspended);
end;
procedure TmainThread.Execute;
var
qutoSock:word;
RU:integer;
wdate:TWSADATA;
hostName:array [0..255]of char;
HEntHOSTENT;
ipstr:string;
sdrsockaddr;
Csock:Tsocket;
buf:array [0..2048] of char;
i:integer;
s:string;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
s:='WSAStartup 初始化失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
Exit;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
s:='WSAStartup 版本不一致';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;}
RU:=gethostname(@hostname,256);
if RU<>0 then
begin
s:='Gethostname 失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;
HEnt:=gethostbyname(@hostname);
ipstr:=inet_ntoa(Pinaddr(HEnt.h_address_list^)^);//取得pcIP地址
sdr:=new(psockaddr);
sdr.sin_addr.S_addr:=inet_addr(Pchar(ipstr));
sdr.sin_family:=2;
sdr.sin_port:=htons(27815);
fd_zero(sockSet);
for I:=0 to fd_setsize-1 do
begin
sockSet.fd_array:=invalid_socket;
end;
sock:=socket(2,1,0);
bind(sock,sdr,sizeof(Tsockaddr));
listen(sock,5);
while true do
begin
csock:=accept(sock,nil,nil);
// RU:=insertFDset(sockSet,csock);
fd_set(csock,sockset);
if RU=-1 then
begin
s:='服务器忙';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
closesocket(csock);
end else
begin
s:='客户端进入任务序列';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
end;
end;
// PostThreadMessage
{ while(true) do
begin
Csock:=accept(sock,nil,nil);
if (Csock=0)or(Csock=-1) then
begin
continue;
end;
RU:=1;
while RU>0 do
begin
RU:=recv(Csock,buf,sizeof(buf),0);
showmessage(strpas(buf));
end;
exit;
end;}
finally
closeSocket(sock);
wsaCleanup();
end;
end;
{ TmangerThread }
//====================================================================
//管理者线程负责select可用的套接字,然后选择一个工作者线程让他完成工作
//====================================================================
constructor TmangerThread.create(createSuspended: boolean; HD: thandle;
ls: ThreadSet);
begin
self.mainFMhandle:=hd;
self.workThreadlist:=ls;
inherited create(createSuspended);
end;
procedure TmangerThread.Execute;
var
i,j:integer;
qutoSock:word;
RU:integer;
wdate:TWSADATA;
s:string;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
s:='WSAStartup 初始化失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
Exit;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
s:='WSAStartup 版本不一致';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;}
while not Terminated do
begin
select(0,@sockSet,nil,nil,nil);
while true do
begin
for j:=0 to fd_setsize-1 do
begin
if (sockSet.fd_array[j]=invalid_socket) or
(sockSet.fd_array[j]=-1 )then
begin
continue;
end;
if j>=fd_setsize-1 then break;
I:=0;
while true do
begin
if (workThreadlist.list.Suspended) then
begin
workThreadlist.list.setClientSock(sockSet.fd_array[j]);
workThreadlist.list.Resume;
break;
end;
inc(i);
if i>=workThreadlist.count-1 then
begin
sleep(1000);
i:=0;
end;
end;
sleep(3000);
break;
end;
end;
end;
finally
wsaCleanup();
end;
end;
{ TworkThread }
//=================================================================
//工作者线程主要接受accept,完成后并将FD_set相应的socket置为无效
//=================================================================
constructor TworkThread.create(CreateSuspended: Boolean; HD: Thandle);
begin
self.mainFMhandle:=hd;
inherited create(createSuspended);
end;
procedure TworkThread.Execute;
var
qutoSock:word;
RU:integer;
wdate:TWSADATA;
s:string;
i:integer;
buf:array [0..2048] of char;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
self.Terminate;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
self.Terminate;
end; }
//CreateMutex
while not Terminated do
begin
if (self.clientSock=0) or (self.clientSock=-1) then
begin
clearSocket(sockset,self.clientsock);
self.Suspend;
end;
{如果在列表里}
if fd_isset(self.clientSock,sockset) then
begin
RU:=1;
while RU>0 do
begin
RU:=recv(self.clientSock,buf,sizeof(buf),0);
s:=strpas(buf);
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
if RU<=0 then
begin
clearSocket(sockset,self.clientSock);
s:='客户端'+inttostr(self.clientSock)+'结束';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
closeSocket(self.clientSock);
end;
end;
end;
self.Suspend;
end;
finally
wsaCleanup();
end;
end;
function TworkThread.setClientSock(c: Tsocket): integer;
begin
self.clientSock:=c;
end;
end.
======================================分割================================
客户端:
var
FStream:TFileStream;
begin
//
try
c1.Connect(5000);
if c1.Connected then
begin
try
FStream:=TfileStream.Create('C:/1.txt',fmOpenRead);
FStream.Position:=0;
FStream.Seek(0,0);
c1.WriteStream(Fstream,true,false);
finally
FStream.Free;
c1.Disconnect;
end;
end
else
showmessage('连接失败');
except
showmessage('error'+' '+inttostr(getlasterror));
end;
end;
==========================分割==========================================
问题:
开了5个workThread,1 mainThread,1 mangerThread,
第一次传输文件走通,然后关闭后重新打开客户端就出现
10057错误.
那为老大知道为什么啊....
unit G_unit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls,StdCtrls,idwinsock2;
{主线程}
type
TmainThread =class (Tthread)
private
mainFMhandle:Thandle;
{ Private declarations }
public
procedure Execute;override;
constructor create(CreateSuspended: Boolean;HD:Thandle);
end;
{工作线程}
TworkThread=class(Tthread)
private
mainFMhandle:Thandle;
clientSock:Tsocket;
public
function setClientSock(c:Tsocket):integer;
procedure Execute;override;
constructor create(CreateSuspended: Boolean;HD:Thandle);
end;
type
threadSet=record
count:integer;
list:array of TworkThread;
end;
{管理线程}
type
TmangerThread=class(Tthread)
private
mainFMhandle:Thandle;
workThreadlist:ThreadSet;
public
constructor create(createSuspended:boolean;HD:thandle;ls:ThreadSet);
procedure Execute;override;
end;
function insertFDset(fds:Tfdset;sock:Tsocket):integer;
function clearSocket(FDs:Tfdset;csock:Tsocket):integer;
var
CsockNum:integer;
Sockset:Tfdset;
sock:Tsocket;
ghmutex:Thandle;
implementation
function clearSocket(FDs:Tfdset;csock:Tsocket):integer;
var
i:integer;
dresultword;
begin
result:=-1;
while true do
begin
dresult:=WaitForSingleObject(ghmutex,INFINITE);
if dresult=WAIT_OBJECT_0 then
begin
for i:=0 to fd_setsize-1 do
begin
if fds.fd_array=csock then
begin
fd_clr(csock,fds);
closesocket(csock);
result:=i+1;
exit;
end;
end;
end;
end;
end;
function insertFDset(fds:Tfdset;sock:Tsocket):integer;
var
i:integer;
dresultword;
begin
result:=-1;
while true do
begin
dresult:=WaitForSingleObject(ghmutex,INFINITE);
if dresult=WAIT_OBJECT_0 then
begin
for i:=0 to fd_setsize-1 do
begin
if fds.fd_array=invalid_socket then
begin
fd_set(sock,fds);
result:=i+1;
exit;
end;
end;
end;
end;
end;
{ TmainThread }
//=======================================================
//主线程负责接受socket,如果连接数64则关闭连接
//=======================================================
constructor TmainThread.create(CreateSuspended: Boolean; HD: Thandle);
begin
self.mainFMhandle:=hd;
inherited create(createSuspended);
end;
procedure TmainThread.Execute;
var
qutoSock:word;
RU:integer;
wdate:TWSADATA;
hostName:array [0..255]of char;
HEntHOSTENT;
ipstr:string;
sdrsockaddr;
Csock:Tsocket;
buf:array [0..2048] of char;
i:integer;
s:string;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
s:='WSAStartup 初始化失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
Exit;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
s:='WSAStartup 版本不一致';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;}
RU:=gethostname(@hostname,256);
if RU<>0 then
begin
s:='Gethostname 失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;
HEnt:=gethostbyname(@hostname);
ipstr:=inet_ntoa(Pinaddr(HEnt.h_address_list^)^);//取得pcIP地址
sdr:=new(psockaddr);
sdr.sin_addr.S_addr:=inet_addr(Pchar(ipstr));
sdr.sin_family:=2;
sdr.sin_port:=htons(27815);
fd_zero(sockSet);
for I:=0 to fd_setsize-1 do
begin
sockSet.fd_array:=invalid_socket;
end;
sock:=socket(2,1,0);
bind(sock,sdr,sizeof(Tsockaddr));
listen(sock,5);
while true do
begin
csock:=accept(sock,nil,nil);
// RU:=insertFDset(sockSet,csock);
fd_set(csock,sockset);
if RU=-1 then
begin
s:='服务器忙';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
closesocket(csock);
end else
begin
s:='客户端进入任务序列';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
end;
end;
// PostThreadMessage
{ while(true) do
begin
Csock:=accept(sock,nil,nil);
if (Csock=0)or(Csock=-1) then
begin
continue;
end;
RU:=1;
while RU>0 do
begin
RU:=recv(Csock,buf,sizeof(buf),0);
showmessage(strpas(buf));
end;
exit;
end;}
finally
closeSocket(sock);
wsaCleanup();
end;
end;
{ TmangerThread }
//====================================================================
//管理者线程负责select可用的套接字,然后选择一个工作者线程让他完成工作
//====================================================================
constructor TmangerThread.create(createSuspended: boolean; HD: thandle;
ls: ThreadSet);
begin
self.mainFMhandle:=hd;
self.workThreadlist:=ls;
inherited create(createSuspended);
end;
procedure TmangerThread.Execute;
var
i,j:integer;
qutoSock:word;
RU:integer;
wdate:TWSADATA;
s:string;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
s:='WSAStartup 初始化失败';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
Exit;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
s:='WSAStartup 版本不一致';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)));
exit;
end;}
while not Terminated do
begin
select(0,@sockSet,nil,nil,nil);
while true do
begin
for j:=0 to fd_setsize-1 do
begin
if (sockSet.fd_array[j]=invalid_socket) or
(sockSet.fd_array[j]=-1 )then
begin
continue;
end;
if j>=fd_setsize-1 then break;
I:=0;
while true do
begin
if (workThreadlist.list.Suspended) then
begin
workThreadlist.list.setClientSock(sockSet.fd_array[j]);
workThreadlist.list.Resume;
break;
end;
inc(i);
if i>=workThreadlist.count-1 then
begin
sleep(1000);
i:=0;
end;
end;
sleep(3000);
break;
end;
end;
end;
finally
wsaCleanup();
end;
end;
{ TworkThread }
//=================================================================
//工作者线程主要接受accept,完成后并将FD_set相应的socket置为无效
//=================================================================
constructor TworkThread.create(CreateSuspended: Boolean; HD: Thandle);
begin
self.mainFMhandle:=hd;
inherited create(createSuspended);
end;
procedure TworkThread.Execute;
var
qutoSock:word;
RU:integer;
wdate:TWSADATA;
s:string;
i:integer;
buf:array [0..2048] of char;
begin
try
qutosock:=makeword(2,2);
RU:=WSAStartup(qutosock,wdate);
if ru<>0 then
begin
self.Terminate;
end;
{if (LOBYTE(wdate.wVersion )<>2) then
begin
self.Terminate;
end; }
//CreateMutex
while not Terminated do
begin
if (self.clientSock=0) or (self.clientSock=-1) then
begin
clearSocket(sockset,self.clientsock);
self.Suspend;
end;
{如果在列表里}
if fd_isset(self.clientSock,sockset) then
begin
RU:=1;
while RU>0 do
begin
RU:=recv(self.clientSock,buf,sizeof(buf),0);
s:=strpas(buf);
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
if RU<=0 then
begin
clearSocket(sockset,self.clientSock);
s:='客户端'+inttostr(self.clientSock)+'结束';
postmessage(self.mainFMhandle,wm_char,0,integer(pchar(s)) );
closeSocket(self.clientSock);
end;
end;
end;
self.Suspend;
end;
finally
wsaCleanup();
end;
end;
function TworkThread.setClientSock(c: Tsocket): integer;
begin
self.clientSock:=c;
end;
end.
======================================分割================================
客户端:
var
FStream:TFileStream;
begin
//
try
c1.Connect(5000);
if c1.Connected then
begin
try
FStream:=TfileStream.Create('C:/1.txt',fmOpenRead);
FStream.Position:=0;
FStream.Seek(0,0);
c1.WriteStream(Fstream,true,false);
finally
FStream.Free;
c1.Disconnect;
end;
end
else
showmessage('连接失败');
except
showmessage('error'+' '+inttostr(getlasterror));
end;
end;
==========================分割==========================================
问题:
开了5个workThread,1 mainThread,1 mangerThread,
第一次传输文件走通,然后关闭后重新打开客户端就出现
10057错误.
那为老大知道为什么啊....