Vous êtes sur la page 1sur 4

DR. A. Q.

KHAN INSTITUTE OF COMPUTER SCIENCES & INFORMATION TECHNOLOGY


(Network Programming) BEIT-VII Write a complete chat application based on UDP. You will write UDPServer and UDPClient applications. UDPServer application has the following Skelton public class UDPServer { HashMap<String,Communicator>comMap=new HashMap<String,Communicator>(101); int currentId =100; public void StartServer(int port) { try { DatagramSocket ds = new DatagramSocket(port); byte[] b = new byte[256]; DatagramPacket dp = new DatagramPacket(b,b.length); boolean flag = true; while(flag) { try { ds.receive(dp); Process(dp); dp.setLength(256); } catch (IOException ex) { } } } catch (SocketException ex) { } } public void Process(DatagramPacket dp){ String str = new String(dp.getData(),0,dp.getLength()); //if str starts with<Register> then format is // <Register><Name> //assign it an id in the following way String cid = Integer.toString(currentId++); Communicator c = new Communicator (/*initialize*/); Set<String> keys = comMap.keySet(); Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { String k = (String) keyIter.next(); Page 1 of 4

Communicator temp = comMap.get(k); temp.SendNewlyRegistered(c.Id,c.name); } comMap.put(c.Id, c); //Send ID to currently registered person using ReceiveYourID(String ID) //Case1 Finished Case2 starts // If starts with <Msg> //then it is in following Format // <Msg>FROM=Id&&TO=Id&&msg //Extract name,id and msg //on the bases of id get Communicator from //call its SendMessage(///) to send message //Case 3 If str starts with <BROADCAST> Server sends message to everyone except the person who forwarded the message Message Format is <BROADCAST>FROM=ID } } public class Communicator { String name; String Id; InetAddress ia; int port; static DatagramSocket ds; static { try { ds = new DatagramSocket(); } catch (SocketException ex) { } } public Communicator(String id, String Name,InetAddress ia, int port )throws SocketException { } public void SendMessage(String from, String msg){ //send message to this communicator in the following format // <Message>From=from&&MSG=msg } public void SendNewlyRegistered(String id,String name){ //send message in the following format //<Register>Name=name&&ID=id } public void ReceiveYourID(String ID){ Page 2 of 4

// // //

//send message in the following format //<YourID>ID=id } } //Client Side Code public class Chatter extends JFrame implements Runnable{ String myName; String myID; InetAddress ia; int port; DatagramSocket ds; public Chatter(String Myname, String ServerAddress, String Port){ //Initialization myName = name; //Create InetAddress //Create DatagramSocket //Create Thread } private void SendButtonActionPerformed(ActionEvent evt) { //Get message from Text field //Send Message to Selected person in List //Append in Text Area // Message Format described in server code } private void BroadcastActionPerformed(ActionEvent evt) { // //Get message from Text field //Broadcast Message //Append in Text Area //Message Format described in server code } public void run(){ //Register Yourself // receive first packet and Initialize MyID //continuously read data from socket, parse it and display appropriately. } private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private java.awt.List list1; }//end class When you launch application, a dialogue box appears which requires you to input following information: Page 3 of 4

Your Name Server address (String) port number (String) This information is passed to Chatter constructor. Do not implement this Dialogue box. You are required to implement Chatter class.

Page 4 of 4

Vous aimerez peut-être aussi