Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE454 Internet Technology. Asst. Prof. Dr

Similar presentations


Presentation on theme: "CSE454 Internet Technology. Asst. Prof. Dr"— Presentation transcript:

1 CSE454 Internet Technology. Asst. Prof. Dr
CSE454 Internet Technology Asst.Prof.Dr.Surasak Mungsing Information Science Institute of Sripatum University (ISIS)

2 Lecture 05 Network Programming

3 Abstract Socket Service
Asymmetric set-up, circuit abstraction Server is passive, waits for connections Client initiates the connections Bi-directional, continuous byte stream TCP is free to break up the data however it likes But often doesn’t Tries really hard when packets are lost really best effort but timeouts on the order of 15 minutes

4 IP Layering Architecture
Application Layer Transport Network Host-to- Net Layer Host A Host B Application Protocol Application Layer Transport Protocols (UDP and TCP) Transport Layer IP IP IP Network Layer Network Layer Network Layer Host-to- Net Layer Host-to- Net Layer Host-to- Net Layer

5 Stream Service Application Protocol Transport Protocol (TCP) Application Layer Transport Network Host-to- Net Layer Host A Host B IP On the Internet, the Transmission Control Protocol (TCP) implements a byte stream network service

6 Internet Addressing Layer 3 addressing
Each IP entity(E.g. host) has a 4-byte address As decimal: A.B.C.D Can also be written as hexadecimal and binary! Domain Name System (DNS) translates symbolic names to IP addresses E.g. remus.rutgers.edu ->

7 Ports Layer 4 addressing
2 byte port # differentiates destinations within an IP address E.g. the mail server vs. the web server Fully qualified IP communication requires 5 tuples: Protocol ID (UDP, TCP) Source IP address, source port number Destination IP address, destination port number

8 Stream Sockets in Java InetAddress class
Object for containing an using IP addresses methods for viewing and changing IP addresses and symbolic names InPutStream, OutPutStream classes get and receive bytes from a socket Socket and ServerSocket, classes A TCP communication objects Contain the stream objects once socket is connected

9 Client-Server Connection Set-up
New server socket create socket accept() blocked …. connection phase blocked …. handshake return…. New socket data phase Time

10 Read-Write, Teardown Phase
Client Server write() read() write() read() close() close() handshake Time

11 Client Algorithm Create a socket object Get Input and Output Streams
Set destination address and port number In constructor implies making a connection Get Input and Output Streams Call read(), write() and flush() methods Close() method when done Be nice to the system, port use

12 Client side String machineName; int port; Socket sock = null;
InputStream in; OutputStream out; sock = new Socket(machineName, port); in = sock.getInputStream; out = sock.getOutPutStream; bytesRead = in.read(byteBuffer);

13 Server Algorithm Create a serverSocket on a port Loop:
wait on accept() method for a new client accept() returns a new socket get input and output streams for this sockets close the socket when done

