D
dxwleaf
Unregistered / Unconfirmed
GUEST, unregistred user!
最近在写一网络通讯的小程序,可以前没接触过这方面的
东东,现在有一主要东要搞不清楚:
1。服务器怎样才能不间断的监听到来自客户的连接信息,
并进行相应的处理之后发消息给客户断!(目前自己用API
写的一个server和client 可是只能接受到客户的一次请
求,可是下次客户再连接的时候,服务端不会再响应它的
连接了,不知怎样去处理它,听一老兄说在ACCEPT时也要用
线程,可做的不对,不知为什么?
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, Winsock, ScktComp, StdCtrls;
type
TLoginFlag = record
ComputerName: array[0..50] of char;
ComputerIP: array[0..20] of char;
PepoNo: array[0..10] of char;
StartTime: array[0..20] of char;
end;
var
ClientId: integer;
type
RecvClintData = class(TThread)
protected
procedure Execute; override;
procedure ClientDataproce;
public
constructor Create(ID: integer); virtual;
end;
procedure TComputerServer.ServerConnct;
var
Ca: SockADDR_IN;
begin
Server := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if Server = INVALID_SOCKET then
begin
SHOWMESSage('创建SOCKET错误');
exit;
end;
Ca.sin_family := PF_INET;
Ca.sin_port := htons(Strtoint(trim('111')));
Ca.sin_addr.S_addr := INADDR_ANY;
if Bind(Server, Ca, sizeof(ca)) = SOCKET_ERROR then
begin
Showmessage('绑定socket错误');
Closesocket(server);
exit;
end
else
showmessage('绑定接收端socket成功');
Listen(Server, 5);
ClientDataRece;
end;
procedure TComputerServer.ClientDataRece;
const ClientLen = 255;
var
ReceSocket: TSocket;
Ra: SockAddr_IN;
Ra_len, RecLen, i_recvLength: Integer;
ClientMsg: array of char;
T1, T2, T3, T4, T5: string;
RecVLoginData: TLoginFlag;
OldTime: Cardinal;
c_result: array[0..50] of char;
str: string;
Flag: BOOLEAN;
begin
ReceSocket := 1;
Ra_Len := Sizeof(Ra);
ClientMsg := nil;
Setlength(ClientMsg, 255);
Flag := true;
//while ReceSocket > 0 do
begin
ReceSocket := Accept(Server, @Ra, @Ra_Len);
// TAcceptData := TAccepThread.Create;
if ReceSocket = INVALID_SOCKET then
EXIT
else
TClientData := RecvClintData.Create(Recesocket);
end;
end;
在 这个TClientData := RecvClintData.Create(Recesocket) 线程
中处理的是接收的客户数据,各位如有需要也可一并放上来),本人现在主要
不知怎样去让它去随时响应客户
东东,现在有一主要东要搞不清楚:
1。服务器怎样才能不间断的监听到来自客户的连接信息,
并进行相应的处理之后发消息给客户断!(目前自己用API
写的一个server和client 可是只能接受到客户的一次请
求,可是下次客户再连接的时候,服务端不会再响应它的
连接了,不知怎样去处理它,听一老兄说在ACCEPT时也要用
线程,可做的不对,不知为什么?
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, Winsock, ScktComp, StdCtrls;
type
TLoginFlag = record
ComputerName: array[0..50] of char;
ComputerIP: array[0..20] of char;
PepoNo: array[0..10] of char;
StartTime: array[0..20] of char;
end;
var
ClientId: integer;
type
RecvClintData = class(TThread)
protected
procedure Execute; override;
procedure ClientDataproce;
public
constructor Create(ID: integer); virtual;
end;
procedure TComputerServer.ServerConnct;
var
Ca: SockADDR_IN;
begin
Server := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if Server = INVALID_SOCKET then
begin
SHOWMESSage('创建SOCKET错误');
exit;
end;
Ca.sin_family := PF_INET;
Ca.sin_port := htons(Strtoint(trim('111')));
Ca.sin_addr.S_addr := INADDR_ANY;
if Bind(Server, Ca, sizeof(ca)) = SOCKET_ERROR then
begin
Showmessage('绑定socket错误');
Closesocket(server);
exit;
end
else
showmessage('绑定接收端socket成功');
Listen(Server, 5);
ClientDataRece;
end;
procedure TComputerServer.ClientDataRece;
const ClientLen = 255;
var
ReceSocket: TSocket;
Ra: SockAddr_IN;
Ra_len, RecLen, i_recvLength: Integer;
ClientMsg: array of char;
T1, T2, T3, T4, T5: string;
RecVLoginData: TLoginFlag;
OldTime: Cardinal;
c_result: array[0..50] of char;
str: string;
Flag: BOOLEAN;
begin
ReceSocket := 1;
Ra_Len := Sizeof(Ra);
ClientMsg := nil;
Setlength(ClientMsg, 255);
Flag := true;
//while ReceSocket > 0 do
begin
ReceSocket := Accept(Server, @Ra, @Ra_Len);
// TAcceptData := TAccepThread.Create;
if ReceSocket = INVALID_SOCKET then
EXIT
else
TClientData := RecvClintData.Create(Recesocket);
end;
end;
在 这个TClientData := RecvClintData.Create(Recesocket) 线程
中处理的是接收的客户数据,各位如有需要也可一并放上来),本人现在主要
不知怎样去让它去随时响应客户