Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program is a java program, its like an IM program. The purpose of this IM is for clients to talk with each other. It's

This program is a java program, its like an IM program. The purpose of this IM is for clients to talk with each other. It's in its early days but, the client send a message to the the server. The server sends the message to the other client(forwards the message). For some reason the secocnd client is not recieving the message from the server. Later ill try to encrypt the message to prevent MITM(man in the middle attack).

Server

import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.util.*;

public class Server extends JFrame { private JTextArea displayArea; // displays packets received private DatagramSocket socket; // socket to connect to client

// set up GUI and DatagramSocket public Server() { super( "Server" );

displayArea = new JTextArea(); // create displayArea add( new JScrollPane( displayArea ), BorderLayout.CENTER ); setSize( 400, 300 ); // set size of window setVisible( true ); // show window

try // create DatagramSocket for sending and receiving packets { socket = new DatagramSocket( 5000 ); } // end try catch ( SocketException socketException ) { socketException.printStackTrace(); System.exit( 1 ); } // end catch } // end Server constructor

// wait for packets to arrive, display data and echo packet to client public void waitForPackets() { while ( true ) { try // receive packet, display contents, return copy to client { byte data[] = new byte[ 100 ]; // set up packet DatagramPacket receivePacket = new DatagramPacket( data, data.length );

socket.receive( receivePacket ); // wait to receive packet

// display information from received packet displayMessage( " Packet received:" + " From host: " + receivePacket.getAddress() + " Host port: " + receivePacket.getPort() + " Length: " + receivePacket.getLength() + " Containing: \t" + new String( receivePacket.getData(), 0, receivePacket.getLength() ) );

sendPacketToClient( receivePacket ); // send packet to client } // end try catch ( IOException ioException ) { displayMessage( ioException.toString() + " " ); ioException.printStackTrace(); } // end catch } // end while } // end method waitForPackets

// echo packet to client private void sendPacketToClient( DatagramPacket receivePacket ) throws IOException { displayMessage( " Echo data to client..." );

// create packet to send DatagramPacket sendPacket = new DatagramPacket( receivePacket.getData(), receivePacket.getLength(), receivePacket.getAddress(), receivePacket.getPort() );

socket.send( sendPacket ); // send packet to client displayMessage( "Packet sent " ); } // end method sendPacketToClient

// manipulates displayArea in the event-dispatch thread private void displayMessage( final String messageToDisplay ) { SwingUtilities.invokeLater( new Runnable() { public void run() // updates displayArea { displayArea.append( messageToDisplay ); // display message } // end method run } // end anonymous inner class ); // end call to SwingUtilities.invokeLater } // end method displayMessage }

Client

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

public class Client extends JFrame { private JTextField enterField; // for entering messages private JTextArea displayArea; // for displaying messages private DatagramSocket socket; // socket to connect to server

// set up GUI and DatagramSocket public Client() { super( "Client" );

enterField = new JTextField( "Type message here" ); enterField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { try // create and send packet { // get message from textfield String message = event.getActionCommand(); // displayArea.append( " Sending packet containing: " + message + " " );

byte data[] = message.getBytes(); // convert to bytes // create sendPacket DatagramPacket sendPacket = new DatagramPacket( data, data.length, InetAddress.getLocalHost(), 5000 );

socket.send( sendPacket ); // send packet //displayArea.append( "Packet sent " ); //displayArea.setCaretPosition(displayArea.getText().length() ); } // end try catch ( IOException ioException ) { displayMessage( ioException.toString() + " " ); ioException.printStackTrace(); } // end catch } // end actionPerformed } // end inner class ); // end call to addActionListener

add( enterField, BorderLayout.NORTH );

displayArea = new JTextArea(); add( new JScrollPane( displayArea ), BorderLayout.CENTER );

setSize( 400, 300 ); // set window size setVisible( true ); // show window

try // create DatagramSocket for sending and receiving packets { socket = new DatagramSocket(); } // end try catch ( SocketException socketException ) { socketException.printStackTrace(); System.exit( 1 ); } // end catch } // end Client constructor

// wait for packets to arrive from Server, display packet contents public void waitForPackets() { while ( true ) { try // receive packet and display contents { byte data[] = new byte[ 100 ]; // set up packet DatagramPacket receivePacket = new DatagramPacket( data, data.length );

socket.receive( receivePacket ); // wait for packet

// display packet contents displayMessage( //" Packet received:" + //" From host: " + receivePacket.getAddress() + //" Host port: " + receivePacket.getPort() + //" Length: " + receivePacket.getLength() + " Containing: \t" + new String( receivePacket.getData(), 0, receivePacket.getLength() ) ); } // end try catch ( IOException exception ) { displayMessage( exception.toString() + " " ); exception.printStackTrace(); } // end catch } // end while } // end method waitForPackets

// manipulates displayArea in the event-dispatch thread private void displayMessage( final String messageToDisplay ) { SwingUtilities.invokeLater( new Runnable() { public void run() // updates displayArea { displayArea.append( messageToDisplay ); } // end method run } // end inner class ); // end call to SwingUtilities.invokeLater } // end method displayMessage } // end class Client

Server Test

import javax.swing.JFrame;

public class ServerTest { public static void main( String args[] ) { Server application = new Server(); // create server application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.waitForPackets(); // run server application } // end main }

Clinet Test

import javax.swing.JFrame;

public class ClientTest { public static void main( String args[] ) { Client application = new Client(); // create client application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.waitForPackets(); // run client application } // end main }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions