Re: TCP Socket Question
From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 03/11/04
- Next message: Allan Chandler: "Re: TCP Socket Question"
- Previous message: Paul Pluzhnikov: "Re: Shared Library Question"
- In reply to: Eric Enright: "TCP Socket Question"
- Next in thread: Allan Chandler: "Re: TCP Socket Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Allan Chandler: "Re: TCP Socket Question"
- Previous message: Paul Pluzhnikov: "Re: Shared Library Question"
- In reply to: Eric Enright: "TCP Socket Question"
- Next in thread: Allan Chandler: "Re: TCP Socket Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|