|
From: Minimiscience on 1 Jul 2008 18:54 There are a couple of minor things about creating a TCP connection in C that I've been wondering about. They're probably of little consequence, but just to make sure... Firstly, after creating the socket, is it strictly necessary to bind it to a local address before connecting to the remote server? The tutorial from which I learned the basics of TCP programming [1] states that you should, yet the example code in my operating system's manpage for getaddrinfo(3) only uses it when creating a program to listen on a port. If all I'm doing is creating a client program to send & receive bytes, what effect, if any, does binding the socket have? Secondly, when using getaddrinfo(3) to obtain addressing information, what is the preferred way to specify a preference for TCP rather than UDP? The example code on my OS simply sets the `ai_socktype' element of `hints' (the third argument) to `SOCK_STREAM', yet I don't see why setting `ai_protocol' to `IPPROTO_TCP' wouldn't be a valid alternative. Would it also be acceptable to use both settings at once? Just wondering, -- Minimiscience [1] http://www.fortunecity.com/skyscraper/arpanet/6/cc.htm -- There are some things in ascensions zorkmids can't buy. For everything else, there's Platinum Yendorian Express Card.
From: Barry Margolin on 2 Jul 2008 00:56 In article <g4ecj9$1h6$1(a)chessie.cirr.com>, Minimiscience <minimiscience+usenet(a)gmail.com> wrote: > There are a couple of minor things about creating a TCP connection in C that > I've been wondering about. They're probably of little consequence, but just > to > make sure... > > Firstly, after creating the socket, is it strictly necessary to bind it to a > local address before connecting to the remote server? The tutorial from > which > I learned the basics of TCP programming [1] states that you should, yet the > example code in my operating system's manpage for getaddrinfo(3) only uses it > when creating a program to listen on a port. If all I'm doing is creating a > client program to send & receive bytes, what effect, if any, does binding the > socket have? It's rarely needed, or even appropriate, to call bind() before calling connect() on a TCP socket. There are some application protocols which have rules about the source port of a connection, but these are rare, special cases. Usually you just let the OS select an unused port, which happens automatically when you call connect() without calling bind(). I took a quick glance at that tutorial, and it's really hard to read and doesn't explain what it's doing or why. > > Secondly, when using getaddrinfo(3) to obtain addressing information, what is > the preferred way to specify a preference for TCP rather than UDP? The > example > code on my OS simply sets the `ai_socktype' element of `hints' (the third > argument) to `SOCK_STREAM', yet I don't see why setting `ai_protocol' to > `IPPROTO_TCP' wouldn't be a valid alternative. Would it also be acceptable > to > use both settings at once? I think the intent is to allow the caller to be as generic or specific as it wishes to be. Using SOCK_STREAM rather than IPPROTO_TCP allows for future extension to other stream protocols than TCP. But if the rest of your code assumes TCP, it may be better to use IPPROTO_TCP to ensure that you don't get something you can't use. -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: Rainer Weikusat on 2 Jul 2008 01:54 Minimiscience <minimiscience+usenet(a)gmail.com> writes: > Firstly, after creating the socket, is it strictly necessary to bind > it to a local address before connecting to the remote server? The > tutorial from which I learned the basics of TCP programming [1] > states that you should, bind associates an existing socket with a specific protocol address. Calling connect with an unbound socket causes the kernel to select one it considers to be appropriate instead. > Secondly, when using getaddrinfo(3) to obtain addressing information, what is > the preferred way to specify a preference for TCP rather than UDP? > The example code on my OS simply sets the `ai_socktype' element of > `hints' (the third argument) to `SOCK_STREAM', yet I don't see why > setting `ai_protocol' to `IPPROTO_TCP' wouldn't be a valid > alternative. Would it also be acceptable to use both settings at > once? Presumably (I haven't tested this), you should rather use the latter if you really want a TCP-address. OTOH, there is little reason to do that: Applications usually rely on properties a transport protocol provides (like 'a reliable, full-duplex virtual circuit') and assuming there was another common protocol providing this except TCP, there shouldn't be a reason why an application should not be as capable to use that instead of TCP. That's why I would rather specify the type of connection I would like to make (eg 'SOCK_STREAM') than a particular protocol providing such a connection.
|
Pages: 1 Prev: problem expanding rpm macros Next: what 64 mean in fopen64? |