1 / 12

System Programming

System Programming. Practical session 10. Java sockets. Streams. The program communicates with data sources and devices using streams. System.out – standard output stream. Communication between the program and the monitor.

jcason
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programming Practical session 10 Java sockets

  2. Streams The program communicates with data sources and devices using streams. System.out – standard output stream. Communication between the program and the monitor. System.in – standard input stream. Communication between the keyboard and the program.

  3. Wrapping streams Streams can be wrapped to achieve additional functionality. printWriter userOut = new printWriter(System.out, true); BufferedReaderuserIn= newBufferedReader(newInputStreamReader(System.in)); String msg; msg = userIn.readLine(); userOut.println(msg);

  4. Networking A distributed application is an application running on several hosts in a network. Hosts communicate with each other by exchanging messages.

  5. Datagram vs. Stream • Datagram communication protocol (UDP) • Connectionless protocol. • Messages arrival and order is not guaranteed. • Fast. • Can be used for broadcasting. • Stream communication protocol (TCP) • Connection oriented. • Sent data will be received without any error and in the right order.

  6. Sockets Stream communication is performed using sockets. Host programs that have to communicate use Socket objects. To establish a connection, one host is set as a server, and waits for connections. Another host (a client) connects its socket to the server socket. Then, data can be transmitted using streams in both directions.

  7. Sockets Stream communication is performed using sockets. Host programs that have to communicate use Socket objects. To establish a connection, one host is set as a server, and waits for connections. Another host (a client) connects its socket to the server socket. Then, data can be transmitted using streams in both directions. Server Client out out in in socket socket

  8. Sockets Stream communication is performed using sockets. Host programs that have to communicate use Socket objects. To establish a connection, one host is set as a server, and waits for connections. Another host (a client) connects its socket to the server socket. Then, data can be transmitted using streams in both directions. Server Client out out in in socket socket

  9. The Client-Server Model • The connection is symmetric except of the connections establishment stage. • Connection establishment • The server waits for connection requests from clients. • The server only reacts to the initiative of the client. • To remain available for further connections, the server does not open the connection directly with the client on the incoming socket. • Instead, it creates a private socket which the client can keep connected for the whole period of the session. Servers must be built to maximize availability

  10. Socket Address A socket address is composed of a host name and a port number. A server listens to connections on a certain port. To communicate with a server, a client has to supply both the host name of the server and the port number. In the following code examples the server program is executed as follows: java EchoServer 4444 And the client is executed as follows: java EchoClient localhost 4444 The server and client can be executed on different computers, or on the same computer.

  11. Server Steps • Creating a ServerSocket. • int listenPort = 4444; • ServerSocket serverSocket=newServerSocket(listenPort); • Waiting for a client. • Socket clientSocket=serverSocket.accept(); • Once connection is established, creating input and output streams. • BufferedReader in=newBufferedReader( • newInputStreamReader(clientSocket.getInputStream(),"UTF-8")); • OutputStreamWriterout=newOutputStreamWriter( • clientSocket.getOutputStream(),"UTF-8"); • Communicating. • Closing connection.

  12. Client Steps • Creating a Socket. • int port = 4444; • String host = “localhost”; • Socket clientSocket=newSocket(host,port); • Or • InetAddress address = InetAddress.getByName(“127.0.0.1”); • Socket clientSocket=newSocket(address,port); • Creating input and output streams. • Communicating. • Closing connection.

More Related