Hello everyone, I am trying to build this simple chat program that consist of two classes. One is Server class and the other one is Clint class, but I have this problem with it. It does send and receive strings, but after the the second received string it does stop.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

    private static String hostIP = "localhost";
    private static int port = 8523;
    private static Socket socket = null;
    private static InputStream is = null;
    private static DataInputStream dis = null;
    private static OutputStream os = null;
    private static DataOutputStream dos = null;
    private static DataInputStream input = null;
    private static String str= null;

    public static void main(String[] args) throws IOException {
       while(true){           
            socket = new Socket(hostIP, port);
            is = socket.getInputStream();
            dis = new DataInputStream(is);
            input = new DataInputStream(System.in);
            os = socket.getOutputStream();
            dos = new DataOutputStream(os);
           
            String incoming = dis.readUTF();
            System.out.println(incoming);

            System.out.println("\nwrite: ");
            str = input.readLine();
            dos.writeUTF(str);
            is.close();
            dis.close();
            input.close();
            os.close();
            dos.close();
       }
    }
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {

    private static int port = 8523;
    private static ServerSocket serverSocket = null;
    private static Socket socket = null;
    private static InputStream is = null;
    private static DataInputStream dis = null;
    private static OutputStream os = null;
    private static DataOutputStream dos = null;
    private static DataInputStream input = null;
    private static String str= null;

    public static void main(String[] args) throws IOException {
     while(true){
            serverSocket = new ServerSocket(port);
            socket = serverSocket.accept(); // Wait and accept a connection
            is = socket.getInputStream();
            dis = new DataInputStream(is);
            input = new DataInputStream(System.in);
            os = socket.getOutputStream();
            dos = new DataOutputStream(os);
        
            System.out.println("Write ur m: ");
            str = input.readLine();
            dos.writeUTF(str);

            String incoming = dis.readUTF();
            System.out.println(incoming);

           
            is.close();
            dis.close();
            input.close();
            os.close();
            dos.close();

    }
   }
}

Try to create seperate threads for reading from socket and from the user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.