Overview of Computer Networks

Start from the beginning
                                        

The function's return value, assigned to SD here, is a socket descriptor, quite analogous to a file descriptor.

Lines 41-54:

We are building up a data structure named Addr ("address") which we will use in line 51. Its type, sockaddr

in (line 31) comes from the #include file and is a standard socket type. Clearly, it is a complex type,

with many fields, and we will not go into the details here. See the man pages if you are interested for more

information.

One thing to beware of is that for the system call connect() and many other socket functions, you will see

that the man pages say you should use a struct of type sockaddr, as opposed to the sockaddr in type we

have used here. Yet the sockaddr type is just meant as a dummy, to be replaced by another struct type

which is specific to the network protocol being used. In our case, we are using TCP/IP, so we choose the

sockaddr in type, where "in" stands for "Internet." There are also types such as sockaddr ns for the Xerox

Networks Systems protocol, though of course TCP/IP has become virtually ubiquitous.

Having already opened a socket, we have to connect it to a specific port at a specific machine. Recall that

the user specifies an Internet host name, such as garnacha.engr.edu. But we need the machine's numerical

Internet address. This is obtained on line 46. The return value is a pointer to another standard data structure.

In line 47, we call memcopy(), a system call which copies strings from one part of memory to another, in

this case from various fields of the struct pointed to by HostPtr to Addr. A given host may have several

addresses; here we are not bothering to check for that, but simply using the first address, contained in

h addr list[0]. (Here 'h' stands for "host.") The h length field gives the length of the address.

In line 51, we now connect the socket to the destination host. (It is here that the negotiation between source

and destination hosts will occur, as to packet sizes, and so on.)

Note that the port number we have specified is for the port at the server, not the client. There is a hidden

port number for the client here, which we will discuss later.

Line 57:

21There are also various others, such as SOCK RAW for raw sockets.

17

Here we write either "w" or "ps" to the destination host, depending on what the user requested. Note that

the function write() is identical to the one used for low-level file access, except that we have as the first

parameter a socket descriptor instead of a file descriptor.

Lines 60-61:

On line 60 we read the message sent by the destination host, which will be the output of that host's running

either the w or ps command. On line 61 we then write that message to the user's screen. (For convenience,

we do so again using write(), making use of the fact that the standard output has file descriptor 1, though of

You've reached the end of published parts.

⏰ Last updated: May 09, 2009 ⏰

Add this story to your Library to get notified about new parts!

Overview of Computer NetworksWhere stories live. Discover now