TCP Socket Question
From: Eric Enright (eric_at_tiptsoft.com)
Date: 03/11/04
- Next message: Paul Pluzhnikov: "Re: Shared Library Question"
- Previous message: Alan Coopersmith: "Re: Shared Library Question"
- Next in thread: nrk: "Re: TCP Socket Question"
- Reply: nrk: "Re: TCP Socket Question"
- Reply: Allan Chandler: "Re: TCP Socket Question"
- Reply: Eric Enright: "Re: TCP Socket Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 / \
- Next message: Paul Pluzhnikov: "Re: Shared Library Question"
- Previous message: Alan Coopersmith: "Re: Shared Library Question"
- Next in thread: nrk: "Re: TCP Socket Question"
- Reply: nrk: "Re: TCP Socket Question"
- Reply: Allan Chandler: "Re: TCP Socket Question"
- Reply: Eric Enright: "Re: TCP Socket Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|