Hi there all, I really need help on this one, I went away for a wedding for a week and missed a week of work and now I have a practical due for 1:00 10 tomorrow and I got back today, but I managed to complete all of the programming but only to discover that it must now be done in UDP and mine is not, so could anyone help me out in converting this code into working UDP form? It will be very much appreciated

Kind Regards
Curtis

Chat server:

package pkg201003757_prac05;

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

//class is used to update the list of user name

// class is used to maintain the list of all online users
public class ChatServer
{
	ServerSocket serversocket;
	Socket clientSocket;
	ArrayList al=new ArrayList();
	ArrayList al1=new ArrayList();
	ArrayList al2=new ArrayList();
	ArrayList alname=new ArrayList();
	Socket socket1,socket2;
	ChatServer()throws IOException
        {
		serversocket = new ServerSocket(1004);	// create server socket
                System.out.println("***Chat Server Started***");
                Login login = new Login();
		while(true)
                {
			clientSocket = serversocket.accept();	//accept the client socket
			socket1=serversocket.accept();
			socket2=serversocket.accept();
			al.add(clientSocket);	// add the client socket in arraylist
			al1.add(socket1);
			al2.add(socket2);
			System.out.println("***Client is Connected***");  
                        
                        
			UpdateUserThread m = new UpdateUserThread(socket2,al2,alname); //new thread for maintaning the list of user name
			Thread t2 = new Thread(m);
			t2.start();

			SendAndRecieveThread r = new SendAndRecieveThread(clientSocket,al);//new thread for receive and sending the messages
			Thread t = new Thread(r);
			t.start();
			
			UpdateThread my=new UpdateThread(socket1,al1,clientSocket,socket2); // new thread for update the list of user name
			Thread t1=new Thread(my);
			t1.start();
		}
	}
	public static void main(String[] args)
        {
		try
                {
                    ChatServer myServer = new ChatServer();
		}
                catch (IOException ex)
                {}
	}
}
class UpdateThread implements Runnable
{
	Socket s1,s,s2;
	static ArrayList al1;
	DataInputStream dataInputStream;
	String sname;
	UpdateThread(Socket s1,ArrayList al1,Socket s,Socket s2)
        {
		this.s1=s1;
		this.al1=al1;
		this.s=s;
		this.s2=s2;
	}
    @Override
	public void run()
        {	
		try
                {
                    dataInputStream = new DataInputStream(s1.getInputStream());
                    while(true)
                    {
                        sname = dataInputStream.readUTF();
                        System.out.println("***" + sname + " logged off the chat server***");
                        UpdateUserThread.alname.remove(sname);//remove the logout user name from arraylist
                        UpdateUserThread.ListUpdate();
                        al1.remove(s1);
                        SendAndRecieveThread.al.remove(s);
                        UpdateUserThread.al2.remove(s2);
                        if(al1.isEmpty())
			System.exit(0); //all client has been logout
                    }
		}
                catch(Exception ie)
                {}
	}
}

// class is used to maintain the list of all online users
//class is used to receive the message and send it to all clients
class UpdateUserThread implements Runnable
{
	Socket s2;
	static ArrayList al2;
	static ArrayList alname;
	static DataInputStream din1;	
	static DataOutputStream dout1;

	UpdateUserThread(Socket s2,ArrayList al2,ArrayList alname)
        {
		this.s2=s2;
		this.al2=al2;
		this.alname=alname;
	}
    @Override
	public void run()
        {
        
		try
                {
                    din1= new DataInputStream(s2.getInputStream());
                    alname.add(din1.readUTF());	// store the user name in arraylist
                    ListUpdate();
		}
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
	}
	// send the list of user name to all client
	static void ListUpdate()throws Exception
        {
		Iterator i1 = al2.iterator();
		Socket st1;		

		while(i1.hasNext())
                {
			st1=(Socket)i1.next();
			dout1=new DataOutputStream(st1.getOutputStream());
			ObjectOutputStream obj=new ObjectOutputStream(dout1);
			obj.writeObject(alname); //write the list of users in stream of all clients
			dout1.flush();
			obj.flush();
		}
	}
}
//class is used to receive the message and send it to all clients
class SendAndRecieveThread implements Runnable
{
	Socket s;
	static ArrayList al;
	DataInputStream dataInput;
	DataOutputStream dataOutput;

