In previous issues of OnlineProgrammer ezine we saw client
section of our client/server application. In this section we
will see the server side software.
Again we will use BSD socket functions for socket calls.
1- First of all we create a socket by calling socket function.
Function will return a handle to socket, which we will use it
to work with the socket.
2- No we must adjust some of connection specifications in an
internet socket address type structure (sockaddr_in). Then we
will use this adjusted structure in our socket functions to
start listening socket for connections. These include socket
family (AF_INET), IP address of listening network interfaces,
and TCP listening port. INADDR_ANY means that server socket
will listen to all network interfaces.
3- Now after setting addresses in socket address structures we
can start creating socket. We create our socket by calling
socket(...) function. SOCK_STREAM parameter specifies that we
need a TCP socket.
4- To be able to receive requests from clients we must listen
the specified port. For this purpose we bind created socket
to the port and network interface and then call listen function
to start listening. In listen function we have told that socket
can queue up to 20 connections before it can process them.
5- After this we reach inside the main server loop. Here we
wait until we have a connection request to listening socket.
If socket has a pending connection it will return it to the
socket behind accept function. Accept function is a blocking
function. It will block at the function until it receives a
connection.
6- After accept function created a socket connected to remote
host we can receive data from socket connection. Because of
the way we have designed communication protocol for this
client/server application we first start by receiving
information from the connection. "recv" function will put
received data in our buffer.
7- At the end server must send requested information. Here the
information we want to send is only a constant string "hello".
We put this in a buffer and pass it to "send" function.
8- After sending the information we must close temporary socket.
9- Now we can return to the first command in server loop to
serve another connection.
And now the source code. You can download source codes from
our site at:
http://op.htmsoft.com
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
main(void)
{
system("clear");
printf("Client:\n");
tcpserv();
}
tcpserv()
{
char buf[8096];
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
int address_size;
long len;
int counter=0;
sock_descriptor=socket(AF_INET,SOCK_STREAM,0);
bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(8000); //we are using port 8000
bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin));
listen(sock_descriptor,20); //queue up to 20 connections
while(1)
{
//get a temporary socket to handle client request
temp_sock_descriptor= accept
(sock_descriptor,(struct sockaddr *)&pin,&address_size);
//receive data from client
recv(temp_sock_descriptor,buf,8096,0);
strcpy(buf,"hello");
len=strlen(buf);
printf("\nA request arrived\n");
// here we can process the client request
// return data to the client
send(temp_sock_descriptor,buf,len,0);
//close the temporary socket as we are done with it
close(temp_sock_descriptor);
}
}
You can run above code on every Unix or Linux operating
system with minor changes. You will need to include socket
library in compile process. So below line will do it.
$gcc server.c -lsocket -o client
You can find previous articles about this software on our
site.
|