Re: TCP Socket Question

From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 03/11/04


Date: Thu, 11 Mar 2004 03:58:37 GMT

Eric Enright wrote:

> 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;

Your host byte order differs from the network byte order. If you really
wanted 42 to be the server port, you should do:

  servAddr.sin_port = htons(42);

Looks like your host is Little-Endian, and 42 (00101010 00000000 in
little-endian byte order) becomes 10752 in network byte order.

> 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).
>

It should, if you tried to actually bind to port 42. However, the bind is
actually being on a different port above 1024.

> 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 guess since the class is about C++ and not about network programming, this
ignorance on the part of the instructor is OK. But, one would hope that a
person who has put the effort into creating an example would also put the
effort into understanding all aspects of it. Your curiosity OTOH is a good
habit that you should actively cultivate :-)

-nrk.

> I did this on Linux 2.6.3, if it matters.
>
>

-- 
Remove devnull for email


Relevant Pages

  • Re: Still cant connect to RWW or OWA remotely
    ... it certainly appears to be something about the SBS configuration. ... Meridian.local Ethernet adapter Local Area Connection: ... Windows SMALL BUSINESS SERVER 2003 Windows IP Configuration ... 192.168.254.254) directly to a port on the router and then ...
    (microsoft.public.windows.server.sbs)
  • Re: Still cant connect to RWW or OWA remotely
    ... it certainly appears to be something about the SBS configuration. ... Meridian.local Ethernet adapter Local Area Connection: ... Windows SMALL BUSINESS SERVER 2003 Windows IP Configuration ... 192.168.254.254) directly to a port on the router and then ...
    (microsoft.public.windows.server.sbs)
  • RE: VBscript Error on SBS2k3
    ... DHCP Server turned of SonicWALL with VPN Pass through request for IP to ... the problem should be caused by the 4125 port. ... > | Accessories and Communications and Remote Desktop Connection? ... > | 2.In Internet Explorer on the workstation you are connecting from, ...
    (microsoft.public.windows.server.sbs)
  • Re: interfaces lo:1 lo:2 lo:3? (for remote ssh tunnels)
    ... That's the problem tunneling (port forwarding) solves. ... >>can't get past the client firewall. ... > I don't understand why the server would be making the ... server initiates another connection to the client -- in this ...
    (Debian-User)
  • Re: Can not access Web and FTP sites from Internet
    ... your IP Configuration on the Server is correctly. ... Connecting To 12.208.215.87...Could not open connection to the host, ... 1> From the result, we can see the telnet failed, which means the router ... does not forward Port 443 to SBS Server. ...
    (microsoft.public.windows.server.sbs)