Hi, trying to make a chat system with multiple servers.

Server needs to send message from a client to all the other clients.

But need to save the client ports that are connected to the server in order to do this.

Not really getting anywhere with this, i'll post ode for both client and server below any help would be great! Thank you

SERVER:

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

public class UDPServerSkeleton {
  // java UDPServer port
  public static void main(String args[ ]) throws Exception
  {
      
      
  
   int serverport = 7777;
   int count=0;
   int [] clientport=new int[100];
    
    

    // Open a new datagram socket on the specified port
    DatagramSocket udpServerSocket = new DatagramSocket(serverport);

    
   while(true)
	  
	  
	  {
	  
	            // create byte buffers to hold the messages to send and recieve, which are at least as long as the number of bytes in the message
          // 1024 bytes is pretty long, we could count the bytes in the message first, then create this buffer, but we'll keep it like this for the time being
          byte[] receiveData = new byte[1024];
    
      
          // create an empty DatagramPacket packet
          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      
          // block until there is a packet to recieve, then recieve it  (into our empty packet)
          udpServerSocket.receive(receivePacket);
     
      
          // extract the message from the packet and make it into a string, then trim off any end characters
          String clientMessage = (new String(receivePacket.getData())).trim();
	  
	  String t = "Client Connected";
  if(clientMessage.compareTo(t) > 0)
   {
     
        
               

        for(int i=0; i<clientport.length;i++)
        {
         clientport[i]=receivePacket.getPort();
         }
       System.out.println(""+clientport);
   }
         

   
   else{
   

        
      
         System.out.println("Client message: "+clientMessage);
          
      
          // get the IP address of the recieved packet 
          
          InetAddress clientaddress=receivePacket.getAddress();
        
          
          
      
          

          // get the port number which the recieved connection came from (i.e. the port number the client automatically assigned to it's end when it created the UDP connection to the port on the server)
  		    //int clientport=receivePacket.getPort();
          
          // compose a response message
          String letter = "Message Sent";
	  byte[] data = letter.getBytes();
  
          // create an empty buffer/array of bytes to send back 
        
                    
          // assign the message to the send buffer
         
	  
	  	  int packetSize = data.length;
	  	 

          
          for(int i=0;i<=clientport.length ; i++)
          {
          
        DatagramPacket sendPacket = new DatagramPacket(data, packetSize, clientaddress, clientport[i]);
         udpServerSocket.send(sendPacket);
         
          }
      }
      }
        
    }
    
}

CLIENT

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

public class UDPClientSkeleton {



 // get the port number to use from the command line
     
   public static void main(String args[ ]) throws Exception {
 int clientport = 7777;
  
   
    
     
      
 
          
      // create a DatagramSocket
     
      
       DatagramSocket udpClientSocket = new DatagramSocket();
            // get the IP address of the local machine - we will use this as the address to send the data to (i.e. the server program running locally)
      InetAddress serverIPAddress = InetAddress.getByName("localhost");
       
       //connect to server
        
        
        
        byte[] connectData = new byte[1024]; 
       
       String connect="Client Connected";
             
       connectData = connect.getBytes();
        
        int connectpacketSize = connectData.length;
       
       DatagramPacket connectPacket = new DatagramPacket(connectData, connectpacketSize, serverIPAddress, clientport);

        udpClientSocket.send(connectPacket);
       
       
       while(true)
     {
    


   
      // create byte buffer to hold the message to send, which is at lease as long as the number of bytes in the message
      // 1024 bytes is pretty long, we could count the bytes in the message first, then creat this buffer, but we'll keep it like this for the time being
      byte[] sendData = new byte[1024]; 

      // form a message to send
      
      
       BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

String letter = null;

System.out.println ("Enter message: ");
try
{
letter= in.readLine ();
}
catch (IOException ex)
{
System.out.println("there was a problem an the keyboard could not be read.");
}
      
      
         // put this message into our emty buffer/array of bytes
     
   
     
      sendData = letter.getBytes();

      
    
   
     
	  //InetAddress address = InetAddress.getByName("localhost");
	  int packetSize = sendData.length;
	  

      
      
      
      
      // create a DatagramPacket with the data, IP address and port number
      DatagramPacket sendPacket = new DatagramPacket(sendData, packetSize, serverIPAddress, clientport);
    
    
      // send the UDP packet
      
     
	  udpClientSocket.send(sendPacket);


      // Create a byte buffer/arrag
      byte[] receiveData = new byte[1024];
      
      // set up a DatagramPacket to recieve the data into - call it receivePacket
      // Hint: see the server class
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      
      
      // recieve a packet from the server (blocks until the packets are recieved)
      // Hint: see the server class
      udpClientSocket.receive(receivePacket);
      

      // extract the reply from the DatagramPacket
       String serverReply = (new String(receivePacket.getData())).trim();

      
     
      
      
      // print to the screen
      
      System.out.println(""+serverReply);
      
      
     
   }
   

    
  //udpClientSocket.close();
  
   }
    
   
}

Recommended Answers

All 2 Replies

on the server end simply store client ports in the array.

on client side use a constructor to make datagram socket. this will keep the ports of each clients fixed ... if you don't do this each time the client port will change

use a thread to receive messages on the client side

Could you explain this in more detail, dont hava a lot of experience, or sample code? even a small bit to get me started would be great! thanks for the reply!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.