old-www/LDP/LG/issue47/bueno.html

260 lines
8.8 KiB
HTML

<!--startcut BEGIN header ==============================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Introduction to Socket Programming LG #47</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Introduction to Socket Programming</font></H1>
<H4>By <a href="mailto:pedro.bueno@persogo.com.br">Pedro Paulo Ferreira
Bueno</a> and <A HREF="mailto:apcastro@dcc.unicamp.br">Antonio Pires de Castro
Junior</A></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<H3>Introduction</H3>
<P> Most operating systems provide precompiled programs that communicate across
a network. Common examples into the TCP/IP world are web clients(browsers) and
web servers, and the FTP and TELNET clients and servers. Sometimes when we are
using this utilities of the internet we don't think about all the process
involved. To better understand this aspects we, in our research group(GTI,
Grupo de Tecnologia em Informática) at Goias Catholic University (Universidade
Católica de Goiás), decide to build, write our own network programs,
mini-chat, using the basic structure about sockets, an application program
interface or API, that mechanism that make all this communication possible over
the Net.
<P> We examine the functions for communication through sockets. A socket is an
endpoint used by a process for bi-directional communication with a socket
associated with another process. Sockets, introduced in Berkeley Unix, are a
basic mechanism for IPC on a computer system, or on different computer systems
connected by local or wide area networks(resource 2). To understand some
structs into this subject is necessary a deeper knowledge about the operating
system and his networking protocols. This subject can be used as either
beginners programmers or as a reference for experienced programmers.
<H3>The Socket Function</H3>
<P> Most network applications can be divided into two pieces: a client and a
server.
<!-- We can draw the communication link between the two as shown in Figure
1.1 (resource 6) -->
<P> Creating a socket
<PRE>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
</PRE>
<P> When you create a socket there are three main parameters that you have to
specify:
<UL>
<LI> the domain
<LI> the type
<LI> the protocol
</UL>
<PRE>
int socket(int domain, int type, int protocol);
</PRE>
<P> The Domain parameter specifies a communications domain within which
communication will take place, in our example the domain parameter was AF_INET,
that specify the ARPA Internet Protocols The Type parameter specifies the
semantics of communication, in our mini chat we used the Stream socket
type(SOCK_STREAM), because it offers a bi-directional, reliable, two-way
connection based byte stream(resource 2). Finally the protocol type, since we
used a Stream Socket type we must use a protocol that provide a
connection-oriented protocol, like IP, so we decide to use IP in our protocol
Type, and we saw in /etc/protocols the number of ip, 0. So our function now is:
<PRE>
s = socket(AF_INET , SOCK_STREAM , 0)
</PRE>
where 's' is the file descriptor returned by the socket function.
<P> Since our mini chat is divided in two parts we will divided the explanation
in the server, the client and the both, showing the basic differences between
them, as we will see next.
<H3>The Mini-chat Server structure</H3>
<H3>Binding a socket to a port and waiting for the connections</H3>
<P> Like all services in a Network TCP/IP based, the sockets are always
associated with a port, like Telnet is associated to Port 23, FTP to 21... In
our Server we have to do the same thing, bind some port to be prepared to
listening for connections ( that is the basic difference between Client and
Server), Listing 2. Bind is used to specify for a socket the protocol port
number where it will be waiting for messages.
<P> So there is a question, which port could we bind to our new service? Since
the system pre-defined a lot of ports between 1 and 7000 ( /etc/services ) we
choose the port number 15000.
<P> The function of bind is:
<PRE>
int bind(int s, struct sockaddr *addr, int addrlen)
</PRE>
<P> The struct necessary to make socket works is the struct sockaddr_in address;
and then we have the follow lines to say to system the information about the
socket.
<DL>
<DT>The type of socket
<DD><CODE>address.sin_family = AF_INET /* use a internet domain */</CODE>
<DT>The IP used
<DD><CODE>address.sin_addr.s_addr = INADDR_ANY /*use a specific IP of host*/</CODE>
<DT>The port used
<DD><CODE>address.sin_port = htons(15000); /* use a specific port number */</CODE>
</DL>
<P> And finally bind our port to the socket
<PRE>
bind(create_socket , (struct sockaddr *)&address,sizeof(address));
</PRE>
<P> Now another important phase, prepare a socket to accept messages from
clients, the listen function is used on the server in the case of connection
oriented communication and also the maximum number of pending
connections(resource 3).
<PRE>
listen (create_socket, MAXNUMBER)
</PRE>
<P> where MAXNUMER in our case is 3. And to finish we have to tell the server to
accept a connection, using the accept() function. Accept is used with
connection based sockets such as streams.
<PRE>
accept(create_socket,(struct sockaddr *)&address,&addrlen);
</PRE>
<P> As we can see in Listing 2 The parameters are the socket descriptor of the
master socket (create_socket), followed by a sockeaddr_in structure and the
size of the structure.(resource 3)
<H3>The Mini-chat Client structure</H3>
<P> Maybe the biggest difference is that client needs a Connect() function. The
connect operation is used on the client side to identify and, possibly, start
the connection to the server. The connect syntax is
<PRE>
connect(create_socket,(struct sockaddr *)&address,sizeof(address)) ;
</PRE>
<H3>The common structure</H3>
<P> A common structure between Client and the Server is the use of the struct
hostent as seeing in Listing 1 and 2. The use of the Send and Recv functions
are another common codes.
<P> The Send() function is used to send the buffer to the server
<PRE>
send(new_socket,buffer,bufsize,0);
</PRE>
<P> and the Recv() function is used to receive the buffer from the server, look
that it is used both in server and client.
<PRE>
recv(new_socket,buffer,bufsize,0);
</PRE>
<H3>Conclusion</H3>
<P> Since the software of the TCP/IP protocol is inside the operating system,
the exactly interface between an application and the TCP/IP protocols depends
of the details of the operating system(resource 4).In our case we examine the
UNIX BSD socket interface because Linux follow this. The Mini-chat developed
here is nothing more than a explain model of a client/server application using
sockets in Linux and should be used like a introduction of how easy is to
develop applications using sockets. After understand this you can easily start
to think about IPC (interprocess Communication), fork, threads(resource 5) and
much more. The basic steps to make it work is:
<OL>
<LI> Run the server
<LI> Run the client with the address of the server
</OL>
Amazing, dont you think?
<P> This example was the start of our server program in our last project, a
network management program. Here are the source listings:
<UL>
<LI> <A HREF=misc/bueno/client.c>client.c</A>
<LI> <A HREF=misc/bueno/server.c>server.c</A>
</UL>
<H3>Resources</H3>
<OL>
<LI> Operating Systems , Harvey M. Deitel , 1990
<LI> Socket Linux Man Page
<LI> Network Functions in C - Tutorial
<LI> Internetworking with TCP/IP Vol1 - Doulgas Commer
<LI> Unix Network Programming , Vol2 , Richard Stevens
<LI> Unix Network Programming, Vol1, Richard Stevens
</OL>
<!-- BEGIN copyright ==================================================-->
<P> <hr> <P>
<H5 ALIGN=center>
Copyright &copy; 1999, Pedro Paulo Ferreira Bueno and Antonio Pires de Castro Junior<BR>
Published in Issue 47 of <i>Linux Gazette</i>, November 1999</H5>
<!-- END copyright ===================================================-->
<!--startcut footer ===================================================-->
<P> <hr> <P>
<A HREF="index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="blanchard.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="../faq/index.html"
><IMG SRC="./../gx/dennis/faq.gif"
ALT="[ Linux Gazette FAQ ]"></A>
<A HREF="deblende.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P>
</BODY></HTML>
<!--endcut ============================================================-->