Simplex Talk Project

by: burt rosenberg
at: january 2016


Our first trip on the information highway!

Simplex Talk

The major software abstraction that we use as programmers is the socket, more properly the Berkeley Socket. This abstraction summarizes all of the machinery into a file handle, very similar to reading and writing a simple file, except slightly different concepts associated with opening a socket, rather than file, such as setting networking addresses.

This project introduces the use of sockets, as well as informing you on subversion, ssh and the lab, and makefiles.

Project

  1. Create folder proj1 inside your userfolder in the respository, e.g. [repo]/burt/proj1,
  2. Add the files simplex-talk-s.c, simplex-talk.c, and Makefile, as below. (Note, these files are also available as [repo]/class/examples/textbook-1.)
  3. Build and run.
  4. Setup ssh so that you can log into two different machines in the lab.
  5. Modify the makefile so as to demonstrate that your code works when client and server are run on different machines.
  6. Commit for grading by the due date

Server code




/*
 * simplex-talk-s.c
 *
 * network programming example from
 * "computer networks a systems approach" peterson and davie
 * slightly modified to obtain a clean compile
 *
 * last update by: burt
 * last update: 23 jan 2017
 */


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

#include<strings.h>
#include<stdlib.h>
#include<unistd.h>

#define SERVER_PORT 5432
#define MAX_PENDING 5 
#define MAX_LINE 256

int main(int argc, char * argv[]) {
        struct sockaddr_in sin ;
        char buf[MAX_LINE] ;
        int len ;
        int s, new_s ;

        bzero((char *)&sin,sizeof(sin)) ;
        sin.sin_family = AF_INET ;
        sin.sin_addr.s_addr = INADDR_ANY ;
        sin.sin_port = htons( SERVER_PORT) ; 
        
        if ( (s = socket(PF_INET,SOCK_STREAM,0)) < 0) {
                perror("simplex-talk: socket") ;
                exit(1) ;
        }
        if   (bind( s, (struct sockaddr *) &sin, sizeof(sin) ) < 0 ) {
                perror("simplex-talk: bind") ; 
                exit(1) ;
        }
        listen( s, MAX_PENDING ) ;

        while(1) {
                if ( (new_s = accept(s,(struct sockaddr *)&sin,
                        (socklen_t *) &len) ) < 0 ) {
                        perror("simplex-talk: accept") ; 
                        exit(1) ;
                }
                while ( (len = recv(new_s, buf, sizeof(buf), 0 )) )
                        fputs(buf, stdout) ;
                close(new_s) ;
        }
}


 

Client code



/*
 * simplex-talk.c
 *
 * network programming example from
 * "computer networks a systems approach" peterson and davie
 * slightly modified to obtain a clean compile
 *
 * last update by: burt
 * last update: 23 jan 2017
 */
 

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

#include<strings.h>
#include<stdlib.h>

#define SERVER_PORT 5432
#define MAX_LINE 256



int main(int argc, char * argv[]) {
        FILE *fp ;
        struct hostent *hp ;
        struct sockaddr_in sin ;
        char * host ;
        char buf[MAX_LINE] ;
        int s ;
        int len ;

        if ( argc==2 ) {
                host = argv[1] ;
        } else {
                fprintf(stderr, "usage: simplex-talk host\n") ;
                exit(1) ;
        }
        
        hp = gethostbyname(host) ;
        if (!hp) {
                fprintf(stderr, "simplex-talk: unknown host: %s\n", host ) ;
                exit(1); 
        }
        bzero((char *)&sin,sizeof(sin)) ;
        sin.sin_family = AF_INET ;
        bcopy( hp->h_addr, (char *) &sin.sin_addr, hp->h_length) ;
        sin.sin_port = htons( SERVER_PORT) ;
        
        if ( (s = socket(PF_INET,SOCK_STREAM,0)) < 0) {
                perror("simplex-talk: socket") ;
                exit(1) ;
        }
        if ( connect(s, (struct sockaddr *) &sin, sizeof(sin) ) < 0 ) {
                perror("simplex-talk: connect") ; 
                exit(1) ;
        }

        while ( fgets(buf, sizeof(buf), stdin) )  {
                buf[MAX_LINE-1] = '\0' ; 
                len = strlen(buf) + 1 ;
                send(s, buf, len, 0 ) ; 
        }
}


 

Makefile


#
# example networking program from
# "computer networks a systems apprroach" by Peterson and Davie 
#
# makefile author: bjr
# created: 23 jan 2017
# last-update: 23 jan 2017
#

HOST= localhost
MESSAGE= "hello world" 

all: simplex-talk simplex-talk-s
        ./simplex-talk-s &
        echo ${MESSAGE} | ./simplex-talk ${HOST}
        killall simplex-talk-s
        make clean

run-s: simplex-talk-s
        ./simplex-talk-s 

run: simplex-talk-s simplex-talk
        echo ${MESSAGE} | ./simplex-talk ${HOST}

simplex-talk: simplex-talk.c
        cc -o $@ $<

simplex-talk-s: simplex-talk-s.c
        cc -o $@ $<

clean:
        -rm simplex-talk simplex-talk-s


Internet crash course

In order to accomplish this project, you will need sneak previews on four networking technologies:

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

author: burton rosenberg (except Peterson/Davie code)
created: 30 jan 2017
update: 30 jan 2017