In previous issues of OnlineProgrammer ezine we saw
client/server programs for windows operating system. We
used a socket Active-X object for those programs.
In Unix operating system we will use BSD sockets library
for network programming. BSD socket functions are the base
for socket functions in other environments too. Winsock
itself is written on this library. There is a port of this
library in other operating systems too.
We start by writing a hello client software. This time we
start with client software, as it is easier to understand.
To test this software you either need the server software
that will be discussed in our next issue or another server
software. You can even use a server software on a windows
platform over TCP/IP network. You will then need remote
computer address to establish TCP/IP connections to it.
We will not have enough time to describe every structure
and function completely but you must be able to write
other programs if you work on this code.
Program is commented on each line so it must be very easy
to understand how it works.
1- First off all we must enter host name in a structure
hostent. This includes the address we want to connect it.
2- After this we must adjust some of connection specifications
in an Internet socket type structure (sockaddr_in). Then we
will use this adjusted structure in our socket functions to
establish the connection. These include socket family
(AF_INET), TCP/IP port (PORT) and remote host address
(s_addr).
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- After creation of socket we use connect function to start
connecting to remote host.We use handle to created socket
(sock_descriptor) and internet address structure (sockaddr_in)
as parameters to connect function.
5- Connect function in this example is used in blocking mode.
This means that above function will block program execution
until either a connection is established or a time out or
error is issued. So if we go normally to next instruction
it means that we have connected to remote host and we can
start sending our message.
6- Send function uses a handle to our active socket, buffer
that contains data that we will send and buffer length.
After using buffer for send step we clear buffer to prepare
it for receiving data.
7- After send step we will wait to receive the answer from
server side software on remote computer. (Our own computer
in this example or you can change remote computer address.)
8- Receive function will need a handle to active socket, a
pointer to data buffer and data buffer size as its parameters.
9- After completion of data receive we can close the connection.
And now the source code. You can download source codes from
our site at:
http://op.htmsoft.com
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#define PORT 12000
main(void)
{
char buf[80];
struct sockaddr_in pin;
int sock_descriptor;
int address_size;
struct hostent *server_host_name;
long len;
strcpy(buf,"Hi there"); //message that will be sent
//remote host is local host
server_host_name=gethostbyname("127.0.0.1");
bzero(&pin,sizeof(pin));
pin.sin_family=AF_INET;
pin.sin_addr.s_addr=((struct in_addr *)
(server_host_name->h_addr))->s_addr;
pin.sin_port=htons(PORT); //we are using port 12000
//create socket
sock_descriptor=socket(AF_INET,SOCK_STREAM,0);
connect(sock_descriptor,(void *)&pin,sizeof(pin));
send(sock_descriptor,buf,strlen(buf),0);
bzero(buf,20);
recv(sock_descriptor,buf,80,0);
printf("\nReceived from server : %s\n",buf);
close(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 client.c -lsocket -o client
In next issue of this ezine we will see the source code for
server side software.
|