Instructor: Engr. Sehar Javaid: Network & System Programming Elementary TCP Sockets
Instructor: Engr. Sehar Javaid: Network & System Programming Elementary TCP Sockets
Lecture 10
Elementary TCP Sockets
Concurrent Server
Handle multiple clients at the same
time.
The simplest way to write a
concurrent server under Unix is
toforka child process to handle each
client.
All descriptors open in the parent
before the call toforkare shared with
the child afterforkreturns.
Concurrent Server
The parent callsacceptand then
callsfork.
The connected socket is then shared
between the parent and child.
Normally, the child then reads and
writes the connected socket and the
parent closes the connected socket.
fork()
There are two typical uses offork:
A process makes a copy of itself so that one
copy can handle one operation while the
other copy does another task. This is typical
for network servers.
A process wants to execute another program.
Since the only way to create a new process is by
callingfork, the process first callsforkto make a
copy of itself, and then one of the copies (typically
the child process) callsexecto replace itself with
the new program.
exec()
The only way in which an executable
program file on disk can be executed
by Unix is for an existing process to
call one of the six exec functions.
It replaces the current process image
with the new program file, and this
program file normally starts at the
main function.
The process ID does not change.
exec()
The differences in the six exec
functions are:
a) Whether the program file to execute is
specified by a filename or a pathname.
b) Whether the arguments to the new
program are listed one by one or
referenced through an array of pointers.
c) Whether the environment of the calling
process is passed to the new program or
whether a new environment is specified.
exec()
#include <unistd.h>
int execl (const char *pathname ,const char *arg0, .. /* (char *) 0
*/ );
int execv (const char *pathname ,char *const argv[]);
int execle (const char *pathname ,const char *arg0, .../* (char *)
0, char *const envp[] */ );
int execve (const char *pathname ,char *const argv[], char
*const envp[]);
int execlp (const char *filename ,const char *arg0, ... /* (char *) 0
*/ );
int execvp (const char *filename ,char *const argv[]);
All six return: -1 on error, no return on success
exec()
Concurrent Server
Concurrent Server
Concurrent Server
Concurrent Server
close() Function
Used to close a socket and terminate a TCP
connection.
Synopsis
#include<unistd.h>
int close(int sockfd);
Returns: 0 if OK, -1 on error
close() Function
Mark the socket as closed and return to
the process immediately.
The socket descriptor is no longer
usable by the process.
It cannot be used as an argument to
read and write.
TCP will try to send any data that is
already queued to be sent to the other
end.
getsockname() Function
Returns the local protocol address
associated with the socket.
Synopsis
#include <sys/socket.h>
int getsockname(int sockfd, struct
sockaddr *localaddr, socklen_t *addrlen);
Return: 0 if OK, -1 on error
getsockname() Function
Required for the following reasons:
After connect successfully returns in a TCP client
that does not call bind, it returns the local IP
address and local port number assigned to the
connection by the kernel.
After calling bind with a port number of 0, it returns
the local port number that was assigned.
Can be called to obtain address family of a socket.
In a TCP server that binds the wildcard IP address,
once a connection is established with the client, the
server can call getsockname to obtain the local IP
address assigned to the connection.
getpeername() Function
Returns the foreign protocol address
associated with the socket.
Synopsis
#include <sys/socket.h>
int getpeername(int sockfd, struct sockaddr
*peeraddr, socklen_t *addrlen);
Return: 0 if OK, -1 on error
getpeername() Function
When a server is execed by the process that calls
accept, the only way the server can obtain the
identity of the client is to call getpeername.
Example of inetd
spawning a server
Questions???
References
http://stackoverflow.com/questions/4
204915/please-explain-exec-functionand-its-family
http://infohost.nmt.edu/~eweiss/22
2_book/222_book/0201433079/ch08lev
1sec10.html
http://english.tebyan.net/newin
dex.aspx?PageSize=1&PageIndex=6
3&LANGUAGE=3&BOOKID=23760&PID=
1159
http://