Synapse <http://synapse.ararat.cz/doku.php/download>,is a great socklib. i
like it ,and use it often.
thanks,geby.
I have found a bug, this
procedure TForm1.sSock1ClientRead(Sender: TObject; Socket:
TCustomWinSocket);
var
s : string;
i : integer;
sk : TCustomWinSocket;
begin
s := Socket.ReceiveText;
//sSock1.Socket.Connections[0].SendText('zz');
if s = 'me' then
for i := 0 to sSock1.Socket.ActiveConnections - 1 do
begin
//if sSock1.Socket.Connections[i].Handle = Socket.Handle then
begin
mmo1.Lines.Add(IntToStr(sSock1.Socket.Connections[i].Handle) + '->No.'
+ IntToStr(i));
sSock1.Socket.Connections[i].SendText('SocketServer=> haha'#13#10);
end;
end;
end;
I bind a port of 127.0.0.1 :6306 with Tserversocket , with Tclientsocket,
it can recv "SocketServer=> haha" ,it hook fd_onread message in async modle.
with
"Synapse <http://synapse.ararat.cz/doku.php/download>" ,i expect sync modle.
var
rcv : string;
fsock : TTCPBlockSocket;
begin
fsock := TTCPBlockSocket.Create;
//fsock.CloseSocket;
//fsock.ConvertLineEnd := True;
//fsock.Bind(cAnyHost, cAnyPort);
//fsock.CloseSocket;
fsock.Connect('127.0.0.1', '6306');
fsock.SendString('me');
if (fsock.lasterror = 0) then
repeat
rcv := fsock.RecvString(1000);
mmo1.Lines.Add(rcv)
until (rcv <> '') or (fsock.lasterror <> 0);
//fsock.CloseSocket;
//fsock.Free;
It RECV NOTHING.
and i try use winsock api ,say so.
below is a test unit of winsockapi, can`t recv anything.
unit sockapi;
interface
uses Windows, WinSock2, SysUtils;
function connect(ip, port: string): Boolean;
function send(s: string): Boolean;
function recv(Timeout: integer): string;
function getRet(cmd: string): string;
var
skt : TSOCKET;
addr : TSockAddr;
WSAData : TWSAData;
hEvent : THandle;
implementation
function connect(ip, port: string): Boolean;
var
re : integer;
blocking : integer;
begin
Result := False;
skt := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if skt = INVALID_SOCKET then
exit;
blocking := 0;
IOCtlSocket(skt, FIONBIO, blocking);
hEvent := WSACreateEvent();
WSAEventSelect(skt, hEvent, FD_READ);
ZeroMemory(@addr, sizeof(addr));
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := inet_addr(pchar(ip));
addr.sin_port := htons(strtoint(port));
re := WinSock2.connect(skt, @addr, sizeof(addr));
Result := re <> 0;
end;
function send(s: string): Boolean;
var
re : integer;
buf : array[0..1023] of Char;
begin
Move(s[1], buf, Length(s));
Result := SOCKET_ERROR <> WinSock2.send(skt, buf, Length(s), 0);
//MessageBox(0,PChar(inttostr(WSAGetLastError)),'',0)
end;
function getRet(cmd: string): string;
var
buf : array[0..1023] of Char;
l : integer;
r:Cardinal;
begin
Move(cmd[1], buf, Length(cmd));
WinSock2.Send(skt, buf, Length(cmd), 0);
r:= WSAWaitForMultipleEvents(1, @hEvent, False, 1000, False) ;
case r of
WSA_WAIT_EVENT_0: //非-1即可。因为连接成功,所以这里自然调用getlasterror
;
begin
l := WinSock2.recv(skt, buf, 1024, MSG_PEEK);
SetString(Result, buf, l);
end;
Wait_TimeOut: begin
l := WinSock2.recv(skt, buf, 1024, MSG_PEEK);
SetString(Result, buf, l);
end; //超时。
else
begin
l := WinSock2.recv(skt, buf, 1024, MSG_PEEK);
SetString(Result, buf, l);
end;
end;
end;
function recv(Timeout: integer): string;
var
re : integer;
buf : array[0..1023] of Char;
l : integer;
SocketTimeout : WinSock2.TTimeVal;
SocketSet : WinSock2.TFDSet;
begin
SocketTimeout.tv_sec := Timeout div 1000;
SocketTimeout.tv_usec := 1000 * (Timeout mod 1000);
// Set the socket parameters
SocketSet.fd_count := 1;
SocketSet.fd_array[0] := skt;
if (WinSock2.Select(0, @SocketSet, nil, nil, @SocketTimeout) > 0) then
begin
l := WinSock2.recv(skt, buf, 1024, MSG_PEEK);
SetString(Result, buf, l);
end
end;
initialization
WSAStartup(makeword(2, 2), WSAData); //<>0)
finalization
WSACleanUp;
end.
------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
synalist-public mailing list
synalist-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/synalist-public