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.
/*
* 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) ;
}
}
/*
* 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 ) ;
}
}
#
# 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
In order to accomplish this project, you will need sneak previews on four networking technologies:
author: burton rosenberg (except Peterson/Davie code)
created: 30 jan 2017
update: 30 jan 2017