Unix Network Programming Manual
A Simple Example
Before explaining the system calls and structures that are used to implement
network programs, the following example is a simple program that demonstrates
their use. It will be referred to frequently on following pages.
/* =============================================================> simple.c
* Simple example of a network program for the Unix environment using TCP.
*
* This client program simply accesses a server socket and writes a message
* gets a response and terminates.
*
* ==================================================================
*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
void main()
{
int sock, addrsize;
struct sockaddr_in addr;
char buffer [81];
/*
* Open a socket for the Internet address family and stream
* communication.
*/
sock = socket(AF_INET, SOCK_STREAM,0);
if (sock == -1)
{
perror("opening socket");
exit(-1);
}
/*
* Connect the socket to a server process as specified by a known IP
* address and port.
*/
addr.sin_family = AF_INET;
addr.sin_port = htons (32351);
addr.sin_addr.s_addr = inet_network ("192.31.215.210");
if (connect(sock, &addr, sizeof (struct sockaddr_in)) == -1)
{
perror("on connect");
exit(-1);
}
/*
* Write a message and get the response.
*/
nchar = send (sock, "client calling server, do you read me", 38, 0);
nchar = recv (sock, buffer, 80, 0);
/*
* Do a shutdown to gracefully terminate by saying - "no more data"
* and then close the socket -- the shutdown is optional
*/
if (shutdown(sock, 1) == -1)
{
perror("on shutdown");
exit(-1);
}
close(sock);
}