Below is the example source code from "TCP/IP Sockets in C: Practical Guide for Programmers" by Michael J. Donahoo and Kenneth L. Calvert. This book can be ordered at your favorite local bookstore or online.

Official Book Website

Disclaimer: The purpose of this book is to provide general information about network programming as of the book's publication date. The authors have included sample code that is intended for the sole purpose of illustrating the use of the sockets API. Neither the authors nor the publisher are aware of any third party patents or proprietary rights that may cover any sample of any kind. The authors and the Publisher DISCLAIM ALL EXPRESS AND IMPLIED WARRANTIES, including warranties of merchantability and fitness for any particular purpose. Your use or reliance upon any sample code or other information in this book will be at your own risk. No one should use any sample code (or illustrations) from this book in any software application without first obtaining competent legal advice.

Example code:

  • Generally, compilation is as follows:
  • Note for older machines (e.g., SVR4 variants such as Solaris 2.5 and before): A few more include files may be necessary. If you get compile errors about u_short, try editing the include files as follows (order matters): #include <stdio.h> /* for printf() and fprintf() */ #include <sys/types.h> /* for Socket data types */ #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */ #include <netinet/in.h> /* for IP Socket data types */ #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ #include <stdlib.h> /* for atoi() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */