120 likes | 140 Views
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.
E N D
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. System.in – standard input stream. Communication between the keyboard and the program.
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);
Networking A distributed application is an application running on several hosts in a network. Hosts communicate with each other by exchanging messages.
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.
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.
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
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
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
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.
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.
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.