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

  • Create a simple Java application that will exhibit concurrency concepts. Your application should create two threads...

    Create a simple Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, please be provide a detailed analysis of appropriate concepts that could impact your application. Specifically, please address: Performance issues with concurrency Vulnerabilities exhibited with use of strings Security of the data...

  • MUST USE JAVAFX FOR THIS PROGRAM Create a application that acts as a simple calculator.  Create buttons...

    MUST USE JAVAFX FOR THIS PROGRAM Create a application that acts as a simple calculator.  Create buttons for 0-9 and a text field that displays the concatenation of the current digits as they are clicked.  Add buttons for operators “+”, “-“, “*”, and “/”.  Add a final button for “=” that calculates the computed value with the value entered in the text field.  For instance, clicking buttons 7, +, 8, =  one-by-one, then it should display 15.  

  • 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.

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