Question
Create GUI using Netbeans JFrame two windows one for Client class and the other for Server class each window will show a space to write
Create GUI using Netbeans JFrame
two windows one for Client class and the other for Server class
each window will show a space to write the message in and a button to send the message and a text area that shows the whole conversation, something like this
for this code
import java.io.*; import java.net.*; class UDPServerSide { public static void main(String args[]) throws Exception { //STEP 1 Create a datagram socket object //and pass the port number or declare it first and use the variable name DatagramSocket ServerSocketObject = new DatagramSocket(7799);
byte[] SendData = new byte[1024]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Server Ready.. Wating for Client"); while (true) { //STEP 2 Create a buffer for incoming datagrams //by creating an array of bytes byte[] RecieveData = new byte[1024];
//STEP 3 Create a datagram packet object for the incoming datagrams //this should take two parameters the byte array and size DatagramPacket IncomingPacket = new DatagramPacket(RecieveData,RecieveData.length);
//STEP 4 Accept incoming datagram packet ServerSocketObject.receive(IncomingPacket);
//STEP 6 Retrieve the data from the buffer //we use String because the data comes in form of bytes String message = new String(IncomingPacket.getData());
//if the client did not enter anyting.. break if (message.trim().length() == 0) break; //if there is a message .. print it System.out.println("Client: " + message); System.out.print("Type Your Message: "); String ServerMessage = br.readLine();
//STEP 5 Accept the senders address and port from the incoming packet InetAddress IPaddress = IncomingPacket.getAddress();
SendData = ServerMessage.getBytes();
//STEP 7 Create the response datagram //this takes 4 arguments //1- the byte array containing the response message //2- the size of the response //3- the clients address //4- the clients port number DatagramPacket OutgoingPacket = new DatagramPacket(SendData,SendData.length, IPaddress, 8899);
//STEP 8 Send the response datagram ServerSocketObject.send(OutgoingPacket); } System.out.println("The Client Left The Chat Room"); //STEP 9 Close the datagram socket ServerSocketObject.close(); }}
--------------------------------
--------------------------------
import java.io.*; import java.net.*; class UDPClientSide { public static void main(String args[]) throws Exception { //standard input stream BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
//STEP 1 Create a datagram socket object //and pass the port number or declare it first and use the variable name DatagramSocket ClientSocketObject = new DatagramSocket(8899);
InetAddress IPa; String message; if (args.length == 0) IPa = InetAddress.getLocalHost(); else IPa = InetAddress.getByName(args[0]); byte[] SendData = new byte[1024]; System.out.println("Press Enter without text to quit"); while (true) { System.out.print("Type Your Message: "); message = br.readLine(); SendData = message.getBytes();
//STEP 2 Create the outgoing datagram DatagramPacket OutgoingPacket = new DatagramPacket(SendData,SendData.length, IPa, 7799 ); //STEP 3 Send the datagram message ClientSocketObject.send(OutgoingPacket);
if (message.trim().length() == 0) break;
//STEP 4 Create a buffer for incoming datagrams byte[] RecieveData = new byte[1024]; //STEP 5 Create a datagram packet object for the incoming datagrams DatagramPacket IncomingPacket = new DatagramPacket(RecieveData,RecieveData.length); //STEP 6 Accept an incoming datagram ClientSocketObject.receive(IncomingPacket); //STEP 7 Retrieve the data from the buffer String response = new String(IncomingPacket.getData()) ;
response = response.trim(); System.out.println("From Server CLIENT SIDE type message here jButton1 Clinet: hi Server: hello Client: this is for chegg Server: ord
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started