Question

Create a simple messaging program application using Java. You can use sockets, ip addresses, etc.

Create a simple messaging program application using Java. You can use sockets, ip addresses, etc.

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

CLIENT SIDE.

import java.io.*;
import java.net.*;
public class Chat
{
public static void main(String[] args) throws Exception
{
     Socket s = new Socket("127.0.0.1", 3000);
                               // reading from keyboard
     BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
                              // sending to client
     OutputStream ostream = s.getOutputStream();
     PrintWriter p = new PrintWriter(ostream, true);
                              // receiving from server
     InputStream istream = s.getInputStream();
     BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

     System.out.println("Start chat, type and press Enter key");

     String receiveM, sendM;             
     while(true)
     {
        sendM = keyRead.readLine(); // keyboard reading
        p.println(sendM);       // sending to server
        p.flush();                  
        if((receiveM = receiveRead.readLine()) != null) //receive from server
        {
            System.out.println(receiveM); // displaying at DOS
        }       
      }             
    }                  
}

server side

import java.io.*;
import java.net.*;
public class ChatS
{
public static void main(String[] args) throws Exception
{
      ServerSocket ss = new ServerSocket(3000);
      System.out.println("Server ready for chatting");
      Socket s = ss.accept( );                        
                              // reading from keyboard
      BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
                          // sending to client
      OutputStream ostream = s.getOutputStream();
      PrintWriter p = new PrintWriter(ostream, true);

                              // receiving from server
      InputStream istream = s.getInputStream();
      BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

      String receiveM, sendM;             
      while(true)
      {
        if((receiveM = receiveRead.readLine()) != null)
        {
           System.out.println(receiveM);       
        }       
        sendM = keyRead.readLine();
        p.println(sendM);           
        p.flush();
      }             
    }                  
}

Add a comment
Know the answer?
Add Answer to:
Create a simple messaging program application using Java. You can use sockets, ip addresses, etc.
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
  • Write a program that shows the operation of TCP/IP socket. You are allowed to use any...

    Write a program that shows the operation of TCP/IP socket. You are allowed to use any programming language. You have to submit your code with a screenshot of the result after running the code. Useful links: https://www.javaworld.com/article/2077322/core-java-sockets-programming-in-java-atutorial.html https://www.c-sharpcorner.com/article/socket-programming-in-cpp-using-boost-asio-tcpserver-and-client/

  • 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 lab you will write a simple chat application in Python using TCP sockets. The...

    In this lab you will write a simple chat application in Python using TCP sockets. The chat application consists of a chat server and a chat client. At any one time, the chat server is communicating with just one chat client. When the client and the server are done chatting, they exchange end messages which ends the session. The client and server read and write Unicode strings using the readUTF() and writeUTF() methods of DataInputStream and DataOutputStream respectively. After compilation,...

  • The purpose of this Java application is to create a simple user interface on top of...

    The purpose of this Java application is to create a simple user interface on top of a relational database. The database you should use is the Java DB database. Setup of the Java DB database is covered in section 24.5. The user interface will allow a user to insert, delete, and update names from a table in the database. The database table will look like this: ID FIRST_NAME LAST_NAME 1452962065165       Rita                           Red                           1452962067770       Oscar                          Orange                        1452962070010       Yet                            The ID value...

  • what is the solution for this Java project? Create the an application that can use different...

    what is the solution for this Java project? Create the an application that can use different types of Linked List to manage either Checking Account or Saving Account. The application should allow users can select the type of Linked List to work on. After finishing one, users can select to work with other type of Linked List until they want to exit Singly Linked List Singly Linked List with Iterator Java Linked List with Iterator 1. 2. 3. You can...

  • Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that...

    Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values

  • Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by...

    Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by step on how to run the code.

  • Write a program that shows the operation of TCP/IP socket. (Java) Please if you can, put...

    Write a program that shows the operation of TCP/IP socket. (Java) Please if you can, put a comment to let me understand the code better.

  • We use bluej for our JAVA program. If you can help id greatly appreciate it. Create...

    We use bluej for our JAVA program. If you can help id greatly appreciate it. Create a new Java program called Flip. Write code that creates and populates an array of size 25 with random numbers between 1-50. Print the array. Print array in reverse.

  • in java : Create Java Application you are to create a project using our designated IDE...

    in java : Create Java Application you are to create a project using our designated IDE (which you must download to your laptop). Create the code to fulfill the requirements below. Demonstrate as stipulated below. Create a Main Application Class called AddressBookApplication (a counsole application / command line application). It should Contain a Class called Menu that contains the following methods which print out to standard output a corresponding prompt asking for related information which will be used to update...

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