简体   繁体   中英

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();

    }

}

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM