Home Index

TCP/IP Client/Server software on Unix and Linux
Date: February 01 2001
Subject: Online Programmer Ezine, Volume 2, No.2
Article: TCP/IP Client/Server software on Unix and Linux (Hello Client)


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 

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:



#define PORT 12000
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 
pin.sin_addr.s_addr=((struct in_addr *)

pin.sin_port=htons(PORT); //we are using port 12000

//create socket
connect(sock_descriptor,(void *)&pin,sizeof(pin));
printf("\nReceived from server : %s\n",buf);


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.

Home Index
2000 OnlineProgrammer (HtmSoft.com). All Rights Reserved