package udpchatserver; /** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; import java.net.*; import java.util.*; import java.io.*; public class ClientRecieve extends Thread{ JTextArea Display = new JTextArea(); static final int BUFFER_SIZE = 512; //Sets Max Packet Size private DatagramSocket ChatSocket = null; //Socket to Communicate with int PacketType; String Resv; int ULength; String Username; int MLength; String Message; String FinalMessage; ClientInterfaceFrame GUI; public ClientRecieve(JTextArea TextMessageDisplay, DatagramSocket Socket, ClientInterfaceFrame PassedGUI) { //sets the refrences to the textarea, Socket, and Entire UI Frame //so they can be manipulated from within this class ChatSocket = Socket; Display = TextMessageDisplay; GUI = PassedGUI; } public void run(){ try{ do { //Block until a Datagram appears byte [] buf = new byte [BUFFER_SIZE]; DatagramPacket IncomingPacket = new DatagramPacket(buf, buf.length); System.out.println("we're recieving"); ChatSocket.receive(IncomingPacket); System.out.println("we've recieved"); //Fill the recieved buffer with the data from incoming packet buf = IncomingPacket.getData(); ProccesPacket(buf); MakeMessage(); Display.append(FinalMessage); }while(true); } catch (IOException e) { System.err.println("Communication error"); e.printStackTrace (); } } public void ProccesPacket(byte[] RecievedBuffer){ //Parse incoming packet for the info contained within System.out.println("Parsing new Packet from server: "); String PacketString = new String(RecievedBuffer); System.out.println("Packet: " +PacketString + "\n"); PacketType = RecievedBuffer[0]; System.out.println("PacketType: " + PacketType); String Resv = new String(RecievedBuffer,1,2); int ULength = RecievedBuffer[3]; System.out.println("ULength: " + ULength); Username = new String(RecievedBuffer,4,ULength); System.out.println("Username: " + Username); int MLength = RecievedBuffer[4+ULength]; System.out.println("MLength: " + MLength); Message = new String(RecievedBuffer,5+ULength,MLength); System.out.println("Message: " + Message); DisableInput(); } public void MakeMessage(){ //method to create the message which is going to be diplayed on the UI FinalMessage = new String(Username+"> "+Message + "\n"); } public void DisableInput(){ //this is the method which makes sure that the server send a join confirmation packet //back to the client before the user interface state gets updated to allow them //to enter messages //If a confirmation is not recieved the only option the user has is to try to rejoin //after correcting their problem (username already taken) or if the server never recieved //their original join packet if(PacketType == 1){ GUI.JoinButton.setEnabled(false); GUI.JoinButton.setToolTipText("You Must Leave Chat Before Rejoining"); GUI.SendButton.setToolTipText("Press to Send Message"); GUI.MessageInput.setToolTipText("Enter Message And Press Enter"); GUI.LeaveButton.setToolTipText("Press to Leave Chat"); GUI.SendButton.setEnabled(true); GUI.LeaveButton.setEnabled(true); GUI.MachinePortInput.setEnabled(false); GUI.UsernameInput.setEnabled(false); GUI.MessageToInput.setEnabled(true); GUI.MessageInput.setEnabled(true); } } }