在java中将套接字消息打印到控制台

时间:2014-07-13 12:25:30

标签: java sockets

  1. 我是Java初学者和学习Socket编程。我正在使用基本的聊天服务器套接字通信。我很难将服务器和客户端消息打印到控制台窗口。

  2. 我在设计聊天服务器窗口UI时也会实现这个概念,并将char服务器互通消息更新到我的UI。我想知道如何实现这一目标?

  3. 代码为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();
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

您的客户端正在调用readLine(),直到它返回null,但您的服务器正在从连接读取,因此它尚未关闭它,因此null将永远不会到达,因此您已经死锁。 / p>

从服务器读取一行行,然后发送响应,然后关闭套接字。让服务器在调用readLine()后关闭套接字。