Question

Design and implement a Java program that enables two users to chat. Using GUIs implement one...

Design and implement a Java program that enables two users to chat. Using GUIs implement one user as the server and the other as the client. The sever has two text areas: one for entering text and the other (noneditable) for displaying text received from the client. The client has two text areas: one for receiving text from the server and the other for entering text. When the user presses the enter key, the current line is sent to the server or client.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TCPClient {

   public static void main(String argv[]) throws Exception {
       String word;
       String wordsent;
     
       BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
       Socket clientSocket = new Socket("localhost", 6789);
       while (true) {
           DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream());
           BufferedReader inFromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));
           word = inFromUser.readLine();
           outToServer.writeBytes(word + '\n');
           if (word.equals("EXIT")) {
               break;
           }
           wordsent = inFromServer.readLine();
           System.out.println("FROM SERVER: " + wordsent);
       }
       clientSocket.close();
   }
}

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPServer {

   public static void main(String argv[]) throws Exception {
       ServerSocket socket = new ServerSocket(6789);
       Responder h = new Responder();
       while (true) {
           Socket connSocket = socket.accept();
           Thread t = new Thread(new ChatServer(h,connSocket));
           t.start();
       }  
   }
}

class ChatServer implements Runnable {

   Responder resp;
   Socket socketConnection;

   public ChatServer(Responder r, Socket connSocket) {
           this.resp = r;
           this.socketConnection = connSocket;
   }

   @Override
   public void run() {
       while (resp.responderMethod(socketConnection)) {
           try {
               Thread.sleep(2000);
           }
           catch (InterruptedException ex) {
               ex.printStackTrace();
           }
       }
       try
       {
           socketConnection.close();
       }
       catch (IOException ex) {
           System.out.println("Exception occured: "+ex.toString());
       }
   }

}

class Responder {
   String word;
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   synchronized public boolean responderMethod(Socket connectionSocket) {
       try {
           BufferedReader inFromClient = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream()));
           DataOutputStream outToClient =new DataOutputStream(connectionSocket.getOutputStream());
           String clientSentence = inFromClient.readLine();
           if (clientSentence == null || clientSentence.equals("EXIT")) {
               return false;
           }
           if (clientSentence != null) {
               System.out.println("client : " + clientSentence);
           }
           word = br.readLine() + "\n";
           outToClient.writeBytes(word);
           return true;
       }
       catch (SocketException e) {
           System.out.println("Disconnected from the server");
           return false;
       }
       catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Design and implement a Java program that enables two users to chat. Using GUIs implement one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROBLEM: Design and implement a Java program that enables two users to chat. Using GUIs implement...

    PROBLEM: Design and implement a Java program that enables two users to chat. Using GUIs implement one user as the server and the other as the client. The sever has two text areas: one for entering text and the other (noneditable) for displaying text received from the client. The client has two text areas: one for receiving text from the server and the other for entering text. When the user presses the enter key, the current line is sent to...

  • In this project,use java to write a chat program that enables real-time communication between 3 persons....

    In this project,use java to write a chat program that enables real-time communication between 3 persons. Specifically the program should permit chat sessions between 2 well as 3 persons,. In case of 3 persons, messages generated by one should reach the other two (Its a conferencing facility and not an individual chat facility). The chat program needs to allow a user to type messages, which should be conveyed to the other end(s) and displayed at the other end(s). The user...

  • implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of...

    implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...

  • In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in whic...

    In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in which the server is multi-threaded. Then, you need to implement the server-side of the chat-room application in Java (implementing the client-side is optional). The server maintains a list (an ArrayList will work well) of all the active connections. It will listen on a port for a new...

  • I have to modify a server program and chat program to work as the following instructions...

    I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

  • Select one favorite application of yours (in e-commence, education, games, and so on). Design a Java...

    Select one favorite application of yours (in e-commence, education, games, and so on). Design a Java FxGUI for a "customer" to access the application of your design. the application of your design. 1. Makes sure you have sufficient components (2 or more buttons, text fields/areas, a selection list, 1 or more images and other optional components of your choice). In addition, add an "account" information so that a customer/user may access or utilize the application. Your GUI application shall also...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT