งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

The ServerSocket Class ใช้ในการจัดทำเครื่องที่เป็นการบริการ ใช้ในการจัดทำเครื่องที่เป็นการบริการ โดยจะมี ช่วงชีวิตดังนี้ โดยจะมี ช่วงชีวิตดังนี้

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "The ServerSocket Class ใช้ในการจัดทำเครื่องที่เป็นการบริการ ใช้ในการจัดทำเครื่องที่เป็นการบริการ โดยจะมี ช่วงชีวิตดังนี้ โดยจะมี ช่วงชีวิตดังนี้"— ใบสำเนางานนำเสนอ:

1 The ServerSocket Class boonrit@feu.ac.th

2 ใช้ในการจัดทำเครื่องที่เป็นการบริการ ใช้ในการจัดทำเครื่องที่เป็นการบริการ โดยจะมี ช่วงชีวิตดังนี้ โดยจะมี ช่วงชีวิตดังนี้ 1. สร้าง object ServerSocket 2. รอรับการเชื่อมต่อกับเครื่อง client accept( ) 3. สร้าง Stream รับส่งข้อมูลโดยใช้ getInputStream() method, getOutputStream( ) 4. รับส่งข้อมูลระหว่าง Client กับ Server 5. Server และ Client ทำการปิดการเชื่อมต่อ close the connection. 6. กลับไป ขั้นตอนที่ 2 รอการเชื่อมต่อใหม่

3 The Constructors public ServerSocket(int port) throws BindException, IOException public ServerSocket(int port) throws BindException, IOException public ServerSocket(int port, int queueLength) public ServerSocket(int port, int queueLength) throws BindException, IOException throws BindException, IOException public ServerSocket(int port, int queueLength, InetAddress bindAddress) public ServerSocket(int port, int queueLength, InetAddress bindAddress) throws IOException throws IOException public ServerSocket( ) throws IOException // Java 1.4 public ServerSocket( ) throws IOException // Java 1.4

