1 Replies - 386 Views - Last Post: 22 January 2015 - 11:25 AM Rate Topic: -----

#1 ozher0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-December 14

How to send a message to a specific client?

Posted 22 January 2015 - 11:24 AM

I want to send a message to a specific client in a server.
This is my code and what I tried(I have only given you 3 classes in which I believe I have the problem).


TextClient:


import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class TextClient {
	public TextClient() {
	}
         
	public static void main(String[] args) throws IOException,
			UnknownHostException, ConnectException,InterruptedException {

		Socket _socket = new Socket("localhost", 9000);
		System.out.println("Please enter your desired username:");
		Scanner _new = new Scanner(System.in);
		String _inputname = _new.nextLine();
		System.out.println("Success.You can now type your messages!");
		ClientPeer _clientpeer = new ClientPeer(_inputname, _socket);
		Scanner _scan = new Scanner(System.in);
		String _input = _scan.nextLine();
                Server.listofusers.add(_clientpeer);
               _clientpeer.start();
		while (true) {
			
			
			if (!_input.equals("exit")) {
				if (_input.startsWith("/w"))
                                {
                                    String [] cmd=_input.split(" ");
					_clientpeer.sendMessage(_input.substring(_input.indexOf("/w")),cmd[1]);
                                        
                                }
                                        else
                                {
                                    _clientpeer.sendMessage(_input);
                                    
                                    
                                }

			} else
				break;

			_input = _scan.nextLine();
		}
	}
}

                 




ServerPeer class:

import java.net.*;
import java.io.*;

public class ServerPeer extends Thread{
	Socket _socket;
        String username;
	public ServerPeer(Socket _socket) {
		this._socket = _socket;
	}
        public  void sendMessage(String _username,String _message) throws IOException {
		ObjectOutputStream _obj = new ObjectOutputStream(
				_socket.getOutputStream());
		_obj.writeObject(new Message(_username, _message));
		_obj.flush();

	}

	public synchronized void run()  {
	try{	ObjectInputStream _ois = new ObjectInputStream(_socket.getInputStream());
		Message _message;
		while (_socket.isConnected()) {
                   
                   _message = (Message) _ois.readObject();
                        String divide=_message.getAll().substring(0,_message.getAll().indexOf(":"));
	        for(ServerPeer sp:Server.listofpeers)
                {  
                    
                 if(_message instanceof PrivateMessage)
                 {
                     PrivateMessage privm=(PrivateMessage)_message;
                     for(ServerPeer sp2:Server.listofpeers)
                        for(ClientPeer cp:Server.listofusers){
                            if(cp._username.equals(privm.getReceiver()))
                            {
                                sp.sendMessage(divide,privm.getAll());
                                System.out.println(privm.getAll());
                                break;
                            }
                            
                        }
                         
                 }
                     else
                     
                     
                
                 {  sp.sendMessage(divide,_message.getAll());
                      
                        System.out.println( _message.getAll());
                 }
                    
                
                }
                     
			_ois = new ObjectInputStream(_socket.getInputStream());
                   

		}
               
                
	}catch(IOException e){e.printStackTrace();}
        catch(ClassNotFoundException e){e.printStackTrace();}
        }
}




Server



import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	static ServerConfig _svconfig = new ServerConfig();
        public static ArrayList<ServerPeer> listofpeers=new ArrayList<ServerPeer>();
        
        public static ArrayList<ClientPeer> listofusers=new ArrayList<ClientPeer>();
 
	// final static int _mysocket;

	public static void main(String[] args) {
		try {
			final int _mysocket = _svconfig.getPORTNumber();
                         System.out.println("Wainting for clients.....");
			ServerSocket _serversocket = new ServerSocket(_mysocket);
                      while(listofpeers.size()<=_svconfig.getCLIENTSNumber()) 
                      {
                        Socket _clientsocket = _serversocket.accept();
			ServerPeer _serverpeer = new ServerPeer(_clientsocket);
			_serverpeer.start();
                        listofpeers.add(_serverpeer);
                       
                      }
			_serversocket.close();
		}
		

		catch (MissingKeyException e) {
			e.printStackTrace();
		} catch (UnknownKeyException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (BindException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			System.out.println("You have been disconnected");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: How to send a message to a specific client?

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6633
  • View blog
  • Posts: 30,894
  • Joined: 10-May 07

Re: How to send a message to a specific client?

Posted 22 January 2015 - 11:25 AM

** Topic closed as duplicate **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1