	SendAndRecieveThread(Socket s, ArrayList al)
        {
		this.s = s;
		this.al = al;
	}
    @Override
	public void run()
        {
		String msg;
		int i = 1;
		try
                {
                    dataInput = new DataInputStream(s.getInputStream());
		}
                catch(Exception ex)
                {}
		
		while(i == 1)
                {
			try
                        {
					
                            msg = dataInput.readUTF(); //read the message
                            SendMsgToAllClients(msg);
			}
                        catch (IOException ep)
                            {}
		}
	}
	// send it to all clients
	public void SendMsgToAllClients(String msg)throws IOException
        {
		Iterator i = al.iterator();
		Socket allMsgSocket;
                
		while(i.hasNext())
                {
			allMsgSocket = (Socket)i.next();
			dataOutput = new DataOutputStream(allMsgSocket.getOutputStream());
			dataOutput.writeUTF(msg);
			dataOutput.flush();
		}
	}
}

Client code:

package pkg201003757_prac05;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;

//create the GUI of the client side
public class MyClient extends WindowAdapter implements ActionListener
{
	JFrame frame;
	JList list;
	JList list1;
	JTextField tf;
	DefaultListModel model;
	DefaultListModel model1;
	JButton btnSend;
	JButton btnLogout;
	JScrollPane scrollpane;
	JScrollPane scrollpane1;
	JLabel label;
	Socket s,s1,s2;
	DataInputStream din;
	DataOutputStream dout;
	DataOutputStream dlout;
	DataOutputStream dout1;
	DataInputStream din1;
	String name;
	
	MyClient(String name)throws IOException
        {
		frame = new JFrame("Client Side");
		tf=new JTextField();
		model=new DefaultListModel();
		model1=new DefaultListModel();
		label=new JLabel("Message");
		list=new JList(model);
		list1=new JList(model1);
		btnSend=new JButton("Send");
		btnLogout=new JButton("Logout");
		scrollpane=new JScrollPane(list);
		scrollpane1=new JScrollPane(list1);
		JPanel panel = new JPanel();
		btnSend.addActionListener(this);
		btnLogout.addActionListener(this);
		panel.add(tf);panel.add(btnSend);panel.add(scrollpane);
		panel.add(label);panel.add(btnLogout);
		panel.add(scrollpane1);
		scrollpane.setBounds(10,20,180,150);
		scrollpane1.setBounds(250,20,100,150);
		label.setBounds(20,180,80,30);
		tf.setBounds(100,180,140,30);
		btnSend.setBounds(260,180,90,30);
		btnLogout.setBounds(260,230,90,30);
		frame.add(panel);
		panel.setLayout(null);
		frame.setSize(400, 400);
	   	frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.name=name;
		frame.addWindowListener(this);
		s=new Socket("localhost",1004);	//creates a socket object
		s1=new Socket("localhost",1004);
		s2=new Socket("localhost",1004);
	    //create inputstream for a particular socket
		din=new DataInputStream(s.getInputStream());
		//create outputstream
		dout=new DataOutputStream(s.getOutputStream());
		//sending a message for login
		dout.writeUTF("***" + name+" has Logged in to chat***");	
		dlout=new DataOutputStream(s1.getOutputStream());
		dout1=new DataOutputStream(s2.getOutputStream());
		din1=new DataInputStream(s2.getInputStream());

// creating a thread for maintaning the list of user name
		usersList m1=new usersList(dout1,model1,name,din1);
		Thread t1=new Thread(m1);
		t1.start();			
	//creating a thread for receiving a messages
		My m=new My(din,model);
		Thread t=new Thread(m);
		t.start();
  	}
    @Override
	public void actionPerformed(ActionEvent event)
        {
		// sending the messages
		if(event.getSource() == btnSend)
                {	
			String str = "";
			str = tf.getText();
			tf.setText("");
			str = name + ": > "+str;
			try
                        {
                            dout.writeUTF(str);
                            System.out.println(str);
                            dout.flush();
			}
                                
                        catch(IOException ex)
                        {
                            System.out.println(ex);
                        }
                        if((str.contains("Bye")) || (str.equals("Bye")))
                        {
                            frame.dispose();
                            try
                            {
				 //sending the message for logout
                                dout.writeUTF(name+" has logged out...");
                                dlout.writeUTF(name);
                                dlout.flush();
                                //Thread.currentThread().sleep(1000);
                                Thread.sleep(1000);
                                System.exit(1);
                            }
                            catch(Exception oe) 
                            {}
                        }
		}
		// client logout
		if (event.getSource() == btnLogout)
                {
			frame.dispose();
			try
                        {
				 //sending the message for logout
                            dout.writeUTF(name + " has Logged out");
                            dlout.writeUTF(name);
                            dlout.flush();
                            //Thread.currentThread().sleep(1000);
                            Thread.sleep(1000);
                            System.exit(1);
			}
                        catch(Exception oe) 
                        {}
		}
	}
    @Override
	public void windowClosing(WindowEvent w)
        {
		try
                {
			dlout.writeUTF(name);
			dlout.flush();	
			//Thread.currentThread().sleep(1000);
                        Thread.sleep(1000);
			System.exit(1);
		}
                catch(Exception oe)
                {}
	}
}

