22
Referring to Java API Specifications http://java.sun.com/j2se/1. 4.1/docs/ api

Referring to Java API Specifications

Embed Size (px)

Citation preview

Page 1: Referring to Java API Specifications

Referring to Java API Specifications

http://java.sun.com/j2se/1.4.1/docs/api

Page 2: Referring to Java API Specifications

Java Network Programming

Page 3: Referring to Java API Specifications

Representing IP address InetAddress

public static InetAddress getByName(String host)

InerAddress a = InetAddress.getByName(“compserv1.cs.sunysb.edu”);

InerAddress a = InetAddress.getByName(“localhost”);

Page 4: Referring to Java API Specifications

TCP Sockets

Client Server

Socket

ServerSocketaccept()

Page 5: Referring to Java API Specifications

Sockets

Client Server

Socket

ServerSocket

connect()

Page 6: Referring to Java API Specifications

Sockets

Client Server

Socket

ServerSocket

connect()

Socket

Page 7: Referring to Java API Specifications

Sockets

Client Server

Socket

ServerSocket

Socket

accept()

Page 8: Referring to Java API Specifications

Socket

• public Socket()– Creates an unconnected socket.

• public Socket(String host, int port)

– Creates a stream (TCP) socket and connects it to the specified port number on the named host.

Page 9: Referring to Java API Specifications

Socket…• void bind(SocketAddress bindpoint)

          Binds the socket to a local address.

•  void close()           Closes this socket.

•  void connect(SocketAddress endpoint)           Connects this socket to the server.

•  void connect(SocketAddress endpoint, int timeout)           Connects this socket to the server with a timeout.

Page 10: Referring to Java API Specifications

ServerSocket• ServerSocket()

          Creates an unbound server socket.

• ServerSocket(int port)           Creates a server socket, bound to the port.

• ServerSocket(int port, int backlog)           Creates a server socket and binds it to the local port number, with the backlog.

• ServerSocket(int port, int backlog, InetAddress bindAddr)           Create a server with the port, listen backlog, and local IP address to bind to.

Page 11: Referring to Java API Specifications

ServerSocket…•  Socket accept()

          Listens for a connection and accepts it.

•  void bind(SocketAddress endpoint)           Binds to an address.

•  void bind(SocketAddress endpoint, int backlog)           Binds to an address with backlog limit.

•  void close()           Closes this socket.

Page 12: Referring to Java API Specifications

TCP Echo Server (again!)

Page 13: Referring to Java API Specifications

import java.io.*;import java.net.*;

public class EchoServer { public static final int PORT = 8080;

public static void main(String[] args) throws IOException {ServerSocket listen_sock = new ServerSocket(PORT); try {

// Block until a connection occurs: Socket socket = listen_sock.accept(); try {

// Connection accepted: BufferedReader in = new BufferedReader(

new InputStreamReader( socket.getInputStream()));

Page 14: Referring to Java API Specifications

// Output is automatically flushed by PrintWriter: PrintWriter out = new PrintWriter(

new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())

) , true);

// echo back and forthwhile (true) {

String str = in.readLine(); if (str.equals("END")) break;out.println(str);

}

} finally { System.out.println("closing..."); socket.close();

}

} finally { listen_sock.close(); }

}

Page 15: Referring to Java API Specifications

TCP Echo Client

Page 16: Referring to Java API Specifications

import java.net.*; import java.io.*; public class EchoClient {

public static void main(String[] args) throws IOException {

InetAddress addr = InetAddress.getByName(“localhost”);

// Create a socket and connect to remote serverSocket socket = new Socket(addr, EchoServer.PORT);

try { // Get the input streamBufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

Page 17: Referring to Java API Specifications

// Get the output stream

PrintWriter out = new PrintWriter( new BufferedWriter(

new OutputStreamWriter(socket.getOutputStream())

), true);

for(int i = 0; i < 10; i ++) {out.println(“Hello " + i); String str = in.readLine();System.out.println(str);

} out.println("END");

} finally {socket.close(); } }

Page 18: Referring to Java API Specifications

DatagramSocket

• DatagramSocket()– Constructs a datagram socket and binds it to any available port.

•  DatagramSocket(int port) – Constructs a datagram socket and binds it to the specified port.

•  DatagramSocket(int port, InetAddress laddr)– Creates a datagram socket, bound to the specified local address.

•  DatagramSocket(SocketAddress bindaddr)– Creates a datagram socket, bound to the specified local address.

Page 19: Referring to Java API Specifications

DatagramPacket

• DatagramPacket(byte[] buf, int length)

– Constructs a DatagramPacket for receiving packets.

• DatagramPacket(byte[] buf, int length, InetAddress address, int port)

– Constructs a datagram packet for sending packets to the specified address and port.

Page 20: Referring to Java API Specifications

UDP Echo Server example

Page 21: Referring to Java API Specifications

// buffer to receive databyte[] buf = new byte[1000]; // buffer

// create packet for receivingDatagramPacket recv_dp = new DatagramPacket(buf, buf.length);

// create socket for sendingDatagramSocket socket;

try {socket = new DatagramSocket(INPORT);while(true) {

// Block until a datagram appears:socket.receive(recv_dp);

// Extract the string receivedString str = new String(recv_dp.getData(), 0,

recv_dp.getLength());

Page 22: Referring to Java API Specifications

//Construct the datagram to sendDatagramPacket echo = new DatagramPacket(

str.getBytes(0, str.length(), buf, 0), str.length,

recv_dp.getAddress(), recv_dp.getPort());

// send it backsocket.send(echo);

}} catch(SocketException e) { System.err.println("Can't open socket"); System.exit(1);} catch(IOException e) { System.err.println("Communication error");

System.exit(1);}