同步型 TCP / multi client:
這樣的同步TCP應用的地方比較特殊,解決需要排隊才能交換資訊的方法。
若兩連接同時詢問,必須要等到伺服器一個個回應才可以。
Server 端:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author MacTaylor. */ public class Server { public static void main(String argv[]) throws Exception { ServerSocket welcomeSocket = new ServerSocket(1234); Responder h = new Responder(); while (true) { // waiting.. Socket connectionSocket = welcomeSocket.accept(); Thread t = new Thread(new MyServer(h, connectionSocket)); t.start(); } } } class MyServer implements Runnable { Responder h; Socket connectionSocket; public MyServer(Responder h, Socket connectionSocket) { this.h = h; this.connectionSocket = connectionSocket; } @Override public void run() { while (h.responderMethod(connectionSocket)) { try { Thread.sleep(5000); } catch (InterruptedException ex) { ex.printStackTrace(); } } try { connectionSocket.close(); } catch (IOException ex) { Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex); } } } class Responder { String serverSentence; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); synchronized public boolean responderMethod(Socket connectionSocket) { try { BufferedReader inFromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient= new DataOutputStream(connectionSocket.getOutputStream()); String clientSentence = inFromClient.readLine(); if (clientSentence == null || clientSentence.equals("EXIT")) { return false; } if (clientSentence != null) { System.out.println("client : " + clientSentence); } serverSentence = br.readLine() + "\n"; outToClient.writeBytes(serverSentence); return true; } catch (SocketException e) { System.out.println("Disconnected"); return false; } } }
非同步型 TCP / multi client:
這樣的TCP適合作為典型的應用,也適合拿來做遊戲伺服器交換座標資訊。
Server 端:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author MacTaylor. */ public class Server { public static void main(String argv[]) throws Exception { ServerSocket welcomeSocket = new ServerSocket(1234); Responder h = new Responder(); while (true) { // waiting.. Socket connectionSocket = welcomeSocket.accept(); Thread t = new Thread(new MyServer(h, connectionSocket)); t.start(); } } } class MyServer implements Runnable { Responder h; Socket connectionSocket; public MyServer(Responder h, Socket connectionSocket) { this.h = h; this.connectionSocket = connectionSocket; } @Override public void run() { while (h.responderMethod(connectionSocket)) { try { Thread.sleep(5000); } catch (InterruptedException ex) { ex.printStackTrace(); } } try { connectionSocket.close(); } catch (IOException ex) { Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE, null, ex); } } } class Responder { String serverSentence; public boolean responderMethod(Socket connectionSocket) { try { BufferedReader inFromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient= new DataOutputStream(connectionSocket.getOutputStream()); String clientSentence = inFromClient.readLine(); if (clientSentence == null || clientSentence.equals("EXIT")) { return false; } if (clientSentence != null) { System.out.println("client : " + clientSentence); } serverSentence = "OK! i see "+ clientSentence +" \n"; outToClient.writeBytes(serverSentence); return true; } catch (SocketException e) { System.out.println("Disconnected"); return false; } } }
至於 Client 實作是沒有分別的,但這只是一個測試用的連接,也可以同時使用其他語言的TCP做連接!
Client 端:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.Socket; /** * * @author MacTaylor. */ public class Client { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost", 6789); while (true) { DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); if (sentence.equals("EXIT")) { break; } modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); } clientSocket.close(); } }
resource:
https://javadeveloperszone.wordpress.com/2013/04/20/java-tcp-chat-multiple-client/
沒有留言:
張貼留言