// class is used to maintaning the list of user name
//class is used to received the messages
//class is used to received the messages
class usersList implements Runnable
{
	DataOutputStream dout1;
	DefaultListModel model1;	
	DataInputStream din1;
	String name,lname;
	ArrayList alname = new ArrayList(); //stores the list of user names
	ObjectInputStream obj; // read the list of user names
	int i=0;
	usersList(DataOutputStream dout1,DefaultListModel model1,String name,DataInputStream din1)
        {
		this.dout1=dout1;
		this.model1=model1;
		this.name=name;
		this.din1=din1;
	}
    @Override
	public void run()
        {
		try
                {
                    dout1.writeUTF(name);  // write the user name in output stream
		while(true)
                {
			obj=new ObjectInputStream(din1);
			//read the list of user names
			alname=(ArrayList)obj.readObject(); 
			if(i>0)
			model1.clear(); 
			Iterator i1=alname.iterator();
			System.out.println(alname);
			while(i1.hasNext())
                        {
				lname=(String)i1.next();
				i++;
				 //add the user names in list box
				model1.addElement(lname);
				}
			}
		}catch(Exception oe)
                {}
	}
}
//class is used to received the messages
class My implements Runnable
{
	DataInputStream din;
	DefaultListModel model;
	My(DataInputStream din, DefaultListModel model)
        {
		this.din=din;
		this.model=model;
	}
    @Override
	public void run()
        {
		String str1="";
		while(true)
                {
			try
                        {
				str1=din.readUTF(); // receive the message
				// add the message in list box
				model.addElement(str1);
			}
                        catch(Exception e)
                        {}
		}
	}
}

I will be here if anyone needs anything, thanks again

jwenting commented: lazy homework kiddo -3

Recommended Answers

All 2 Replies

Is this thread the same as the other one?
One should be enough.

You'll probably get faster results if you hire a programmer. This forum is mostly for helping students learn java programming.

@Curt1337: No, no one here will convert the chat server that you copied directly from Rose India to UDP so you can turn it in as a work product.

As Norm said, hire a programmer if you want someone to do your work for you.

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.