A
Another_eYes
Unregistered / Unconfirmed
GUEST, unregistred user!
请问下面这段代码又什么问题?
为什么accept客户端连接后,客户端还没有发数据,wsarecv就报错:10045?
为什么accept客户端连接后,客户端还没有发数据,wsarecv就报错:10045?
代码:
procedure TForm1.FormCreate(Sender: TObject);
var
__wsadt: TWSAData;
begin
WSAStartup(MAKEWORD(2, 2), __wsadt);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
WSACleanUp;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s, s1, n, l, v: Integer;
saddr: TSockAddrIn;
ov: _OVERLAPPED;
buf: string;
begin
s := WSASocket(AF_INET, SOCK_STREAM, 0, nil, 0, 1);
saddr.sin_family := AF_INET;
saddr.sin_port := htons(10240);
saddr.sin_addr.S_addr := htonl(INADDR_ANY);
l := sizeof(saddr);
bind(s, @saddr, l);
listen(s, 5);
while true do
begin
s1 := wsaaccept(s, nil, nil, nil, 0);
setlength(buf, 1024);
fillchar(ov, sizeof(ov), #0);
if wsarecv(s1, pwsabuf(integer(buf)-4), 1, cardinal(l), cardinal(n), Pwsaoverlapped(@ov), nil) = SOCKET_ERROR then
begin
v := wsagetlasterror;
showmessage(inttostr(v));
end
else showmessage(copy(buf, 1, l));
end;
end;