Presentation is loading. Please wait.

Presentation is loading. Please wait.

L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.

Similar presentations


Presentation on theme: "L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when."— Presentation transcript:

1 L 2 - 1 3( 1/ 20) : Java Network Programming

2 The Socket API The previous contents describe client-server interaction that application programs use when they communicate across an internet. Here, we show how clients and servers use transport protocols to communicate, and gives two complete programs for both client and server. API: Application Program Interface. -is the interface that an application uses when it interacts with transport protocol software. Although protocol standards allow OS designers to choose an API, many have adopted the socket API. Originally designed as part of the BSD UNIX OS that contained TCP/IP internet working protocol. Instead of modifying their basic OS, vendors created a socket library that provides the socket API.

3 The JAVA Socket API Java’s Socket class, which is used by both clients and servers, has methods that correspond to the first four of these operations. The last three operations are only needed by servers that need to wait for clients to connect to them. They are implemented by the ServerSocket class. The Java 2 platform API specification can be found from the link below: Java 2 Platform API Specification Sockets shield the programmer from low-level details, like media types, network address, … A socket can perform seven basic operations: connect to a remote machine send data receive data close a connection bind to a port listen for incoming data accept connection from remote machine on the bound port

4 The Socket/ServerSocket/DatagramSocket Class (in Java) Constructors : many constructors are available but some are introduced below. public Socket(String host, int port) Creates a stream socket and connects it to the specified port number on the named host. public Socket(InetAddress host, int port) Creates a stream socket and connects it to the specified port number at the specified IP address. public ServerSocket(int port) Creates a server socket on a specified port. public ServerSocket(int port, int backlog) Creates a server socket and binds it to the specified local port number. public DatagramSocket() Constructs a datagram socket and binds it to any available port on the local host machine. public DatagramSocket(int port) Constructs a datagram socket and binds it to the specified port on the local host machine.

5 The Socket Class methods Methods : many methods are available for use but some are introduced below. Socket theSocket = new Socket(“www.k.hosei.ac.jp”, 80); // for example public int getInetAddress() Returns the address to which the socket is connected. e.g. InetAddress host = theSocket.getInetAddress(); public int getPort() Returns the remote port to which this socket is connected. e.g. int port = theSocket.getPort(); public InputStream getInputStream() Returns an input stream for this socket. e.g. InputStream input = theSocket.getInputStream(); public OutputStream getOutputStream() Returns an output stream for this socket. e.g. OutputStream output = theSocket.getOutputStream(); public void close() close this socket. e.g. theSocket.close();

6 The ServerSocket/DatagramSocket Class methods (in Java) Methods : many methods are available for use but some are introduced below. public Socket accept() Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. e.g. ServerSocket server_socket = new ServerSocket(); Socket connection = server_socket.accept(); public void receive(DatagramPacket p) Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine. e.g. DatagramSocket DSocket = new DatagramSocket(5000); DatagramPacket receivePacket = new DatagramPacket(data, data.length); DSocket.receive(receivePacket); public void send(DatagramPacket p) Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host. e.g. DatagramPacket sendPacket = new DatagramPacket(data, data.length); DSocket.send(sendPacket);

7 Example: Stream Socket Connections (TCP: transmission control protocol ) The basic life cycle of a server is: ServerTCP.java ServerTCP.java A creation of a ServerSocket -- ServerSocket server = new ServerSocket(5000,100); Listen for incoming connection -- Socket connection = server.accept(); Receive data -- DataInputStream input = new DataInputStream(connection.getInputStream()); input.readUTF(); Send data -- DataOutputStream output = new DataOutputStream(connection.getOutputStream()); output.writeUTF(“connected!”); Close the connection -- connection.close(); Wait for the next connection. What a client does: ClientTCP.java ClientTCP.java Connect to a remote machine -- Socket client = new Socket(InetAddress.getLocalHost(),5000); Receive data -- DataInputStream input = new DataInputStream(client.getInputStream()); input.readUTF(); Send data -- DataOutputStream output = new DataOutputStream(client.getOutputStream()); output.writeUTF(“Thanks!”); Close a connection -- client.close(); Server Client 5000 Input.readUTF( ) Output.writeUTF Input.readUTF( ) Socket client Socket connection CloseWindowAndExit.java

8 Example: Connectionless Transmission with Datagrams (UDP: User Datagram Protocol ) The basic life cycle of a server is: ServerUDP.java ServerUDP.java A creation of a DatagramSocket -- DatagramSocket server_socket = new DatagramSocket(5000); Set up packet -- byte data[] = new byte[100]; DatagramPacket receivePacket = new DatagramPAcket(data, data.length); Wait for packet -- server_socket.receive(receivePacket); Process packet -- receivePacket.getAddess(), receivePacket.getPort(), (display or output received packet) receivePacket.getLength(), receivPacket.getData() Send packet back to client -- DatagramPacket sendPacket = new DatagramPacket(……); server_socket.send(sendPacket); Wait for next packet …… What a client does is similar to the server: ClientUDP.java ClientUDP.java Create a Datagrame socket -- DatagramSocket client_socket = new DatagramSocket(); Set up packet -- byte data[] = new byte[100]; receivePacket = new DatagramPacket(data, data.length); Wait for packet -- client_socket.receive(receivePacket); Process packet -- receivePacket.getAddess(), receivePacket.getPort(), (display or output received packet) receivePacket.getLength(), receivPacket.getData() Send packet back to client -- DatagramPacket sendPacket = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5000); client_socket.send(); Wait for next packet …… Server Client 5000 client_socket.send(sendPacket) client_socket.receive(receivPacket)server_socket.send(sendPacket) server_socket.receive(receivPacket) ? ?=receivePacket.getPort() CloseWindowAndExit.java

9 Exercises Ex1. Run server and client programs based on TCP and modify the server program so that you can output the internet address and port number the server is connected to. Ex2. (optional?) Modify server and client programs so that they can continue to communications without disconnection. Ex3. (optional?) Add “disconnection” button. When pressing the button, the connection between the server and client is disconnected. You can get the source files: ServerTCP.java ClientTCP.javaServerTCP.javaClientTCP.java


Download ppt "L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when."

Similar presentations


Ads by Google