การออกอากาศไปยังไคลเอนต์หลายเครื่องโดยใช้การเชื่อมต่อ TCP ใน Java

ฉันกำลังพยายามเผยแพร่ข้อความไปยังไคลเอนต์หลายรายแต่ไม่สามารถทำได้ ฉันพยายามบันทึกการเชื่อมต่อซ็อกเก็ตในรายการอาร์เรย์และใช้ for loop ฉันพยายามออกอากาศข้อความ แต่เฉพาะไคลเอนต์ที่โพสต์ข้อความเท่านั้นที่ได้รับการตอบกลับจากเซิร์ฟเวอร์

รหัสลูกค้า:

import java.net.*;
import java.util.Scanner;

import org.json.simple.JSONObject;

import java.io.*;

public class TCPClient {
public static void main (String args[]) {
  // arguments supply message and hostname
  JSONObject clientObj=new JSONObject();
Socket s = null;
try{
    int serverPort = 7899;
    Scanner scan=new Scanner(System.in);
    s = new Socket("127.0.0.1", serverPort);  
    System.out.println("Connection Established");
    DataInputStream in = new DataInputStream( s.getInputStream());
    DataOutputStream out =new DataOutputStream( s.getOutputStream());
    while(true)
    {
   System.out.print(">");
   String inputdata=scan.nextLine();
   clientObj.put("ID",inputdata );
   System.out.println("Sending data"); 
     out.writeUTF(clientObj.toString());     // UTF is a string encoding see Sn. 4.4
   String data = in.readUTF();   // read a line of data from the stream
   System.out.println(data) ;       //writing received data
   }
    }catch (UnknownHostException e) {
 System.out.println("Socket:"+e.getMessage());
 }catch (EOFException e){
 System.out.println("EOF:"+e.getMessage());
 }catch (IOException e){
 System.out.println("readline:"+e.getMessage());
 }finally {
 if(s!=null) try {
   s.close();
 }catch (IOException e){
   System.out.println("close:"+e.getMessage());
 }
  }
  }
}

รหัสเซิร์ฟเวอร์:

import java.net.*;
import java.util.ArrayList;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;

public class TCPServer {
static ArrayList<String> client=new ArrayList<String>();
static ArrayList<Socket> clientSock=new ArrayList<Socket>();
static int i = 0;
public static void main (String args[]) {
try{
  int serverPort = 7899; // the server port
  ServerSocket listenSocket = new ServerSocket(serverPort);
  while(true) {
    System.out.println("Server listening for a connection");
    Socket clientSocket = listenSocket.accept();
    i++;
    System.out.println("Received connection " + i );
    Connection c = new Connection(clientSocket);
    client.add("guest"+i);
    clientSock.add(clientSocket);
   }
 } 
 catch(IOException e) 
 {
   System.out.println("Listen socket:"+e.getMessage());
 }
 }
 public void display(String BroadMsg1)
 {
 for(int j=0;j<client.size();j++)
 {
    System.out.println(clientSock.get(j));
 }
 }
 public void broadcast(String BroadMsg)
 {
  String clientName=null;
  Socket cSock=null;
  //DataInputStream inBroad;
  DataOutputStream outBroad=null;
 for(int j=0;j<client.size();j++)
 {
    clientName=client.get(j);
    cSock=clientSock.get(j);
    try{
    outBroad=new DataOutputStream(cSock.getOutputStream());
    outBroad.writeUTF(clientName+">"+BroadMsg);
    }catch(Exception ex)
    {
        /*client.remove(j);
        clientSock.remove(j);*/
        System.out.println(ex.getMessage());
    }
    }
  }
  }

    class Connection extends Thread {
    TCPServer tcpser=new TCPServer();
  DataInputStream in;
  DataOutputStream out;
  Socket clientSocket;

  public Connection (Socket aClientSocket) {
  try {
   clientSocket = aClientSocket;
   in = new DataInputStream( clientSocket.getInputStream());
   out =new DataOutputStream( clientSocket.getOutputStream());
   this.start();
  } catch(IOException e) {
   System.out.println("Connection:"+e.getMessage());
  }
 }

 public void run(){

   JSONObject serObj=new JSONObject();
    JSONParser jParser=new JSONParser();
    try {           // an echo server
        while(true)
        {
 System.out.println("server reading data");
 String data = in.readUTF();        // read a line of data from the stream
  try {
    serObj=(JSONObject)jParser.parse(data);         //parsing JSONObject
   } catch (ParseException e) {
    e.printStackTrace();
    }
   System.out.println("server writing data");
  // tcpser.display(serObj.get("ID").toString());
  tcpser.broadcast(serObj.get("ID").toString());
 }}
 catch (EOFException e){
  System.out.println("EOF:"+e.getMessage());
 } catch(IOException e) {
  System.out.println("readline:"+e.getMessage());
 } finally{ 
 try {
   clientSocket.close();
 }catch (IOException e){/*close failed*/}
}
}
}

ใครก็ได้โปรดช่วยฉันแก้ไขข้อผิดพลาด ขอบคุณล่วงหน้า :)


person Th3Dark0    schedule 30.08.2015    source แหล่งที่มา


คำตอบ (1)


หลังจากสร้าง Client Socket ด้วยคำสั่ง Accept ในเซิร์ฟเวอร์แล้ว ให้เริ่ม Thread ใหม่โดยส่งพารามิเตอร์ socket is นี้ ในเธรดใหม่ ให้เปิด InputStream และดำเนินการ

ดูรายการซ็อกเก็ตไคลเอ็นต์ทั้งหมดแล้วส่งข้อความโดยเปิด OutputStream บนซ็อกเก็ตที่เกี่ยวข้อง

ดูตัวอย่างนี้ Socket Programming คุณสามารถดูเอกสาร Java สำหรับแนวทางอื่นการออกอากาศ

1) สร้างเธรดไคลเอ็นต์หลังจากเพิ่มลงในรายการ

2) พิมพ์การติดตามสแต็กข้อยกเว้นในบล็อก catch เพื่อทราบข้อยกเว้น

3) แทนที่ cSock=clientSock.get(j); ด้วย Iterator เพื่อผ่านซ็อกเก็ตทั้งหมด

person Ravindra babu    schedule 30.08.2015