0

I have a Class create a serversocket and it's working.

I connect to the server with TCP&UDP debug tools. and I found what I send is blocked until I disconnect.

So I can't recieved any message from server in my socket client also can't get any message from client in server until disconnect.

here is my class:

package com.udpdemo.multicast;

import java.net.*;
import java.io.*;

public class Myserver extends Thread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Myserver myserver = new Myserver();
        myserver.startServer();
        System.out.println("sever start");
    }

    private boolean started = false;
    private int port = 3333;

    public Myserver() {
    }

    public void run() {
        try {
            ServerSocket server = new ServerSocket(port, 0);

            while (started) {
                Socket socket = null;
                try {
                    socket = server.accept();
                    Thread workThread = new Thread(new Handler(socket)); // 创建一个工作进程
                    workThread.start(); // 启动工作进程
                } catch (IOException ee) {
                    ee.printStackTrace();
                } finally {
                    // socket.close();
                }
            }
            server.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void startServer() {
        started = true;
        this.start();
    }

    public void stopServer() {
        started = false;
    }

    class Handler implements Runnable {

        private Socket socket;

        public Handler(Socket socket) {
            this.socket = socket;
        }

        private PrintWriter getWriter(Socket socket) throws IOException {
            OutputStream socketOut = socket.getOutputStream();
            return new PrintWriter(socketOut, true);
        }

        private BufferedReader getReader(Socket socket) throws IOException {
            InputStream socketIn = socket.getInputStream();
            return new BufferedReader(new InputStreamReader(socketIn));
        }

        public String echo(String msg) {
            return "echo:" + msg;
        }

        public void run() {
            // TODO Auto-generated method stub
            try {
                System.out.println("New connection accepted "
                        + socket.getInetAddress() + ":" + socket.getPort());
                BufferedReader br = getReader(socket);
                PrintWriter pw = getWriter(socket);

                String msg = null;
                while ((msg = br.readLine()) != null) { // 接收和发送数据, 直到通信结束

                    System.out.println(msg);
                    pw.println(echo(msg));
                    if (msg.equals("bye")) {
                        break;
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null)
                        socket.close(); // 断开连接
                } catch (IOException e) {
                }
            }
        }
    }

}
chanjianyi
  • 607
  • 4
  • 15
  • 35

0 Answers0