 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bonny Gijzen Guest
|
Posted: Fri Mar 25, 2005 10:15 am Post subject: Winsock UDP problem |
|
|
Hi guys,
I am using the following (winsock) code, to be able to handle incoming UDP
data packets.
Unfortunately, when data arrives, the recv (or recvfrom) function falls
through (I guess it was blocking), but resulting with a -1 value which
indicates an error.
When using WSAGetLastError() I get a value of 5.
Can somebody please have a look at my (simple) code to see if I made some
(obvious) mistake?
Var LAddrIn : TSockAddrIn;
Res, len : Integer;
ServSocket : TSocket;
FSockAddrIn : TSockAddrIn; // Address Information Block
Buf : Array[00..$FFF] of Byte;
begin
ServSocket := WinSock2.Socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
//0=IPPROTO_IP !
FillChar(LAddrIn, SizeOf(TSockAddrIn), 0);
LAddrIn.SIn_Family := AF_INET;
LAddrIn.SIn_Port := PortLookup('4000'); // Default behaviour for backward
compatibility
LAddrIn.SIn_Addr.S_Addr := HToNL(INADDR_ANY); // No HostLookup(...)
Because INADDR_ANY Is A Windows Constant
Res := WinSock2.Bind( ServSocket, Addr(LAddrIn), SizeOf(LAddrIn));
Memo1.Lines.Add('bind='+IntToStr(Res));
Res:=SizeOf(TSockAddrIn);
//len:=Winsock2.recv(ServSocket, Buf, 3, 0);
len:=Winsock2.recvfrom(ServSocket, Buf, 3, 0, FSockAddrIn, Res);
Memo1.Lines.Add('len='+IntToStr(Len));
Res:=WSAGetLastError();
Memo1.Lines.Add('Err='+IntToStr(Res));
while (len>0) do
begin
Memo1.Lines.Add('len='+IntToStr(Len));
len:=Winsock2.recv(ServSocket, Buf, 3, 0);
end;
Winsock2.closesocket(ServSocket);
end;
Thank you very much for having a look,
Bonny
|
|
| Back to top |
|
 |
Bonny Gijzen Guest
|
Posted: Fri Mar 25, 2005 10:19 am Post subject: Winsock UDP problem (addon) |
|
|
Hi,
I forgot to mention that I can successfully do TCP communications (which
I've used in other apps),
but for this project I wanted to use UDP but I cannot get it to work.
1) I have successfully initialized WSA (otherwise TCP would fail also)
2) The socket return value is >0 so OK
3) Bind result = 0, so OK
Thanks
Bonny
|
|
| Back to top |
|
 |
Francois PIETTE [ICS - Mi Guest
|
Posted: Fri Mar 25, 2005 5:49 pm Post subject: Re: Winsock UDP problem |
|
|
Why don't you use Indy or ICS ?
--
[email]francois.piette (AT) overbyte (DOT) be[/email]
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
"Bonny Gijzen" <bonny_dot_gijzen_at_q-lite_dot_net> a écrit dans le message
de news:4243e487 (AT) newsgroups (DOT) borland.com...
| Quote: | Hi guys,
I am using the following (winsock) code, to be able to handle incoming UDP
data packets.
Unfortunately, when data arrives, the recv (or recvfrom) function falls
through (I guess it was blocking), but resulting with a -1 value which
indicates an error.
When using WSAGetLastError() I get a value of 5.
Can somebody please have a look at my (simple) code to see if I made some
(obvious) mistake?
Var LAddrIn : TSockAddrIn;
Res, len : Integer;
ServSocket : TSocket;
FSockAddrIn : TSockAddrIn; // Address Information Block
Buf : Array[00..$FFF] of Byte;
begin
ServSocket := WinSock2.Socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
//0=IPPROTO_IP !
FillChar(LAddrIn, SizeOf(TSockAddrIn), 0);
LAddrIn.SIn_Family := AF_INET;
LAddrIn.SIn_Port := PortLookup('4000'); // Default behaviour for
backward
compatibility
LAddrIn.SIn_Addr.S_Addr := HToNL(INADDR_ANY); // No HostLookup(...)
Because INADDR_ANY Is A Windows Constant
Res := WinSock2.Bind( ServSocket, Addr(LAddrIn), SizeOf(LAddrIn));
Memo1.Lines.Add('bind='+IntToStr(Res));
Res:=SizeOf(TSockAddrIn);
//len:=Winsock2.recv(ServSocket, Buf, 3, 0);
len:=Winsock2.recvfrom(ServSocket, Buf, 3, 0, FSockAddrIn, Res);
Memo1.Lines.Add('len='+IntToStr(Len));
Res:=WSAGetLastError();
Memo1.Lines.Add('Err='+IntToStr(Res));
while (len>0) do
begin
Memo1.Lines.Add('len='+IntToStr(Len));
len:=Winsock2.recv(ServSocket, Buf, 3, 0);
end;
Winsock2.closesocket(ServSocket);
end;
Thank you very much for having a look,
Bonny
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Mar 25, 2005 6:43 pm Post subject: Re: Winsock UDP problem |
|
|
"Bonny Gijzen" <bonny_dot_gijzen_at_q-lite_dot_net> wrote
| Quote: | When using WSAGetLastError() I get a value of 5.
|
There is no error code 5 in socket programming. All socket errors start at
10000 minimum.
In Win32 API programming, error code 5 is ERROR_ACCESS_DENIED.
| Quote: | ServSocket := WinSock2.Socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
You did not check to see if ServSocket is being assiged a nil pointer.
| Quote: | LAddrIn.SIn_Addr.S_Addr := HToNL(INADDR_ANY); // No HostLookup(...)
|
You don't need ntonl() here. INADDR_ANY is defined as 0.
Gambit
|
|
| Back to top |
|
 |
Bonny Gijzen Guest
|
Posted: Fri Mar 25, 2005 7:11 pm Post subject: Re: Winsock UDP problem |
|
|
Hi,
| Quote: | There is no error code 5 in socket programming. All socket errors start
at
10000 minimum.
|
I know this, that is why it is confusing me.
| Quote: | In Win32 API programming, error code 5 is ERROR_ACCESS_DENIED.
ServSocket := WinSock2.Socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
You did not check to see if ServSocket is being assiged a nil pointer.
|
Well, I checked it in debugger, it is a positive value and similar to what I
get when creating a TCP socket,
so it is OK.
| Quote: | LAddrIn.SIn_Addr.S_Addr := HToNL(INADDR_ANY); // No
HostLookup(...)
You don't need ntonl() here. INADDR_ANY is defined as 0.
|
Ok, optimisation only.
This still doesn't give me the faintest idea of what could be wrong.
As I've said before, I use similar code to do TCP comms which works fine.
Is there anybode in this newsgroup who *does* have working winsock code for
UDP?
(preferably Delphi, but its easy to port C aswell)
Rgs Bonny,
|
|
| Back to top |
|
 |
Francois PIETTE [ICS - Mi Guest
|
Posted: Fri Mar 25, 2005 8:18 pm Post subject: Re: Winsock UDP problem |
|
|
| Quote: | Is there anybode in this newsgroup who *does* have working winsock code
for
UDP? (preferably Delphi, but its easy to port C aswell)
|
I have within TWSocket. No problem.
You can check ICS source code.
--
[email]francois.piette (AT) overbyte (DOT) be[/email]
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|