Download presentation
Presentation is loading. Please wait.
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 printWriter userOut = new printWriter(System.out, true); BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in)); Streams can be wrapped to achieve additional functionality. 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 socket in out in out
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 socket in out in out
9
The Client-Server Model The connection is symmetric except of the connections establishment stage. Connection establishment 1.The server waits for connection requests from clients. 2.The server only reacts to the initiative of the client. 3.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:following code examples 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 1.Creating a ServerSocket. int listenPort = 4444; ServerSocket serverSocket = new ServerSocket(listenPort); 2.Waiting for a client. Socket clientSocket = serverSocket.accept(); 3.Once connection is established, creating input and output streams. BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream(),"UTF-8")); OutputStreamWriter out = new OutputStreamWriter( clientSocket.getOutputStream(),"UTF-8"); 4.Communicating. 5.Closing connection.
12
Client Steps 1.Creating a Socket. int port = 4444; String host = “ localhost ” ; Socket clientSocket = new Socket(host, port); Or InetAddress address = InetAddress.getByName( “ 127.0.0.1 ” ); Socket clientSocket = new Socket(address, port); 2.Creating input and output streams. 3.Communicating. 4.Closing connection.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.