TCP Server Examples

From PeformIQ Upgrade
Jump to navigation Jump to search

Python Test Server

A simple ECHO server

# cat svr.py 
#!/usr/bin/env python
 
# Echo server program
import socket

HOST = ''    # Symbolic name meaning the local host
PORT = 9578  # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((HOST, PORT))

s.listen(1)

conn, addr = s.accept()

print 'Connected by', addr

while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)

conn.close()


C Client

# cat tcp_client_tst.c
/*
 *      Author:  Peter Harding  <plh@performiq.com.au>
 *
 *          PerformIQ Pty. Ltd.
 *          Level 6, 6195,
 *          170 Queen Street,
 *          MELBOURNE, VIC, 3000
 *
 *          Phone:   03 9641 2222
 *          Fax:     03 9641 2200
 *          Mobile:  0418 375 085
 *
 *      Copyright (C) Peter Harding, 1997-2008
 */

static char
        id[] = "@(#) [1.0.0] tcp_client_tst.c 05/03/2008";


/********************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>


//--------------------------------------------------------------------

#define TCP            "tcp"

//--------------------------------------------------------------------

extern    int            errno;

//--------------------------------------------------------------------

static    int            ierr;

//--------------------------------------------------------------------

char *recv_host       = "plh";
char *recv_service    = "dserver";

long    the_address;
int     the_port          = 9578;
int     sfd               = -1;

//--------------------------------------------------------------------

char* sock_send(char*);
void init(void);

//--------------------------------------------------------------------

void init(void)
{
   struct hostent        *host_ent;
   struct servent        *serv_ent;
   struct sockaddr_in    sin;

   u_short                b0, b1, b2, b3;

   recv_host = "localhost";

   fprintf(stderr, "recv_host = \"%s\"\n", recv_host);

   if ((host_ent = gethostbyname(recv_host)) == (struct hostent*)0 ) {
      fprintf(stderr, "Call failed\n");
      exit(0);
   }

   // fprintf(stderr, "Call -> %s\n", host_ent);

   b0 = *(host_ent->h_addr_list[0] + 0) & 0xff;
   b1 = *(host_ent->h_addr_list[0] + 1) & 0xff;
   b2 = *(host_ent->h_addr_list[0] + 2) & 0xff;
   b3 = *(host_ent->h_addr_list[0] + 3) & 0xff;
    
   the_address  = (b0 << 24) + (b1 << 16) + (b2 <<  8) + b3;

   fprintf(stderr, "      name: %s\n",  host_ent->h_name);
   fprintf(stderr, "      type: %d\n",  host_ent->h_addrtype);
   fprintf(stderr, "     alias: %s\n", *host_ent->h_aliases);
   fprintf(stderr, "    length: %d\n",  host_ent->h_length);
   fprintf(stderr, "IP Address: %d.%d.%d.%d\n",
         b0 & 0xff, b1 & 0xff, b2 & 0xff, b3 & 0xff);

   if ((serv_ent = getservbyname(recv_service, TCP)) == (struct servent*)0 ) {
      exit(2);
   }

   //the_port         = serv_ent->s_port;

   fprintf(stderr, "Got address 0x%08x\n", the_address);
   fprintf(stderr, "Got port %d for %s\n", the_port, recv_service);

   //if ((sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == 0 ) {
   if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == 0 ) {
      fprintf(stderr, "[initiateConnection] socket() failed (errno %d)", errno);
      exit(3);
   }

   fprintf(stderr, "@@@ socket fd = %d\n", sfd );

   memcpy(&sin.sin_addr.s_addr, host_ent->h_addr, host_ent->h_length);
   sin.sin_family      = AF_INET;
   sin.sin_port = htons (the_port);
   //sin.sin_addr.s_addr = the_address;

   if ((ierr = connect(sfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in))) == -1) {
      fprintf(stderr,
         "[initiateConnection] connect() failed (errno %d)", errno);
      exit(4);
   }

   fprintf(stderr, "connected\n");
}  // init

//--------------------------------------------------------------------


char* sock_send(char *buf)
{
  int n;

   //fprintf(stderr, "[sock_send] buf = \"%s\"\n\n", buf);

   n = write(sfd, buf, strlen(buf));

   if ((n = read(sfd, buf, 1024)) == 0) {
         exit(0);
   }

   buf[n] = '\0';

   //fprintf(stderr, "\nbuf -> \"%s\"\n", buf);

   return buf;
}  // sock_send

//--------------------------------------------------------------------


main(argc, argv)
int argc;
char *argv[];
{
   char buf[1024];
   char *s;
   int i;
   int ds_idx;
   int serial;

   init();

   sprintf(buf, "REG|Sequence");

   s = sock_send(buf);

   ds_idx = atoi(s);

   for (i = 0; i < 3; i++) {
      sprintf(buf, "GETN|%d", ds_idx);

      s = sock_send(buf);

      serial = atoi(s);

      fprintf(stderr, "serial -> \"%d\"\n", serial);
      sleep(2);
   }
}  // main

//--------------------------------------------------------------------