BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Winsock UDP problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Bonny Gijzen
Guest





PostPosted: Fri Mar 25, 2005 10:15 am    Post subject: Winsock UDP problem Reply with 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
Bonny Gijzen
Guest





PostPosted: Fri Mar 25, 2005 10:19 am    Post subject: Winsock UDP problem (addon) Reply with quote



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





PostPosted: Fri Mar 25, 2005 5:49 pm    Post subject: Re: Winsock UDP problem Reply with quote



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





PostPosted: Fri Mar 25, 2005 6:43 pm    Post subject: Re: Winsock UDP problem Reply with quote


"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





PostPosted: Fri Mar 25, 2005 7:11 pm    Post subject: Re: Winsock UDP problem Reply with quote

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,

Quote:

Gambit





Back to top
Francois PIETTE [ICS - Mi
Guest





PostPosted: Fri Mar 25, 2005 8:18 pm    Post subject: Re: Winsock UDP problem Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.