Ch Standard Demos
ISO C90 Standard
Wide characters
ISO C99 Standard
C++ features
Complex IEEE floating-point arithmetic
Assumed-shape arrays
Nested functions
Interactive C statement execution
Interactive command shell
Shell Programming
Safe Ch
Ch applets
String Type
Adjustable array bounds
Auto array bound checking
Socket/Winsock
POSIX
Unix utilities for Windows
Windows
GTK+
X/Motif
OpenGL
Toolkits Demos
CGI
ODBC
ChExcel
Ch Professional Demos
Computational array
Plotting
Numerical analysis
C LAPACK functions
Demo programs server.ch and client.ch are located in the directory CHHOME/demos/lib/libc/sys/socket/ for Unix and CHHOME/demos/lib/libc/winsock/ for Windows.
/* 
   server.ch
   This is a server program that waits for clients to connect
   and records the connected times by clients. You need run client.ch after 
   this program is started. 

   Usage :
      server.ch [PORT_NUMBER]
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define PROTOPORT 5139 /* default protocol port number */
#define QLEN 6         /* lenght of listenning queue */
 
/* Usage will be display if there is any incorrect option */
char usage[]= "\nThis is a server program that waits for clients to connect.\n\n"
              "Usage :\n      server.ch [PORT_NUMBER]\n\n"
              "      PORT_NUMBER : TCP port number will be used\n\nFor example :\n      server.ch\n"
              "      server.ch 6000\n\nThe default optional value :\n"
              "      PORT_NUMBER : 5139\n\n"; 

int main(int argc,char *argv[]) {
    struct protoent *ptrp;  /* pointer to a protocol table entry  */
    struct sockaddr_in sad; /* structure to hold server's address */
    struct sockaddr_in cad; /* structure to hold client's address */
    int    sd,sd2;          /* socket descriptors                 */
    int port;               /* ptotocol prot number               */
    int alen;               /* length of address                  */
    char buf[1000];         /* buffer for string the server sends */
    int childpid, guest_num = 0;

    memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
    sad.sin_family=AF_INET;             /* set family to Internet   */ 
    sad.sin_addr.s_addr=INADDR_ANY;     /* set the local IP address */
    
    if(argc>1) {              /* if argument specified */     
        port=atoi(argv[1]);   /* convert argument to binary */
    }
    else {
        port=PROTOPORT;       /* use default port number */
    }
    if(port>0)              /* test for illegal value */
        sad.sin_port=htons((u_short)port);
    else {                   /* print erro message and exit */ 
        fprintf(stderr,"\nError : bad port number %s\n",argv[1]);
        printf("%s", usage);
        exit(1);
    }

    /* Map TCP transport protocol name to protocol number */ 
    if(((int)(ptrp=getprotobyname("tcp")))==0) {
        fprintf(stderr,"\nError : cannot map \"tcp\" to protocol number");
        printf("%s", usage);
        exit(1);
    }
    
    /* Creat a socket */
    sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
    if(sd<0) {
        fprintf(stderr,"\nError : socket creation failed \n");
        printf("%s", usage);
        exit(1);
    }
    
    /* Bind a local address to the socket */
    if(bind(sd,(struct sockaddr *)&sad,sizeof(sad))<0) {
        fprintf(stderr,"\nError : bind failed\n");
        printf("%s", usage);
        close(sd);
        exit(1);
    }
    
    /* Specify size of request queue */
    if(listen(sd,QLEN)<0) {
        fprintf(stderr,"listen failed \n");
        close(sd);
        exit(1);
    }
    
    /* Main server loop - accept and handle requests */
    while(1) {
        alen = sizeof(cad);
        if((sd2=accept(sd,(struct sockaddr *)&cad, &alen))<0) {
            fprintf(stderr,"accept failed \n");
            exit(1);
        }

        guest_num++; 
        if( (childpid = fork()) < 0) {
           printf("server: fork error\n");
           exit(1);
        }
        else if( childpid == 0 ) { /* Child process */
           close(sd);
           recv(sd2,buf,sizeof(buf),0);
           printf("\nData received from guest %d : %s\n", guest_num, buf);

           sprintf(buf,"From the Server to guest No. %d", guest_num);
           send(sd2,buf,strlen(buf),0);
	   printf("Connection to guest %d closed\n", guest_num);
           exit(0);
        }

        close(sd2); /* Parent process */
    }
} 


