1 Replies - 787 Views - Last Post: 07 September 2013 - 08:01 AM Rate Topic: -----

#1 jesroni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-August 13

How to send message from server to all connected clients

Posted 07 September 2013 - 06:11 AM

hi..i am new to JAVA i am trying to build up chat app..
code pasted below..all connected clients communicated well.,i want to send messages from server also how to send it..share your ideas..
Client:
/*ASSIGNMENT:28*/
import java.io.*;
import java.net.*;
public class MulClientThread implements Runnable
{
//declaration
static int port = 1774;
static String host = "192.168.1.113";
static Socket soc = null;
static BufferedReader input = null;
static DataInputStream is = null;//receive response from server
static boolean end = false;
static PrintStream ps = null;//send the data to server
public static void main(String args[])throws IOException
{
//start of main method
soc = new Socket(host,port);//socket creation for communication
System.out.println("chat started using multi thread on port "+port+"\n on host "+host);
//input and output stream communication with server
input = new BufferedReader(new InputStreamReader(System.in));
ps = new PrintStream(soc.getOutputStream());
is = new DataInputStream(soc.getInputStream());
String receive;
BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
//write the data to socket
if(soc != null&&ps != null&&is != null)
{
try
{
//Create a thread to read from the server
new Thread(new MulClientThread()).start();
while(!end)
{
ps.println(input.readLine());
}
while((receive=in.readLine())!=null)
{
System.out.println("Server:>"+receive);
}
}
catch(IOException ex)
{
System.out.println("IO Exception"+ex);
}
}
}
public void run()
{
//read from the server
String message;
try
{
while((message = is.readLine()) != null)
{
System.out.println(message);
if(message.equals("close"))
{
//message equals close terminate the connection
break;
}
}
if(soc!=null)
{
//close the connection
soc.close();
System.out.println("connection terminated....");
System.exit(0);
}
end=true;
}
catch(IOException x)
{
System.out.println("IO Exception "+x);
}
}//end of run method
}//end of main method
server:
/*ASSIGNMENT:28*/
import java.io.*;
import java.net.*;
class MulServerThread
{
//declaration
private static ServerSocket serverSocket = null;
private static Socket socket = null;
private static final int maxclients = 5;
//array of thread creation
private static final clientThread[] threads = new clientThread[maxclients];//accepts only 5 clients
public static void main(String args[])throws IOException
{
//start of main method
int port = 1774;
serverSocket = new ServerSocket(port);
System.out.println("waiting for client......");
while(true)
{
socket = serverSocket.accept();//accept the
int i = 0;
//pass into new client thread
for(i = 0;i < maxclients;i++)
{
if(threads[i]==null)
{
(threads[i] = new clientThread(socket,threads)).start();//start array of thread
break;
}
}
if(i == maxclients)
{
PrintStream out = new PrintStream(socket.getOutputStream());//send data to the client
out.println("Server busy........please try after some time.");
//close the connection
out.close();
socket.close();
}
}
}
}
class clientThread extends Thread
{
private int maxclients;
private DataInputStream is = null;//receive response from client
private PrintStream out = null;//send the data to client
private Socket socket = null;
private final clientThread[] threads;
public clientThread(Socket socket,clientThread[] threads)
{
this.socket = socket;
this.threads = threads;
maxclients = threads.length;
}
public void run()
{
//start of run method
int maxclients = this.maxclients;
clientThread[] threads = this.threads;
try
{
//create input output stream for client
is=new DataInputStream(socket.getInputStream());
//send data to the client
out = new PrintStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String send;
out.println("Hai...Enter your name.");
String name,message;
name = is.readLine();
out.println("Hello "+name+" to our chat room.\tTo leave enter close");//display the message to available client thread
for(int i = 0;i < maxclients;i++)
{
if(threads[i] != null)
{
threads[i].out.println("A new user "+name+" entered in to the chat room");
}
}
System.out.println("New user "+name+" entered in to the chat room\n");//print in a console
while(true)
{
message = is.readLine();
//message equals to close..terminate the connection
if(message.equals("close"))
{
break;
}
for(int i = 0;i < maxclients;i++)
{
if(threads[i] != null)
{
threads[i].out.println(name + ">" +message);
}
}
System.out.println(name + ">" +message+"\n");//console
for(int i=0;i<maxclients;i++)
{
while((send=br.readLine())!=null)
{
threads[i].out.println(send);
}
}
}
for(int i = 0;i < maxclients;i++)
{
if(threads[i] != null)
{
threads[i].out.println("The user " + name+ " left the chat ");
}
}
System.out.println("The user " + name + " left the chat\n");
out.println("Bye "+name);
System.out.println("Bye "+name+"\n");
for(int i = 0;i < maxclients;i++)
{
if(threads[i] == this)
{
threads[i] = null;//current thread variable null so new client thread accepted by the server
}
}
//close connection
is.close();
out.close();
}
catch(IOException e)
{
System.out.println(e);
}
}//enf of run method
}

Is This A Good Question/Topic? 0
  • +

Replies To: How to send message from server to all connected clients

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: How to send message from server to all connected clients

Posted 07 September 2013 - 08:01 AM

Why you always forget the code tags? We cant read your code without code tags.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1