Question

In: Computer Science

Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over...

Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If you do not have any network environment, you can run on a single machine by instantiating your program multiple times. E.g. you can have program1 and program 2 running on same machine exchanging texts between themselves.

Solutions

Expert Solution

// Java implementation of Server side

// It contains two classes : Server and ClientHandler

// Save file as Server.java

import java.io.*;

import java.util.*;

import java.net.*;

// Server class

public class Server

{

// Vector to store active clients

static Vector<ClientHandler> ar = new Vector<>();

// counter for clients

static int i = 0;

public static void main(String[] args) throws IOException

{

// server is listening on port 1234

ServerSocket ss = new ServerSocket(1234);

Socket s;

// running infinite loop for getting

// client request

while (true)

{

// Accept the incoming request

s = ss.accept();

System.out.println("New client request received : " + s);

// obtain input and output streams

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

System.out.println("Creating a new handler for this client...");

// Create a new handler object for handling this request.

ClientHandler mtch = new ClientHandler(s,"client " + i, dis, dos);

// Create a new Thread with this object.

Thread t = new Thread(mtch);

System.out.println("Adding this client to active client list");

// add this client to active clients list

ar.add(mtch);

// start the thread.

t.start();

// increment i for new client.

// i is used for naming only, and can be replaced

// by any naming scheme

i++;

}

}

}

// ClientHandler class

class ClientHandler implements Runnable

{

Scanner scn = new Scanner(System.in);

private String name;

final DataInputStream dis;

final DataOutputStream dos;

Socket s;

boolean isloggedin;

// constructor

public ClientHandler(Socket s, String name,

DataInputStream dis, DataOutputStream dos) {

this.dis = dis;

this.dos = dos;

this.name = name;

this.s = s;

this.isloggedin=true;

}

@Override

public void run() {

String received;

while (true)

{

try

{

// receive the string

received = dis.readUTF();

System.out.println(received);

if(received.equals("logout")){

this.isloggedin=false;

this.s.close();

break;

}

// break the string into message and recipient part

StringTokenizer st = new StringTokenizer(received, "#");

String MsgToSend = st.nextToken();

String recipient = st.nextToken();

// search for the recipient in the connected devices list.

// ar is the vector storing client of active users

for (ClientHandler mc : Server.ar)

{

// if the recipient is found, write on its

// output stream

if (mc.name.equals(recipient) && mc.isloggedin==true)

{

mc.dos.writeUTF(this.name+" : "+MsgToSend);

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

try

{

// closing resources

this.dis.close();

this.dos.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

1. Send Message:-

Thread sendMessage = new Thread(new Runnable() {
@Override
public void run() {
while (true) {

// read the message to deliver.
String msg = sc.nextLine();
try {

// write on the output stream
dos.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});


2. Read messages:-

Thread readMessage = new Thread(new Runnable() {

@Override
public void run() {

while (true) {
try {

// read the message sent to this client
String msg = dis.readUTF();
System.out.println(msg);
} catch (IOException e) {

e.printStackTrace();
}
}
}
});

3. socket connection & communication:-

// Java implementation for multithreaded chat client
// Save file as Client.java

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client
{
final static int ServerPort = 1234;

public static void main(String args[]) throws UnknownHostException, IOException
{
Scanner scn = new Scanner(System.in);

// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");

// establish the connection
Socket s = new Socket(ip, ServerPort);

// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());

// sendMessage thread
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run() {
while (true) {

// read the message to deliver.
String msg = scn.nextLine();

try {
// write on the output stream
dos.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});

// readMessage thread
Thread readMessage = new Thread(new Runnable()
{
@Override
public void run() {

while (true) {
try {
// read the message sent to this client
String msg = dis.readUTF();
System.out.println(msg);
} catch (IOException e) {

e.printStackTrace();
}
}
}
});

sendMessage.start();
readMessage.start();

}
}


Related Solutions

in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
Use Java GUI to write a chessboard 8 Queens Problem Write a program that can place...
Use Java GUI to write a chessboard 8 Queens Problem Write a program that can place 8 queens in such a manner on an 8 x 8 chessboard that no queens attack each other by being in the same row, column or diagonal.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
Online bank loan sanction facility is launched to facilitate the client. Write a java program to...
Online bank loan sanction facility is launched to facilitate the client. Write a java program to create class Loan with data members client name, address, age, salary, loan amount, loan type(housing, vehicle, personal) . Take the necessary inputs and write the object into the file. The bank manager will fetch the loan details from the file and verify the details for approval. a. Write a java program for a client to submit the application in a file. b. Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT