TCP協議下java通信
阿新 • • 發佈:2017-11-27
trac array 線程 pre out xtend arr nbsp scan
初步,還要改進
服務器
package six.four.five.server; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; importjava.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; import java.util.ArrayList; import java.util.List; //:這是一個服務器 public class Sever { public static void main(String[] args) { //:接受來自客戶端的輸出流,並讀取其中的信息 List<BufferedWriter> list = new ArrayList<BufferedWriter>();try { //實現服務器套接字。服務器套接字等待請求通過網絡傳入。它基於該請求執行某些操作,然後可能向請求者返回結果。 ServerSocket server = new ServerSocket(8888); System.out.println("服務器已啟動"); while(true) { //此類實現客戶端套接字(也可以就叫“套接字”)。套接字是兩臺機器間通信的端點。 Socket socket = server.accept(); System.out.println("Client has been linked!"); //返回此套接字的輸入流,也就是客戶端的輸入流,交給線程並讀取其中的數據到 //InputStream is = socket.getInputStream(); //將字節流轉換為字符流 InputStreamReader ir = new InputStreamReader(socket.getInputStream()); //創建緩沖流 BufferedReader br = new BufferedReader(ir); //返回此套接字的輸出流,同理是客戶端的輸出流,交給集合list,要發給每個客戶端 OutputStream os = socket.getOutputStream(); //將字符流轉換為字節流 OutputStreamWriter ow = new OutputStreamWriter(os); //創建緩沖流 BufferedWriter bw = new BufferedWriter(ow); //添加輸出流 list.add(bw); //將輸入流和集合傳到線程中,並執行線程 new MyThread(br, list).start(); } } catch (IOException e) { e.printStackTrace(); } } }
服務器的線程
package six.four.five.server; //:不停的接受客戶端發來的消息 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.List; public class MyThread extends Thread{ //接受輸出流的集合 private List<BufferedWriter> ow; //緩沖流 private BufferedReader br; //線程的構造方法 public MyThread(BufferedReader br, List<BufferedWriter> ow) { this.ow = ow; //this.is = new InputStreamReader(is); this.br = br; } @Override public void run() { String msg = null; while(true) { try { while(null != (msg = br.readLine())) { //輸出字符串 System.out.println(msg); } for(BufferedWriter lo : ow) { //將接受到客戶端的信息寫到流中,以便發給每個客戶端 lo.write(msg); lo.flush(); } } catch (IOException e) { e.printStackTrace(); } } } }
客戶端
package six.four.five.clients; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Clients { public static void main(String[] args) { try { Socket socket = new Socket(InetAddress.getByName("192.168.0.124"), 8888); System.out.println("You has benn linked Sever!"); // InputStream is = socket.getInputStream(); InputStreamReader ir = new InputStreamReader(is); BufferedReader br = new BufferedReader(ir); // OutputStream os = socket.getOutputStream(); OutputStreamWriter ow = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(ow); // Scanner input = new Scanner(System.in); System.out.println("What your name?"); String name = input.nextLine(); System.out.print("Now,you can say anything:"); String msg = ""; new MyThread(br).start(); while(!(msg.equals("bye"))) { //b = msg.getBytes(); msg = name + ": " + input.nextLine(); bw.write(msg); bw.flush(); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
客戶端的線程
package six.four.five.clients; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class MyThread extends Thread{ private BufferedReader br; public MyThread(BufferedReader br) { this.br = br; } @Override public void run() { String msg = null; while(true) { try { while(null != (msg = br.readLine())) { System.out.println(msg); } } catch (IOException e) { e.printStackTrace(); } } } }
TCP協議下java通信