但是同一時間只能有一方傳送資料,且 Server 端只能同時服務一個 Client
這樣的對播較容易實作,如需服務多個 Client 實則需透過執行緒去處理
這個之後我在發文吧,先來看看以下簡易的網路對播程式 在 Server/Client 的作法都類似
透過 flag 變數去控制目前是該等待接收資料 還是 輸入傳送資料
end 變數則是控制目前對話的狀態,true 為中止。以下片斷程式以 Client 為例
while(!end)
{
while(flag == 0){ //傳送
//.....
if(message.equals("@")) //遇到 @ 時,轉換 flag
}
while(flag ==1){ //接收
//.....
if(message.equals("@")) //遇到 @ 時,轉換 flag
}
}
ClientCom.java
import java.io.*; import java.net.*; public class ClientCom { public ClientCom(int port) { try{ int flag = 0; boolean end = false; Socket client = new Socket("xxx.xxx.x.xx", port); //目的端IP位址 System.out.println("---- connect to xxx.xxx.x.xx ----"); DataOutputStream dataout = new DataOutputStream(client.getOutputStream()); DataInputStream datain = new DataInputStream(client.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String message; while(!end){ while(flag == 0 && !end){ //send message = br.readLine(); dataout.writeUTF(message); if(message.equals("#")){ //# 為結束目前對播 end = true; } if(message.equals("@")){ //@ 為更換目前的 flag flag = 1; } } while(flag == 1 && !end){ //receive message = datain.readUTF(); System.out.println("[from server] : " + message); if(message.equals("#")){ end = true; } if(message.equals("@")){ flag = 0; } } } client.close(); }catch(Exception e){ System.out.println(e.getMessage()); System.exit(-1); } } public static void main(String[] args) { int port = 0; if(args.length < 1) { System.out.println("Usage: java ClientCom [port]"); System.exit(1); }else{ port = Integer.parseInt(args[0]); ClientCom client = new ClientCom(port); } } }
ServerCom.java
import java.io.*; import java.net.*; public class ServerCom { public ServerCom(int port) { try{ int flag = 0; boolean end = false; ServerSocket server = new ServerSocket(port); System.out.println("waiting..."); Socket clientSocket = server.accept(); java.util.Date currentDate = new java.util.Date(); /*** 印出 client 的資料 ***/ System.out.println("----- Accept a client -----"); System.out.println("Clinet IP: " + clientSocket.getInetAddress().getHostAddress()); System.out.println("Client Port: " + clientSocket.getPort()); System.out.println("connect date: " + currentDate); System.out.println("----- Accept a client -----"); DataOutputStream dataout = new DataOutputStream(clientSocket.getOutputStream()); DataInputStream datain = new DataInputStream(clientSocket.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String message; while(!end) { while(flag == 0 && !end){ //receive message = datain.readUTF(); System.out.println("[from client] : " + message); if(message.equals("#")){ end = true; } if(message.equals("@")){ System.out.println("change to you"); flag = 1; } } while(flag == 1 && !end){ message = br.readLine(); dataout.writeUTF(message); if(message.equals("#")){ end = true; } if(message.equals("@")){ flag = 0; } } } clientSocket.close(); }catch(IOException e){ System.out.println("Could not listen on port:" + port); System.exit(-1); } } public static void main(String[] args) { int port; if(args.length < 1) { System.out.println("Usage: java Server_7 [port]"); System.exit(1); } try{ port = Integer.parseInt(args[0]); ServerCom s = new ServerCom(port); } catch(NumberFormatException e){ System.out.println("Port is Integer!!"); } } }
沒有留言:
張貼留言