Core Functions for Developing Network

Outline and Objective
The Makefile
The Header File
The Server
The Client
Core Functions for Developing Network
Applications
Zhongwei Zhang and Ron Addie
August 5, 2014
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
Core Functions for Developing Network Applications
Practical 3
Understand the core functions used in the server-client
network application
Understand the data structure such as sockaddr in, servent,
Learn how to open an existing file using the system calls from
the local filesystem, and create a new file using system calls.
Realization of writing data of a file on the local fileystem to
the socket and or reading data from the active socket by using
system calls: read and write.
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
Task List
List the core functions a server often used
List the core functions a client usually need
Find out how a client attempts to establish a connection with
a server.
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Practical Question
Simple File Transfer on the Internet
An application which will tansfer a file requested by a client by
name.
SFTP protocol
The client send the name of a file to the server, and the server
will read the name of the file, retrieve the file from the disk,
and send that file back to the client.
Implementation
The implementation of the file transfer application will be
based on the client- server model. We adopt the Transmission
Control Protocol (TCP) as the transmission protocol in the
transport layer. The program language is ANSI C. The
sequence of establishing a conection is depicted in Figure 1.
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The sequence of steps in the client and the server
Figure : The sequence of steps to establish and undertake Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Makefile
#
SRC = x f e r C l i e n t . c x f e r S e r v e r . c
OBJ =(
. o . c )
CC = gcc
a l l : x f e r C l i e n t x f e r S e r v e r
x f e r S e r v e r : x f e r S e r v e r . o
$ (CC)
o x f e r S e r v e r x f e r S e r v e r . o
x f e r C l i e n t : x f e r C l i e n t . o
$ (CC)
o x f e r C l i e n t x f e r C l i e n t . o
#
x f e r S e r v e r . o : x f e r S e r v e r . c x f e r c o n s t . h
$ (CC)
c x f e r S e r v e r . c
x f e r C l i e n t . o : x f e r C l i e n t . c x f e r c o n s t . h
$ (CC)
c x f e r C l i e n t . c
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Header File
It is good to separate common code, i.e. code which is used in
more than one place, and which must be the same each time, into
a
header file, which is included in other files.
The file
#i n c l u d e <s t d l i b . h>
# d e f i n e SERVERPORT 8888
# d e f i n e MAXBUF 1024
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Server
#i n c l u d e <s t d l i b . h>
#i n c l u d e <f c n t l . h>
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <s y s / s o c k e t . h>
#i n c l u d e <netdb . h>
#i n c l u d e <s t d i o . h>
#d e f i n e SERVERPORT 8888
#d e f i n e MAXBUF 1024
i n t main ( )
f
i n t so ck et1 , s o c k e t 2 ;
i n t a d d r l e n ;
s t r u c t s o c k a d d r i n x f e r S e r v e r , x f e r C l i e n t ;
i n t r e t u r n S t a t u s ;
/
c r e a t e a TCP s o c k e t /
s o c k e t 1 = s o c k e t ( AF INET , SOCK STREAM, 0 ) ;
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Server
Part 2

i f ( s o c k e t 1 == 1) f
f p r i n t f ( s t d e r r , ” Could not c r e a t e s o c k e t !nn ” ) ;

e x i t ( 1 ) ;
g /
b ind to a so c k e t , u s e INADDR ANY f o r a l l l o c a l a d d r e s s e s /
x f e r S e r v e r . s i n f a m i l y = AF INET ;
x f e r S e r v e r . s i n a d d r . s a d d r = INADDR ANY ;
x f e r S e r v e r . s i n p o r t = h ton s (SERVERPORT ) ;
r e t u r n S t a t u s = b ind ( so ck et1 , ( s t r u c t s o c k add r
)& x f e r S e r v e r ,
s i z e o f ( x f e r S e r v e r ) ) ;