14 Server Receive Socket nextClientSock; while ( ... ) {
nextClientSock = ss.accept(); // new socket // the return socket is “bound” and can // be used to send/receive data in = nextClientSock.getInputStream; out = nextClientSock.getOutputStream; }

15 Server echo using execeptions
try { while( (bytesRead = in.read(byteBuffer)) != -1) { out.write(byteBuffer,0,bytesRead); out.flush(); totalBytesMoved += (long) bytesRead; } nextClientSock.close(); } catch (IOException e) { System.out.println(“Socket Error");

16 Other useful methods inetAddress socket.getLocalAddress()
get the machine’s local address socket.setSoTimeOut(int milliseconds) block only for int milliseconds before returning socket.toString get ip, port in a string

17 Important Points Work with bytes, not strings, if possible
Conversions don’t always work like you think Can use BufferedReader and writer around base classes

18 Using Strings Strings must be converted to bytes
Use wrappers around basic byte stream Example: String InputLine; out = new PrintWriter(sock.getOutputStream(),true); in = new BufferedReader( new InputStreamReader( sock.getInputStream())); InputLine = in.readLine(); out.println(InputLine);

19 Network Programming & Threads
Network code involves logical simultaneous movement of data Multiple levels of movement at once E.g. From the client to server and server to client E.g Between multiple clients and servers. Clients and servers must wait for events

20 Supporting Logical Channels
Socket A Client Stream.read() Stream.write() Server Flow from client to server must operate independently of flow from server to client If read blocks, which to call first? What if read does not block?

21 Multiple Logical Channels
Socket A Client Thread Server Socket A Client Thread Need to support many channels at once

22 Concurrency in Java: Threads
Create a class that extends thread Must override the run method Instantiate an object of that class Invoking run method starts a new thread When caller returns, run method is still going! Calling join method waits for run to terminate

23 Threads in Java Class Channel extends Thread {
Channel(...) { // constructor } public void run() { /* Do work here */ /* other code to start thread */ Channel C = new Channel(); // constructor C.start(); // start new thread in run method C.join(); // wait for C’s thread to finish.

24 Threads in Java Time Calling Thread New object created
C = new Channel(…) Channel(…) C.start() New thread started Run() Work…. Work…. C.join() Run() terminates suspended

25

26 (Source: Sirak Kaewjamnong)
Socket Programming (Source: Sirak Kaewjamnong)

27 What’s a Socket? A socket is a communication mechanism that allows client/systems to be developed either locally or across networks.

28 Connection-Oriented Protocol
socket() server bind() listen() accept() client socket() blocks until connection from client connection establishment connect() data (request) read() write() data(reply) write() read()

29 Connectionless Protocol
socket() Server bind() Client socket() recvfrom() bind() blocks until data received from a client data(request) sendto() process request data(reply) sendto() recvfrom()

30 The Steps in Establishing a Socket on Server
Create a socket with the socket() system call Bind the socket to an address using the bind() system call. For a server socket, an address consists of a port number on the host machine Listen for connections with listen() system call Accept a connection with the accept() system call. This call typically blocks until a client connects with the server Send and receive data

31 The Steps in Establishing a Socket on Client
Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() or send() and recv()system calls

32 Kinds of Socket Traditional BSD families TCP/IP (AF_INET,Internet)
UNIX (AF_UNIX) XNS (AF_NS) APPLETALK,DECNET,IPX

33 Socket Address A sockadd_in is a structure containing an internet address. This structure must defined in <netinet/in.h> struct sockadd_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; };

34 Socket System Call int socket (int family, int type, int protocol);
int socket (int domain, int type, int protocol); Domain parameter specifies a communication domain;this selects the protocol family which will be used for communication Type specifies the communication semantics Protocol specifies a particular protocol to be used with the socket

35 Start Server Code(1) #include <sys/types.h>
This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files. #include <sys/socket.h> The header file socket.h includes a number of definitions of structures needed for sockets. #include <netinet/in.h> The header file in.h contains constants and structures needed for internet domain addresses.

36 Start Server Code(2) int main( ) {
int sockfd, newsockfd, portno, clilen; sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table. These two variables store the values returned by the socket system call and the accept system call. portno stores the port number on which the server accepts connections. clilen stores the size of the address of the client. This is needed for the accept system call.

37 Start Server Code(3) char buffer[256];
The server reads characters from the socket connection into this buffer struct sockaddr_in serv_addr, cli_addr; The variable serv_addr will contain the address of server The variable cli_addr will contain the address of client To create socket and return a socket descriptor that can be used for accessing the socket sockfd = socket(AF_INET,SOCK_STREAM,0); if (sockfd < 0) error(“Error opening Socket”); AF_INET is family of IPv4 Internet Protocol SOCK_STREAM is type of connection-orient(TCP) SOCK_DGRAM is type of connectionless (UDP)

38 Start Server Code(4) serv_addr.sin_family = AF_INET;
The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET. serv_addr.sin_port = htons(portno); The second field of serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field, it is necessary to convert this to network byte order using the function htons() which converts a port number in host byte order to a port number in network byte order

39 Byte Ordering different byte ordering in varoius platform
little endian : Intel 80x86 , DEC VAX , DEC PDP-11 big endian : IBM 370 , Motorola , Pyramid little endian high-order byte low-order byte addr A+1 addr A big endian high-order byte low-order byte addr A addr A+1

40 Byte Ordering Routines
Network Byte Order is big endian u_long htonl (u_long hostlong); //convert host-to-network,long int u_short htons(u_short hostshort); //convert host-to network,short int u_long ntohl (u_long netlong); // convert network-to-host,long int u_short ntohs (u_short netshort); // convert network-to-host,short integer

41 Start Server Code(5) serv_addr.sin_addr.s_addr = INADDR_ANY; The third field of sockaddr_in is a structure of type struct in_addr which contains only a single field unsigned long s_addr. This field contains the IP address of the host. For server code, this will always be the IP address of the machine on which the server is running, and there is a symbolic constant INADDR_ANY which gets this address.

42 Start Server Code(6) if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); The bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run. bind() takes three arguments, the socket file descriptor the address to which is bound the size of the address to which it is bound. The second argument is a pointer to a structure of type sockaddr, but what is passed in is a structure of type sockaddr_in, and so this must be cast to the correct type. This can fail for a number of reasons, the most obvious being that this socket is already in use on this machine

43 Start Server Code(7) listen(sockfd,5);
The listen system call allows the process to listen on the socket for connections. The first argument is the socket file descriptor The second is the size of the backlog queue: the number of connections that can be waiting while the process is handling a particular connection.The maximum size permitted by most systems. If the first argument is a valid socket, this call cannot fail, and so the code doesn't check for errors

44 Start Server Code(8) clilen = sizeof(struct sockaddr_in);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); The accept() system call causes the process to block until a client connects to the server. It wakes up the process when a connection from a client has been successfully established. It returns a new file descriptor, and all communication on this connection should be done using the new file descriptor. The second argument is a reference pointer to the address of the client on the other end of the connection The third argument is the size of this structure

45 Start Server Code(9) n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); This code reads from the socket,the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned by socket().

46 Start Server Code(10) n=write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket"); Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. This code simply writes a short message to the client. The last argument of write is the size of the message

47 Start Server Code(11) close (sockfd); close(newsockfd) } The close() system call closes a socket descriptor of sockfd and newsockfd and terminates program

48 Start Client Code(1) The header files are the same as for the server
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> The header files are the same as for the server

49 Start Client Code(2) int main(int argc, char *argv[]) { int sockfd, portno,n; struct sockaddr_in serv_addr; char buffer[256]; All of this code is the same as that in the server if (argc < 2) { fprintf(stderr,"usage %s IP address and port\n", argv[0]); exit(0); } The user needs to pass in the port number on which the server will accept connections as an argument. This code displays an error message if the user fails to do this.

50 Start Client Code(3) sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); The socket() system call creates a new socket. It takes three arguments 1st argument is family:AF_INET is family of IPv4 Internet Protocol 2nd argument is type:SOCK_STREAM is type of connection-orient(TCP), SOCK_DGRAM is type of connectionless (UDP) 3rd argument is the protocol: If this argument is zero, the operating system will choose the most appropriate protocol. It will choose TCP for stream sockets and UDP for datagram sockets.

51 Start Client Code(4) serv_addr.sin_family = AF_INET; The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET serv_addr.sin_port = htons(portno); serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field, it is necessary to convert this to network byte order using the function htons() which converts a port number in host byte order to a port number in network byte order

52 Start Client Code(5) inet_pton(AF_INET, argv[1],&serv_addr.sin_addr); This function converts the character string from first argument, Server address into a network address structure in the AF address if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); The connect function is called by the client to establish a connection to the server. It takes three arguments The socket file descriptor, The address of the host to which it wants to connect (including the port number) The size of this address. This function returns 0 on success and -1 if it fails. The client needs to know the port number of the server, but it does not need to know its own port number. This is typically assigned by the system when connect is called.

53 Start Client Code(6) n=write(sockfd,“This is my message",18); if (n < 0) error("ERROR writing to socket"); Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. This code simply writes a short message to the client. The last argument of write is the size of the message

54 Start Client Code(7) This code reads from the socket close(sockfd);
n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); This code reads from the socket close(sockfd); The close() system call closes a socket descriptor of sockfd

55 Enhancements to The Server Code
To allow the server to handle multiple simultaneous connections Put the accept statement and the following code in an infinite loop. After a connection is established, call fork() to create a new process. The child process will close sockfd and call dostuff(), passing the new socket file descriptor as an argument. When the two processes have completed their conversation, as indicated by do_stuff() returning, this process simply exits. The parent process closes newsockfd. Because all of this code is in an infinite loop, it will return to the accept statement to wait for the next connection.

56 Enhancements to The Server Code
while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd);

57 Enhancements to The Server Code
void do_stuff (int sock) { int n; char buffer[256]; n = read(sock,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(sock,"I got your message",18); if (n < 0) error("ERROR writing to socket"); }


Download ppt "CSE454 Internet Technology. Asst. Prof. Dr"

Similar presentations


Ads by Google