Network Programming - PowerPoint PPT Presentation

network programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
Network Programming PowerPoint Presentation
Download Presentation
Network Programming

play fullscreen
1 / 94
Network Programming
Download Presentation
Download Presentation

Network Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Network Programming James Atlas July 10, 2008

  2. Review • Multi-Threaded Programming • Processes • Threads (light-weight processes) • Synchronization James Atlas - CISC370

  3. Today • Network programming • Connection and Connectionless Sockets • Classroom network James Atlas - CISC370

  4. Network Programming and Java • Computer networks • each computer is independent • some method of communication exists between them • Java abstracts the details • No need to know about the underlying network and how the operating system interacts with it • Hidden and encapsulated in the package • Makes network programming easier James Atlas - CISC370

  5. Distributed Programming (Client/Server) • Server application provides a service • Client program(s) communicates with server application Client Network Server Client Client Application code James Atlas - CISC370

  6. Distributed Programming (Peer-to-Peer) • Application has both client and server properties Network Application Client Application Client Server Server Application Client Server James Atlas - CISC370

  7. Network Addresses • Uniquely identifies the computer (host) • Most common address system in use today is the Internet Protocol (IPv4) addressing system • a 32-bit address, typically written as a “dotted-quad”: four numbers, 0 through 254, separated by dots, e.g., James Atlas - CISC370

  8. Ports • Each host on the network has a set of ports • Ports are like mailboxes: the address specifies the host, the port specifies the application on the host • Ports range from 1 to 65537 • These ports allow multiple applications to use the same network interface/address to communicate over the network. • For example • a web server will communicate on the network using port 80 • an FTP server on the same host will have the same address but use port 21 James Atlas - CISC370

  9. A Machine’s Ports “Mailboxes” 21 FTP server 23 Telnet server Network 80 HTTP server 3477 HTTP client James Atlas - CISC370

  10. Well-Known Ports • Port numbers < 1024 are well-known ports • Well-known ports are assigned to application servers • Port 80 always has an HTTP server • Port 21 always has an FTP server • Client listens on another port (above 1024) to receive responses from a server • No technical reason servers must conform to these standards • A convention so that clients will always know where the web server is, where the FTP server is, … • Can have an HTTP server at a port > 1024 James Atlas - CISC370

  11. Sockets • A socket is an abstraction of an endpoint of a two-way communications link • An application creates a socket that is bound to a remote address and remote port. • port on the host (client) could be random • a “connection” is created between the client using this port and the specified remote address at the remote port James Atlas - CISC370

  12. Services provided by networks • Connection-oriented • Connection-less James Atlas - CISC370

  13. Connection-Oriented Service • A connection-oriented service is like the telephone system • acts like a pipe • sender pushes object bits into the pipe and then come out of the receiver in the same condition as they were pushed in • pipe is connected to one port on the sender and one port on the receiver • Implemented in Java using stream sockets James Atlas - CISC370

  14. Stream Sockets • After a stream socket is open on both the server and the client and the sockets connect to each other, a pipe connects endpoints and provides a reliable byte stream • Transmission Control Protocol (TCP) • most popular protocol that implements a stream, or connection-oriented, service • Java uses to implement stream sockets • Reliable service • when something is sent from one end to the other, it arrives in order, in the same state, and is not lost or duplicated in the network James Atlas - CISC370

  15. Stream Sockets • Stream Socket: communicates using TCP • Hides details of TCP from programmer Network Server Client Socket James Atlas - CISC370

  16. Connectionless Service • A connectionless service is like the postal system • One side sends messages to the other side • Each message is independent • Can lose messages in the network, duplicate messages, corrupt data during transport • An unreliable service • although most of the time this bad stuff does not happen -- much more reliable underlying network • One side creates a message and sends it to the other side James Atlas - CISC370

  17. Datagram Sockets • User Datagram Protocol (UDP) • Popular protocol that Java uses to implement datagram sockets • Unreliable: All sorts of things can happen to the messages during transport in the network (although most of the time, they get there just fine). • No connection between these sockets • A socket is opened to another socket, but no connection is actually made • When a message is passed to the socket, it is sent over the network to the other socket. Most of the time it gets there. James Atlas - CISC370

  18. Example Java Client Program • Connect to a server (another host on the network) • Open a stream to a certain port • Display what the server sends James Atlas - CISC370

  19. public class SocketTest { public static void main(String[] args) { try { Socket s = new Socket( “”, 13); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); boolean more = true; while (more) { String line = in.readLine(); if (line == null) more = false; else System.out.println(line); } } catch (IOException exp) { System.out.println(“Error:” + exp); } } } James Atlas - CISC370

  20. Reading from a Socket Socket s = new Socket( “”, 13); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); • The first line creates a socket that connects to the host with the specified name at port 13 on that host • getInputStream() is called on the socket to get a byte stream that reads from the socket • An InputStreamReader wraps the byte stream and a BufferedReader wraps the InputStreamReader • The BufferedReader reads all characters sent by the server using readLine() and displays each line to System.out. James Atlas - CISC370

  21. Network I/O and Exceptions • All of the networking code in this example is inside of a try block • A number of things can go wrong with network communications • a power failure knocking out an intermediate router or switch • a misconfiguration, • someone tripping over a cable • If any of these errors are detected, an IOException is generated, so any program performing such functionality should handle such exceptions James Atlas - CISC370

  22. Host Names and IP Addresses • A name is provided to the socket constructor • not an IP address • called a host name • Java uses the Domain Name Service (DNS) to resolve the host name into an IP address • connect to the host using the IP address • Usually, you will not work directly with IP addresses • You can connect a socket using a host’s IP address James Atlas - CISC370

  23. Host Names and IP Addresses • InetAddress’s static method, getByName(). • For example, will return an InetAddress object that encapsulates the sequence of four bytes InetAddress addr = InetAddress.getByName( “”); James Atlas - CISC370

  24. Multiple IP Addresses per Host • A host can have more than one IP address • facilitate load-balancing. • For example, currently corresponds to 8 different IP addresses • one can be picked at random whenever the host is accessed (usually just the first) • To determine all of the IP addresses of a specific host, call getAllByName()… InetAddress[] addresses = InetAddress.getAllByName( “”); James Atlas - CISC370

  25. The Loopback Address and localhost • To get information about the machine the program is running on, the hostname localhost always represents the local host • Hostname corresponds to the IP address, which is known as the loopback address. • a special IP address that means “the computer connected right here” James Atlas - CISC370

  26. Determining the Local Address • If the program calls getByName() with localhost, the returned IP address is • To get the actual IP address of the host, call getLocalHost() • returns the actual IP address of the host on the network • For example… InetAddress address = InetAddress.getLocalHost(); James Atlas - CISC370

  27. A Bi-Directional Client • Our simple client program connects to a server and displays what the server sent back • After the server finishes, the client disconnects • Often, the client wants to send data to the server as well as receive data from the server • Sockets are bi-directional • Need to open an output stream on the socket James Atlas - CISC370

  28. This test program opens both input and output streams on the same socket – to both read from and write to the server. public class BidirSocketTest { public static void main(String[] args) { try { Socket s = new Socket( “”, 13); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter( s.getOutputStream(), true); // auto-flush // read from in (input – received from server) // write to out (output – send to server) } catch (IOException exp) { System.out.println(“Error:” + exp); } } } James Atlas - CISC370

  29. Clients and Servers • When we open a connection, it is made to a host at a certain address to a certain port. • For this to work, the server on the remote host must be listening to that port and wait for a client to connect to that port • the server obtains a socket that is an abstraction of its end of the stream, connected to the connecting client James Atlas - CISC370

  30. The Socket Abstraction • Each end of socket has input/output Network Server Client Socket James Atlas - CISC370

  31. The ServerSocket Class • Server programs (programs that listen to a port for a connection request) are implemented using the ServerSocket class. • A ServerSocket object is created by specifying the port number to listen for connections on… • creates a server socket on port 1998 • not a well-known port number because it is > 1024 • Server object listens for connection requests on this port ServerSocket svr1 = new ServerSocket(1998); James Atlas - CISC370

  32. Accepting a Connection • The server program can wait for a client request to connect on that port by calling accept() • blocking method that waits indefinitely until a client attempts to connect to the port • When client connects, accept() returns a Socket object, which is how the server communicates with the client… // will block until a client connects Socket incoming = svr1.accept(); James Atlas - CISC370

  33. Example: An Echo Server • Create a simple server that will wait for a client to connect • When a client connects, the server will read a line from the client and then return a line identical to what it has received. • Known as an echo server because it echoes back what it receives from the client • As an added twist, echo server will echo back what it receives in all capital letters James Atlas - CISC370

  34. public class CapsEchoServer { public static void main(String[] args) { try { ServerSocket svr1 = new ServerSocket(1998); Socket incoming = svr1.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()); PrintWriter out = new PrintWriter( incoming.getOutputStream(), true); out.println(“CAPS Echo Server. Type BYE to exit”); boolean done = false; while (!done) { String line = in.readLine(); if (line == null) done = true; else if (line.trim().equals(“BYE”)) done = true; else out.println(“Echo:” + line.trim().toUpperCase()); } incoming.close(); } catch (IOException exp) { System.out.println(“Error:” + exp); } }} James Atlas - CISC370

  35. Example: An Echo Server • Purpose of a ServerSocket object is to wait for connections • When a client connects, it generates a new Socket object, which is the server’s endpoint of the connection, and returns the from the call to accept() • Suppose we would like to allow multiple clients to connect to the echo server at the same time James Atlas - CISC370

  36. Servers and Multiple Clients • Servers should handle multiple concurrent clients • If a server only allowed one client to connect at any given time, any client can monopolize the service by remaining connected to the server for a long time • How would you implement this behavior? James Atlas - CISC370

  37. Servers and Multiple Clients • After the server returns from accept() with the Socket object, the server can start a newthread to handle the connection between the server and this client • The main server program can go back and call accept() again, waiting for a new client to connect James Atlas - CISC370

  38. A Multithreaded Server while (true) { Socket incoming = svr1.accept(); Thread clientThread = new ThreadedEchoHandler(incoming); clientThread.start(); } • User-defined ThreadedEchoHandler class derives from Thread • the client communication loop is its run() method… James Atlas - CISC370

  39. class ThreadedEchoHandler extends Thread { ThreadedEchoHandler(Socket incoming) { this.incoming = incoming; } public void run() { try { BufferedReader in = new BufferedReader( new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter( incoming.getOutputStream()); boolean done = false; while (!done) { String line = in.readLine(); if (line == null) done = true; else if (line.trim().equals(“BYE”)) done = true; else out.println(“Echo:” + line.trim().toUpper(); } incoming.close(); } catch (IOException exp) { System.out.println(“Error:” + exp); } } Socket incoming; } James Atlas - CISC370

  40. A Multithreaded Server • Each connection starts a new thread • multiple clients can connect to the server at the same time • As soon as a client connects, accept() returns a Socket that encapsulates this new connection • socket is passed into a new thread to handle the connection • The thread is then started and deals with the connection from then on • The main thread goes back to waiting for a new connection James Atlas - CISC370

  41. Multithreaded Server Issues • Any problems with having a thread handle each incoming request? • Performance James Atlas - CISC370

  42. Multithreaded Server Issues • For each request, must create a thread • Overhead in creating threads • What happens if receive too many client requests and have to start/fork too many threads? • Machine runs out of memory • Machine gets bogged down • Threads can’t make progress James Atlas - CISC370

  43. Multi-threaded Server Solutions • Solution: Limit the number of incoming connections/threads available • new ServerSocket( int port, int backlog ) • The maximum length of the queue • After <backlog> requests, additional requests are refused • Create a thread pool • Create available threads at startup • Get one of these threads when to handle requests • See java.util.concurrent.Executors James Atlas - CISC370

  44. Other Types of Network Streams • So far, we have connected BufferedReaders and PrintWriters to our socket’s input and output streams • To receive and send text from the streams • Reader/Writer classes handle text • Socket Streams can be wrapped by ObjectStreams or DataInput/OutputStreams James Atlas - CISC370

  45. Socket Timeouts • In a real-life situation, reading from a socket indefinitely is a bad idea • the network could go down, causing the program to wait on the socket forever. • Java supports a timeout value • If the program has been waiting for the socket for the specified timeout interval, a InterruptedIOException is generated • Timeout value is set by calling setSoTimeout() on the socket James Atlas - CISC370

  46. Socket Timeouts Socket sckt1 = new Socket(. . . ); sckt1.setSoTimeout(10000); // 10 second timeout try { String line; while ((line = in.readLine()) != null) { process received data } } catch (InterruptedException) { System.out.println( “The socket timeout has been reached.”); } James Atlas - CISC370

  47. Socket Timeout Limitations • Socket constructor automatically attempts to connect • What problem could this cause? James Atlas - CISC370

  48. Socket Timeout Limitations • Socket constructor automatically attempts to connect • What problem could this cause? • will block until the socket is initially connected • In Java 1.4 they added a default constructor • connect(SocketAddress, timeout) can now handle timing out a connection attempt James Atlas - CISC370

  49. Datagram Communications • Let’s look at connectionless communications using datagrams • specifies the creation of individual messages, called datagrams, which are transmitted one at a time, from one host to the other James Atlas - CISC370

  50. Datagram Communications • Two primary classes deal with datagram communications • DatagramPacket and DatagramSocket • No server socket class • No connections in datagram communications • packets are simply transmitted from one host to another • To transmit a datagram, the program should first construct a DatagramPacket object and then deliver it to a DatagramSocket James Atlas - CISC370