/* 
  client.ch
  This is a client program which is set up to be run along with server.ch.

  Usage :
      client.ch [-i IP_ADDRESS | -h HOST_NAME] [-p PORT_NUMBER]
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define PROTOPORT 5139    /* default protocol port number */
char localhost[]= "localhost"; /* default host name */

/* Usage will be display if there is any incorrect option */
char usage[]= "\nThis is a client program which is set up to be run along with server.ch.\n\n"
              "Usage :\n      client.ch [-i IP_ADDRESS | -h HOST_NAME] [-p PORT_NUMBER]\n\n      "
              "      IP_ADDRESS  : IP address of the machine on which server.ch is running.\n"
              "      HOST_NAME   : hostname of the machine on which server.ch is running.\n"
              "      PORT_NUMBER : TCP port number used by server.ch\n\n"
              "For example :\n      client.ch\n      client.ch -i 12.34.56.78\n"
              "      client.ch -h myhost\n      client.ch -p 6000\n"
              "      client.ch -i 12.34.56.78 -p 6000\n"
              "      client.ch -h myhost -p 6000\n\nThe default optional values :\n"
              "      HOST_NAME   : localhost\n      PORT_NUMBER : 5139\n\n";  

int main(int argc,char *argv[]) {
    struct hostent *ptrh;      /* pointer to a host table entry     */
    struct protoent *ptrp;     /* pointer to a protocol table entry */
    struct sockaddr_in sad;    /* structure to hold an IP address   */
    int    sd;                 /* socket descriptor                 */
    int port;                  /* protocol port number              */
    char *host;                /* pointer to host name              */
    char buf[1000];            /* buffer for data from the server   */

    setbuf(stdout,NULL);       /* turn off the out buffer           */
    memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
    sad.sin_family=AF_INET;             /* set family to internet   */
 
    /* set default optional values */
    port=PROTOPORT;
    host=localhost;
    sad.sin_addr.s_addr = (in_addr_t)-1;

    /* get the user-defined optional values */
    if(argc > 1) {
      for(; *argv != NULL; argv++) {
         /* get ip address */ 
         if(strcmp(*argv, "-i") == 0 || strcmp(*argv, "-I") == 0) {
           argv++;
           sad.sin_addr.s_addr = inet_addr(*argv); 
         }
         /* get tcp port number */
         if(strcmp(*argv, "-p") == 0 || strcmp(*argv, "-P") == 0) {
           argv++;
           port = atoi(*argv); 
         }
         /* get hostname */
         if(strcmp(*argv, "-h") == 0 || strcmp(*argv, "-H") == 0) {
           argv++;
           host = *argv;
         }

      }
    }  

    /* convert port */ 
    if(port>0)
        sad.sin_port=htons((u_short)port);
    else {
        fprintf(stderr,"\nError : bad port number %s\n",argv[1]);
        printf("%s", usage);
        exit(1);
    }
  
    /* convert IP address. */
    if( sad.sin_addr.s_addr == (in_addr_t)-1) {
       ptrh=gethostbyname(host);
       if(((char *)ptrh)==NULL) {
          fprintf(stderr,"\nError : invalid host: %s \n",host);
          printf("%s", usage);
          exit(1);
       }
       memcpy(&sad.sin_addr,ptrh->h_addr,ptrh->h_length);
    }

    /* map TCP transport protocol name to protocol number. */
    if(((int)(ptrp=getprotobyname("tcp")))==0) {
        fprintf(stderr,"\nError : cannot map \"tcp\" to protocol number");
        printf("%s", usage);
        exit(1);
    }
  
    /* creat a socket */
    sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
    if(sd<0) {
        fprintf(stderr,"\nError : socket creation failed \n");
        printf("%s", usage);
        exit(1);
    }
  
    /* connect the socket to the specified  server. */
    if(connect(sd,(struct sockaddr *)&sad,sizeof(sad))!=0) {
       fprintf(stderr,"\nError : connect failed\n");
       printf("%s", usage);
       close(sd);
       exit(1);
    }

    /* send a tcp package tp server */
    sprintf(buf,"From the Client");
    send(sd,buf,strlen(buf),0);

    /* receive a tcp package from server */
    recv(sd,buf,sizeof(buf),0);
    printf("Data received: %s\n", buf); 

    printf("Connection to server closed\n"); 
    close(sd);
    exit(0);
}