TCP Socket Question

From: Eric Enright (eric_at_tiptsoft.com)
Date: 03/11/04


Date: Thu, 11 Mar 2004 02:36:08 GMT

Hello everyone,

Earlier today in my C++ class, our instructor introduced us to sockets.
This is nothing new to me, as I have experience with them from my personal
studies, and the following code seems strange to me...

protoent *protTableEnt = getprotobyname("tcp");
int socketDescriptor = socket(PF_INET, SOCK_STREAM, protTableEnt->p_proto);

sockaddr_in servAddr = {0};
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = INADDR_ANY;
servAddr.sin_port = (u_short) 42;
bind(socketDescriptor, (sockaddr*)&servAddr, sizeof(servAddr));

It opens a TCP socket on port 42, correct?
It's taken from a sample server, which sends the client a simple text
string then closes the connection.

First of all, shouldn't bind() fail when running that as a regular user?
(It doesn't).

Second, only the client connects to it correctly; telnet fails.
The following is an unedited shell transcript:

eric@lazybone:~/src/ss $ ./client
This server and has been used by 1 users.
eric@lazybone:~/src/ss $ ./client
This server and has been used by 2 users.
eric@lazybone:~/src/ss $ telnet localhost 42
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
eric@lazybone:~/src/ss $ netstat --inet -nap | grep server
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
tcp 0 0 0.0.0.0:10752 0.0.0.0:* LISTEN 1707/server
eric@lazybone:~/src/ss $ telnet localhost 10752
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
This server and has been used by 3 users.
Connection closed by foreign host.
eric@lazybone:~/src/ss $

I asked my instructor about this, and he seemed to be at a loss to explain
it as well.

I did this on Linux 2.6.3, if it matters.

-- 
Eric Enright                   /"\
sauronAtiptsoftDcom            \ /    ASCII Ribbon Campaign
                                X      Against HTML E-Mail
Public Key: 0xBEDF636F         / \


Relevant Pages

  • Socket communication on multihomed box
    ... I'm building a kind of server which is supposed to handle ... The thing is that connection MUST always stay open. ... as for sockets, I'm using native syscalls, not MFC. ... Another thing is that is should work both under Linux and Windows. ...
    (microsoft.public.win32.programmer.networks)
  • Socket communication on multihomed box
    ... I'm building a kind of server which is supposed to handle ... The thing is that connection MUST always stay open. ... as for sockets, I'm using native syscalls, not MFC. ... Another thing is that is should work both under Linux and Windows. ...
    (comp.programming)
  • Re: Problem with a Socket server program opening/accepting many connections and the GC is running.
    ... I'm not quite sure what you mean by "slowed down my server by adding a 10ms ... to handle the connection as quickly as possible? ... other clients requests are being serviced. ... >> available sockets. ...
    (microsoft.public.dotnet.framework.performance)
  • My server sometimes goes deaf to certain hosts
    ... For some reason my server is sporadically ignoring incoming ... they get "connection refused". ... I'm wondering if somehow these Unix sockets I've created might ...
    (comp.os.linux.development.apps)
  • Mysql broker since FreeBSD update
    ... I updated a FreeBSD 5.2.1 server to HEAD. ... -bash-2.05b$ telnet localhost 3306 ... Connection closed by foreign host. ...
    (comp.unix.bsd.freebsd.misc)