i f ( r e t u r n S t a t u s == 1) f
f p r i n t f ( s t d e r r , ” Could not b ind to s o c k e t !nn ” ) ;

e x i t ( 1 ) ;
g

r e t u r n S t a t u s = l i s t e n ( so ck e t1 , 5 ) ;
i f ( r e t u r n S t a t u s == 1) f
f p r i n t f ( s t d e r r , ” Could not
e x i t ( 1 ) ;
l i s t e n on s o c k e t !nn ” ) ;

g
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Server
Part 3
f o r ( ; ; ) f
i n t fd ;
i n t i , r eadCount e r , w r i t e C o u n t e r ;
ch a r
b u f p t r ;
ch a r buf [MAXBUF ] ;
ch a r f i l e n a m e [MAXBUF ] ;
/
w a i t f o r an in coming c o n n e c t i o n /
a d d r l e n = s i z e o f ( x f e r C l i e n t ) ;

/u s e a c c e p t ( ) to h and l e in coming c o n n e c t i o n r e q u e s t s /
/and f r e e u p th e o r i g i n a l s o c k e t f o r o t h e r r e q u e s t s /

s o c k e t 2 = a c c e p t ( so ck et1 , ( s t r u c t s o c k add r )& x f e r C l i e n t , &a d d r l e n ) ;

i f ( s o c k e t 2 == 1) f
f p r i n t f ( s t d e r r , ” Could not a c c e p t c o n n e c t i o n !nn ” ) ;

e x i t ( 1 ) ;
g

/
g e t th e f i l e n a m e from th e c l i e n t
i = 0 ;
o v e r th e s o c k e t /

Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Server
Part 4
i f ( ( r e adC oun t e r = r e ad ( so ck et2 , f i l e n a m e+i , MAXBUF) ) > 0 ) f
i += r e adC oun t e r ;
g

i f ( r e adC oun t e r == 1) f
f p r i n t f ( s t d e r r , ” Could not r e ad f i l e n a m e from s o c k e t !nn ” ) ;

c l o s e ( s o c k e t 2 ) ;
c o n t i n u e ;
g
f i l e n a m e [ i +1]= ’n0 ’;
p r i n t f (” Reading f i l e %s
nn ” , f i l e n a m e ) ;
/
open th e f i l e f o r r e a d i n g /
fd = open ( f i l e n a m e , O RDONLY ) ;

i f ( fd == 1) f
f p r i n t f ( s t d e r r , ” Could not open f i l e
c l o s e ( s o c k e t 2 ) ;
c o n t i n u e ;
f o r r e a d i n g !nn ” ) ;

g /
r e s e t th e r e ad c o u n t e r /
r e adCoun t e r = 0 ;
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Server
Part 5

/r e ad th e f i l e , and send i t to th e c l i e n t i n chunck of s i z e MAXBUF /
w h i l e ( ( r e adC oun t e r = r e ad ( fd , buf , MAXBUF) ) > 0 ) f

w r i t e C o u n t e r = 0 ;
b u f p t r = buf ;
w h i l e ( w r i t e C o u n t e r
< r e adC oun t e r ) f
r e adC oun t e r = w r i t e C o u n t e r ;
b u f p t r += w r i t e C o u n t e r ;
w r i t e C o u n t e r = w r i t e ( so ck e t2 , b uf p t r , r e adC oun t e r ) ;

i f ( w r i t e C o u n t e r == 1) f
f p r i n t f ( s t d e r r , ” Could not w r i t e
c l o s e ( s o c k e t 2 ) ;
c o n t i n u e ;
f i l e to c l i e n t !nn ” ) ;

