0% found this document useful (0 votes)
35 views8 pages

Bridge

The document contains code for a Java program that implements a basic UDP client-server application for chat messaging. The Bridge class acts as a middleman to forward messages between the client and server. The UDPClient class defines the client that allows user input and displays received messages. The UDPServer class defines the server that handles receiving messages, saving them to a file, and sending back capitalized messages to the client.

Uploaded by

Pratik Bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Bridge

The document contains code for a Java program that implements a basic UDP client-server application for chat messaging. The Bridge class acts as a middleman to forward messages between the client and server. The UDPClient class defines the client that allows user input and displays received messages. The UDPServer class defines the server that handles receiving messages, saving them to a file, and sending back capitalized messages to the client.

Uploaded by

Pratik Bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Bridge.

java file
import java.net.*;

public class Bridge {

public static void main(String[] args) throws Exception {

DatagramSocket receiveFromClientSocket = new DatagramSocket(4000);

DatagramSocket sendToServerSocket = new DatagramSocket();

byte[] receivedFromClientData = new byte[1024];

byte[] sendToServerData = new byte[1024];

byte[] receivedFromServerData = new byte[1024];

byte[] sendToClientData = new byte[1024];

while (true) {

DatagramPacket receivedFromClientPacket = new DatagramPacket(receivedFromClientData,

receivedFromClientData.length);

receiveFromClientSocket.receive(receivedFromClientPacket);

String clientSentence = new String(receivedFromClientPacket.getData());

System.out.println("RECEIVED FROM CLIENT AND SENDING TO SERVER: " + clientSentence);

sendToServerData = clientSentence.getBytes();

DatagramPacket sendToServerPacket = new DatagramPacket(

sendToServerData, sendToServerData.length, 5000);

sendToServerSocket.send(sendToServerPacket);

}
}

UDP_chat.java file

/**

* Write a description of class UDP_Chat here.

* @author (your name)

* @version (a version number or a date)

*/

public class UDP_Chat

// instance variables - replace the example below with your own

private int x;

/**

* Constructor for objects of class UDP_Chat

*/

public UDP_Chat()

// initialise instance variables

x = 0;

}
/**

* An example of a method - replace this comment with your own

* @param y a sample parameter for a method

* @return the sum of x and y

*/

public int sampleMethod(int y)

// put your code here

return x + y;

}
UDPClient.java file

/**

* Write a description of class UDPClient here.

* https://systembash.com/a-simple-java-udp-server-and-udp-client/

* @author J Hamlyn-Harris

* @version 1.0

*/

import java.io.*;

import java.net.*;

import java.util.Scanner;

class UDPClient {

public static void main(String args[]) throws Exception {

System.out

.print("Enter the filename to download from server or enter 'q' to proceed to


capitalization program: ");

Scanner sc = new Scanner(System.in);

String fileName = "\\f" + sc.nextLine();

if (!fileName.equalsIgnoreCase("\\fq")) {

byte[] b = new byte[1024];

FileInputStream f = new FileInputStream(fileName);

DatagramSocket fileSocket = new DatagramSocket(4000);

int i = 0;
while (f.available() != 0) {

b[i] = (byte) f.read();

i++;

f.close();

fileSocket.send(new DatagramPacket(b, i, InetAddress.getLocalHost(), 5000));

DatagramSocket clientSocket = new DatagramSocket();

String sentence = "";

while (!sentence.equals("\\q")) {

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

InetAddress IPAddress = InetAddress.getByName("127.0.0.1");

byte[] sendData = new byte[1024];

byte[] receiveData = new byte[1024];

sentence = inFromUser.readLine();

sendData = sentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,


IPAddress, 4000);

clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());

System.out.println("FROM SERVER:" + modifiedSentence);

}
clientSocket.close();

UDPServer.java file

/**

* Write a description of class UDPServer here.

* @author J Hamlyn-Harris

* https://systembash.com/a-simple-java-udp-server-and-udp-client/

* @version 1.0

*/

import java.io.*;

import java.net.*;

class UDPServer {

public static void main(String args[]) throws Exception {

String fileName = "text.txt";

initializeFile(fileName);

DatagramSocket serverSocket = new DatagramSocket(5000);

byte[] receiveData = new byte[1024];

byte[] sendData = new byte[1024];


while (true) {

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData());

System.out.println("RECEIVED: " + sentence);

BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));

writer.append(sentence).append("\n");

writer.close();

InetAddress IPAddress = receivePacket.getAddress();

int port = receivePacket.getPort();

FileOutputStream fout = new FileOutputStream(sentence);

if (sentence.charAt(0) == '\\' && sentence.charAt(1) == 'f') {

DatagramPacket filePacket = new DatagramPacket(sendData, sendData.length);

serverSocket.receive(filePacket);

System.out.println(new String(filePacket.getData(), 0, filePacket.getLength()));

String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,


IPAddress, port);

serverSocket.send(sendPacket);

}
}

private static void initializeFile(String fileName) throws Exception {

BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

writer.write("");

writer.close();

You might also like