Download presentation
Presentation is loading. Please wait.
1
Referring to Java API Specifications http://java.sun.com/j2se/1.4.1/docs/api
2
Java Network Programming
3
Representing IP address InetAddress InetAddress public static InetAddress getByName(String host)InetAddressString InerAddress a = InetAddress.getByName(“compserv1.cs.sunysb.edu”); InerAddress a = InetAddress.getByName(“localhost”);
4
TCP Sockets Client Server Socket ServerSocket accept()
5
Sockets Client Server Socket ServerSocket connect()
6
Sockets Client Server Socket ServerSocket connect() Socket
7
Sockets Client Server Socket ServerSocket Socket accept()
8
Socket public Socket() –Creates an unconnected socket. public Socket(String host, int port)String –Creates a stream (TCP) socket and connects it to the specified port number on the named host.
9
SocketSocket… void bind(SocketAddress bindpoint) Binds the socket to a local address.bindSocketAddress void close() Closes this socket.close void connect(SocketAddress endpoint) Connects this socket to the server.connectSocketAddress void connect(SocketAddress endpoint, int timeout) Connects this socket to the server with a timeout.connectSocketAddress
10
ServerSocket ServerSocket() Creates an unbound server socket.ServerSocket ServerSocket(int port) Creates a server socket, bound to the port.ServerSocket ServerSocket(int port, int backlog) Creates a server socket and binds it to the local port number, with the backlog.ServerSocket ServerSocket(int port, int backlog, InetAddress bindAddr) Create a server with the port, listen backlog, and local IP address to bind to.ServerSocket InetAddress
11
ServerSocketServerSocket… Socket accept() Listens for a connection and accepts it. Socketaccept void bind(SocketAddress endpoint) Binds to an address.bindSocketAddress void bind(SocketAddress endpoint, int backlog) Binds to an address with backlog limit.bindSocketAddress void close() Closes this socket.close
12
TCP Echo Server (again!)
13
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()) );
14
// Output is automatically flushed by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()) ), true); // echo back and forth while (true) { String str = in.readLine(); if (str.equals("END")) break; out.println(str); } } finally { System.out.println("closing..."); socket.close(); } } finally { listen_sock.close(); } }
15
TCP Echo Client
16
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 server Socket socket = new Socket(addr, EchoServer.PORT); try { // Get the input stream BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream()));
17
// 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(); } }
18
DatagramSocket DatagramSocket()DatagramSocket –Constructs a datagram socket and binds it to any available port. DatagramSocket(int port)DatagramSocket –Constructs a datagram socket and binds it to the specified port. DatagramSocket(int port, InetAddress laddr)DatagramSocketInetAddress –Creates a datagram socket, bound to the specified local address. DatagramSocket(SocketAddress bindaddr)DatagramSocketSocketAddress –Creates a datagram socket, bound to the specified local address.
19
DatagramPacket DatagramPacket(byte[] buf, int length)DatagramPacket –Constructs a DatagramPacket for receiving packets. DatagramPacket(byte[] buf, int length, InetAddress address, int port)DatagramPacketInetAddress –Constructs a datagram packet for sending packets to the specified address and port.
20
UDP Echo Server example
21
// buffer to receive data byte[] buf = new byte[1000]; // buffer // create packet for receiving DatagramPacket recv_dp = new DatagramPacket(buf, buf.length); // create socket for sending DatagramSocket socket; try { socket = new DatagramSocket(INPORT); while(true) { // Block until a datagram appears: socket.receive(recv_dp); // Extract the string received String str = new String(recv_dp.getData(), 0, recv_dp.getLength());
22
//Construct the datagram to send DatagramPacket echo = new DatagramPacket( str.getBytes(0, str.length(), buf, 0), str.length, recv_dp.getAddress(), recv_dp.getPort()); // send it back socket.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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.