g
g
g
c l o s e ( fd ) ;
c l o s e ( s o c k e t 2 ) ;
g
c l o s e ( s o c k e t 1 ) ;
r e t u r n 0 ;
g
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications
Outline and Objective
The Makefile
The Header File
The Server
The Client
The Client
Part 1
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <s y s / s o c k e t . h>
#i n c l u d e <n e t i n e t / i n . h>
#i n c l u d e <a rpa / i n e t . h>
#i n c l u d e <s t r i n g . h>
#i n c l u d e <s t d i o . h>
#i n c l u d e <s y s / s t a t . h>
#i n c l u d e <f c n t l . h>
#i n c l u d e ” x f e r c o n s t . h”
i n t main ( i n t argc , ch a r
a rg v [ ] ) f
i n t sockd ;
i n t c o u n t e r ;
i n t fd ;
s t r u c t s o c k a d d r i n x f e r S e r v e r ;
ch a r buf [MAXBUF ] ;
i n t r e t u r n S t a t u s ;

i f ( a r g c < 3 ) f
f p r i n t f ( s t d e r r , “Usage : %s <i p add r e s s> <f i l e n a m e> [ d e s t f i l e n a m e ]nn ” ,

a rg v [ 0 ] ) ;
e x i t ( 1 ) ;
g /
∗ ∗/

sockd = s o c k e t ( AF INET , SOCK STREAM,
Zhongwei Zhang and Ron Addie
0 ) ;
Core Functions for Developing Network Applications

Outline and Objective
The Makefile
The Header File
The Server
The Client
The Client
Part 2
/s e t up th e s e r v e r i n f o r m a t i o n /
x f e r S e r v e r . s i n f a m i l y = AF INET ;
x f e r S e r v e r . s i n a d d r . s a d d r = i n e t a d d r ( a rg v [ 1 ] ) ;
x f e r S e r v e r . s i n p o r t = h ton s (SERVERPORT ) ;
/
conn e c t to th e s e r v e r /
r e t u r n S t a t u s = conn e c t ( sockd , ( s t r u c t s o c k add r
)& x f e r S e r v e r ,
s i z e o f ( x f e r S e r v e r ) ) ;

i f ( r e t u r n S t a t u s == 1) f
f p r i n t f ( s t d e r r , ” Could not conn e c t to s e r v e r !nn ” ) ;

e x i t ( 1 ) ;
g

/
send th e name of th e
f i l e we want to th e s e r v e r /
r e t u r n S t a t u s = w r i t e ( sockd , a rg v [ 2 ] , s t r l e n ( a rg v [ 2 ] ) + 1 ) ;

 

i f ( r e t u r n S t a t u s == 1) f
f p r i n t f ( s t d e r r , ” Could not send f i l e n a m e to s e r v e r !nn ” ) ;

e x i t ( 1 ) ;
g /
c a l l shutdown to s e t our s o c k e t to reado n l y /
shutdown ( sockd , SHUT WR ) ;

/open up a h and l e to our d e s t i n a t i o n f i l e to r e c e i v e th e c o n t e n t s /
/from th e s e r v e r /
fd = open ( a rg v [ 3 ] , O WRONLY j O CREAT j O APPEND ) ;
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications

Outline and Objective
The Makefile
The Header File
The Server
The Client
The Client
Part 3

i f ( fd == 1) f
f p r i n t f ( s t d e r r , ” Could not open d i s t i n a t i o n
fd = 1 ;
f i l e , u s i n g s t d o u t .nn ” ) ;

g /
r e ad th e f i l e from th e s o c k e t a s l on g a s t h e r e i s data /

w h i l e ( ( c o u n t e r = r e ad ( sockd , buf , MAXBUF) ) > 0 ) f
/send th e c o n t e n t s to s t d o u t /

w r i t e ( fd , buf , c o u n t e r ) ;
g

i f ( c o u n t e r == 1) f
f p r i n t f ( s t d e r r , ” Could not r e ad
e x i t ( 1 ) ;
f i l e from s o c k e t !nn ” ) ;

g
c l o s e ( so ckd ) ;
r e t u r n 0 ;
g
Zhongwei Zhang and Ron Addie Core Functions for Developing Network Applications