Memory  bot
Memory bot

Reputation: 45

Print Socket Messages to Console in java

  1. I am beginner to java and learning Socket Programming.I am using the basic chat server socket communication. I am having difficulty to print the server and client messages to the console window.

  2. I would also implement this concept when i design my chat Server window UI and will update the char server intercommunication messages to my UI. I would like to know as how can I achieve that ?

Code for 1
Server.java

package ChApp;

import java.io.IOException;
import java.net.*;
public class Server  {
    public static void main(String[] args) throws Exception {
        Socket s;
        ServerSocket server = new ServerSocket(3900);
        while(true)
        {

        s = server.accept();
        ServerHandl handle1 = new ServerHandl(s);
        Thread t1= new Thread(handle1);
        t1.start();
        System.out.println("Connection Succesful...");
        server.close();
        }
    }

}

Serverhandl.java

package ChApp;
import java.io.*;
import java.net.*;

public class ServerHandl implements Runnable {
    Socket s= null;
    BufferedReader read;
    PrintWriter write;
    String msg="Server is sending a sample msg";
        public ServerHandl(Socket s)
    {
        this.s = s;

    }
    public void run()
    {

        try {
            write = new PrintWriter(s.getOutputStream());
            write.println(msg);
            read = new BufferedReader(new InputStreamReader(s.getInputStream()));
            System.out.println(read.readLine());


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{

            try {
                read.close();
                write.close();
                s.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }


}

Client.java

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

public class Client {

    public static void main(String[] args) throws IOException {
        Socket s= null;
        BufferedReader read;
        PrintWriter write = null;
        String h;
        StringBuilder sb = new StringBuilder();
        String sendmsg="Reply from client";
        s= new Socket("localhost",3900);
        read = new BufferedReader(new InputStreamReader(s.getInputStream()));
        while((h=read.readLine())!=null)
        {

            sb.append(h);

        }
        write = new PrintWriter(s.getOutputStream(),true);
        write.write(sendmsg);
        write.flush();
        s.close();
        read.close();
        write.close();

    }

}

Upvotes: 1

Views: 3481

Answers (1)

user207421
user207421

Reputation: 310884

Your client is calling readLine() until it returns null, but your server is reading from the connection so it hasn't closed it yet, so the null will never arrive, so you're deadlocked.

Read one line from the server and then send a response, then close the socket. Have the server close the socket after it calls readLine().

Upvotes: 1

Related Questions