B
beamgx
Unregistered / Unconfirmed
GUEST, unregistred user!
UDP通信问题:
uses WinSock;
接收端:
var
s_addr:Tsockaddrin;
sock_c:Tsocket;
WSAData_c:TWSADATA;
ErrorCode: Integer;
buf:array [1..15] of char;
bufsize,a:integer;
s_port:word;
begin
for bufsize:=1 to 15 do buf[bufsize]:='1';
bufsize:=15;
ErrorCode := WSAStartup($0101, WSAData_c);
if ErrorCode <> 0 then showmessage('WSAStartup failed!');
s_port:=5555;
s_addr.sin_family :=AF_INET;
s_addr.sin_port:=htons(s_port);
s_addr.sin_addr.S_addr:=INADDR_ANY;
sock_c:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if sock_c=SOCKET_ERROR then showmessage('socket call failed!')
else begin
ErrorCode:=bind(sock_c,s_addr,sizeof(s_addr));
if ErrorCode<>0 then showmessage('bind call failed!')
else begin
ErrorCode:=recvfrom(sock_c,buf,bufsize,0,s_addr,a);
caption:=buf;
ErrorCode:=WSAGetLastError;
if ErrorCode<>0 then showmessage('recvfrom call failed!Error:'+inttostr(ErrorCode))
end;
end;
closesocket(sock_C);
ErrorCode := WSACleanup;
if ErrorCode <> 0 then showmessage('WSACleanup failed!');
end;
用Internet页的UDPSocket控件或TindyUDPClient发都能收到,这说明接收端正常,但是如果用以下方法发送就收不到。为什么?请大家指点指点
发送端:
var
s_addr:Tsockaddrin;
sock_c:Tsocket;
WSAData_c:TWSADATA;
ErrorCode: Integer;
buf:array [1..15] of char;
bufsize:integer;
s_port:word;
begin
for bufsize:=1 to 15 do buf[bufsize]:='1';
bufsize:=15;
ErrorCode := WSAStartup($0101, WSAData_c);
if ErrorCode <> 0 then showmessage('WSAStartup failed!');
s_port:=5555;
s_addr.sin_family :=AF_INET;
s_addr.sin_port:=htons(s_port);
s_addr.sin_addr.S_addr:=inet_addr('192.168.1.11');
sock_c:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if sock_c=SOCKET_ERROR then showmessage('socket call failed!')
else begin
ErrorCode:=bind(sock_c,s_addr,sizeof(s_addr));
if ErrorCode<>0 then showmessage('bind call failed!');
ErrorCode:=sendto(sock_c,buf,bufsize,0,s_addr,sizeof(s_addr));
if ErrorCode<>bufsize then showmessage('sendto failed!');;
end;
closesocket(sock_C);
ErrorCode := WSACleanup;
if ErrorCode <> 0 then showmessage('WSACleanup failed!');
end;
uses WinSock;
接收端:
var
s_addr:Tsockaddrin;
sock_c:Tsocket;
WSAData_c:TWSADATA;
ErrorCode: Integer;
buf:array [1..15] of char;
bufsize,a:integer;
s_port:word;
begin
for bufsize:=1 to 15 do buf[bufsize]:='1';
bufsize:=15;
ErrorCode := WSAStartup($0101, WSAData_c);
if ErrorCode <> 0 then showmessage('WSAStartup failed!');
s_port:=5555;
s_addr.sin_family :=AF_INET;
s_addr.sin_port:=htons(s_port);
s_addr.sin_addr.S_addr:=INADDR_ANY;
sock_c:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if sock_c=SOCKET_ERROR then showmessage('socket call failed!')
else begin
ErrorCode:=bind(sock_c,s_addr,sizeof(s_addr));
if ErrorCode<>0 then showmessage('bind call failed!')
else begin
ErrorCode:=recvfrom(sock_c,buf,bufsize,0,s_addr,a);
caption:=buf;
ErrorCode:=WSAGetLastError;
if ErrorCode<>0 then showmessage('recvfrom call failed!Error:'+inttostr(ErrorCode))
end;
end;
closesocket(sock_C);
ErrorCode := WSACleanup;
if ErrorCode <> 0 then showmessage('WSACleanup failed!');
end;
用Internet页的UDPSocket控件或TindyUDPClient发都能收到,这说明接收端正常,但是如果用以下方法发送就收不到。为什么?请大家指点指点
发送端:
var
s_addr:Tsockaddrin;
sock_c:Tsocket;
WSAData_c:TWSADATA;
ErrorCode: Integer;
buf:array [1..15] of char;
bufsize:integer;
s_port:word;
begin
for bufsize:=1 to 15 do buf[bufsize]:='1';
bufsize:=15;
ErrorCode := WSAStartup($0101, WSAData_c);
if ErrorCode <> 0 then showmessage('WSAStartup failed!');
s_port:=5555;
s_addr.sin_family :=AF_INET;
s_addr.sin_port:=htons(s_port);
s_addr.sin_addr.S_addr:=inet_addr('192.168.1.11');
sock_c:=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if sock_c=SOCKET_ERROR then showmessage('socket call failed!')
else begin
ErrorCode:=bind(sock_c,s_addr,sizeof(s_addr));
if ErrorCode<>0 then showmessage('bind call failed!');
ErrorCode:=sendto(sock_c,buf,bufsize,0,s_addr,sizeof(s_addr));
if ErrorCode<>bufsize then showmessage('sendto failed!');;
end;
closesocket(sock_C);
ErrorCode := WSACleanup;
if ErrorCode <> 0 then showmessage('WSACleanup failed!');
end;