I am trying to broadcast a message to multiple clients but could not do so. I tried saving the socket connections in an array list and using a for loop, i tried to broadcast the message but only the client who posted the message is receiving the reply from server.
Client Code:
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());
}
}
}
}
Server Code:
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*/}
}
}
}
Can anyone please help me out to rectify the mistake. Thanks in advance :)
After creating Client Socket with accept command in server, start a new Thread by passing this socket is parameter. In the new thread, open InputStream and process it.
Go through the list of all client sockets and send the message by opening OutputStream on respective socket
Have look at this example Socket Programming. You can look at java docs for alternative approach Broadcasting