4 public ServerSocket(int port) throws BindException, IOException try { try { ServerSocket httpd = new ServerSocket(80); ServerSocket httpd = new ServerSocket(80); } catch (IOException ex) { catch (IOException ex) { System.err.println(ex); System.err.println(ex); }

5 import java.net.*; import java.net.*; import java.io.*; import java.io.*; class localPortScanner { class localPortScanner { public static void main(String[] args) { public static void main(String[] args) { for (int port = 1; port <= 65535; port++) { for (int port = 1; port <= 65535; port++) { try { try { // the next line will fail and drop into the catch block if // the next line will fail and drop into the catch block if // there is already a server running on the port // there is already a server running on the port ServerSocket server = new ServerSocket(port); ServerSocket server = new ServerSocket(port); } } catch (IOException ex) { catch (IOException ex) { System.out.println("There is a server on port " + port + "."); System.out.println("There is a server on port " + port + "."); } // end catch } // end catch } // end for } // end for } } }

6 There is a server on port 135. There is a server on port 135. There is a server on port 1025. There is a server on port 1025. There is a server on port 1026. There is a server on port 1026. There is a server on port 1027. There is a server on port 1027. There is a server on port 1028. There is a server on port 1028.

7 Accepting and Closing Connections ในการทำงานของ server จะมีการรอรับการ ติดต่อกับ เครื่อง Client โดยที่ ใช้ accept( ) method ซึ่งจะคืนค่าของ Object ของ Socket คืนมาให้ เป็นการเริ่มการเชื่อมต่อ ในการทำงานของ server จะมีการรอรับการ ติดต่อกับ เครื่อง Client โดยที่ ใช้ accept( ) method ซึ่งจะคืนค่าของ Object ของ Socket คืนมาให้ เป็นการเริ่มการเชื่อมต่อ เมื่อทำการเชื่อมต่อแล้วต้องการยกเลิกการ ติดต่อ ต้องใช้ Method Close() เมื่อทำการเชื่อมต่อแล้วต้องการยกเลิกการ ติดต่อ ต้องใช้ Method Close()

8 ServerSocket server = new ServerSocket(5776); ServerSocket server = new ServerSocket(5776); while (true) { while (true) { Socket connection = server.accept( ); Socket connection = server.accept( ); OutputStreamWriter out OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream( )); = new OutputStreamWriter(connection.getOutputStream( )); out.write("You've connected to this server. Bye-bye now.\r\n"); out.write("You've connected to this server. Bye-bye now.\r\n"); connection.close( ); connection.close( ); }

9

10 SO_TIMEOUT public void setSoTimeout(int timeout) throws SocketException public void setSoTimeout(int timeout) throws SocketException public int getSoTimeout( ) throws IOException public int getSoTimeout( ) throws IOException

11 SO_TIMEOUT try try { { ServerSocket server = new ServerSocket(2048); server.setSoTimeout(30000); // block for no more than 30 seconds ServerSocket server = new ServerSocket(2048); server.setSoTimeout(30000); // block for no more than 30 seconds try { try { Socket s = server.accept( ); Socket s = server.accept( ); // handle the connection // handle the connection } } catch (InterruptedIOException ex) { catch (InterruptedIOException ex) { System.err.println("No connection within 30 seconds"); System.err.println("No connection within 30 seconds"); } } finally { finally { server.close( ); server.close( ); } } catch (IOException ex) { catch (IOException ex) { System.err.println("Unexpected IOException: " + e); System.err.println("Unexpected IOException: " + e); }

12 try { try { ServerSocket server = new ServerSocket(2048); ServerSocket server = new ServerSocket(2048); server.setSoTimeout(30000); // block for no more than 30 seconds server.setSoTimeout(30000); // block for no more than 30 seconds int timeout = server.getSoTimeOut( ); int timeout = server.getSoTimeOut( ); if (timeout > 0) { if (timeout > 0) { System.out.println(server + " will time out after " System.out.println(server + " will time out after " + timeout + "milliseconds."); + timeout + "milliseconds."); } } else if (timeout == 0) { else if (timeout == 0) { System.out.println(server + " will never time out."); System.out.println(server + " will never time out."); } } else { else { System.out.println("Impossible condition occurred in " + server); System.out.println("Impossible condition occurred in " + server); System.out.println("Timeout cannot be less than zero." ); System.out.println("Timeout cannot be less than zero." ); } } }

13 import java.net.*; import java.net.*; import java.io.*; import java.io.*; //import com.macfaq.io.SafeBufferedReader; // from Chapter 4 //import com.macfaq.io.SafeBufferedReader; // from Chapter 4 class clientTest { class clientTest { public static void main(String[] args) { public static void main(String[] args) { int port; int port; try { try { port = Integer.parseInt(args[0]); port = Integer.parseInt(args[0]); } } catch (Exception ex) { catch (Exception ex) { port = 0; port = 0; } } try { try { ServerSocket server = new ServerSocket(port, 1); ServerSocket server = new ServerSocket(port, 1); System.out.println("Listening for connections on port " System.out.println("Listening for connections on port " + server.getLocalPort( )); + server.getLocalPort( )); while (true) { while (true) { Socket connection = server.accept( ); Socket connection = server.accept( ); try { try { System.out.println("Connection established with " System.out.println("Connection established with " + connection); + connection); Thread input = new InputThread(connection.getInputStream( )); Thread input = new InputThread(connection.getInputStream( )); input.start( ); input.start( ); Thread output Thread output = new OutputThread(connection.getOutputStream( )); = new OutputThread(connection.getOutputStream( )); output.start( ); output.start( ); // wait for output and input to finish // wait for output and input to finish try { try { input.join( ); input.join( ); output.join( ); output.join( ); } } catch (InterruptedException ex) { catch (InterruptedException ex) { } } catch (IOException ex) { System.err.println(ex); } finally { try { if (connection != null) connection.close( ); } catch (IOException ex) {} } catch (IOException ex) { e.printStackTrace( ); }

14


ดาวน์โหลด ppt The ServerSocket Class ใช้ในการจัดทำเครื่องที่เป็นการบริการ ใช้ในการจัดทำเครื่องที่เป็นการบริการ โดยจะมี ช่วงชีวิตดังนี้ โดยจะมี ช่วงชีวิตดังนี้

งานนำเสนอที่คล้ายกัน


Ads by Google