多线程mutisocket程序

时间:2014-03-17 11:18:39

标签: java multithreading sockets serversocket

我想创建一个服务器,它创建一个客户端并开始与他进行对话但是在Handler的代码中发生IOException我无法理解为什么Br.readLine方法抛出异常 这是我的服务器项目包的classess代码和两个客户abc,def类也是

这是服务器项目的代码classeess ...............

        package server;
        import java.io.IOException;
        import java.net.ServerSocket;
        public class Server {
            private void operate() {
                try {
                    ServerSocket serverSocket = new ServerSocket(2222);
                    while(true) new Thread(new Handler(serverSocket.accept())).start();
                } catch(IOException e){
                    System.out.println("IOException in operate method of Server");
                }
            }    
            public static void main(String[] args) {
                new Server().operate();
            }
        }

package server;
import java.io.*;
import java.net.Socket;
public class Handler implements Runnable {
    Handler(Socket s) {
        socket = s;
        counter++;
    }
    public void run() {
        try {
            while(true) System.out.println(new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine());                 //This throw the IOExceptionnnnnnnnnnnnnnnnnnnnn...............
        } catch(IOException e) {
            System.out.println("IOException in "+counter+"'s run method");
        }
    }
    private final Socket socket; 
    private static int counter =0;
}

第一客户ABC代码...........................

package abc;
import java.net.Socket;
import java.io.*;
public class Abc {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",2222);
            while(true) new PrintWriter(socket.getOutputStream()).println("HI from Abc");
        } catch(IOException e) {
            System.out.println("IOException in main ");
        }
    }
}

另一位客户的代码DEf .........................

package def;
import java.io.*;
import java.net.Socket;
public class DEf {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",2222);
            while(true) new PrintWriter(socket.getOutputStream()).println("HI from Abc");
        } catch(IOException e) {
            System.out.println("IOException in main ");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的客户端使用socket.getOutputStream()从套接字重复请求输出流。相反,您应该调用此方法并仅创建一次相应的编写器,例如:

Socket socket = new Socket("localhost",2222);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
while(true) {
  writer.println("HI from Abc");
  ...
}

与Handler类相同 - 创建一次缓冲读取器。

答案 1 :(得分:0)

我已经在服务器 - 客户端套接字通信上发布了答案。请看看。


试试这段代码。它可能会解决你的问题。

Handler.java:

  • BufferedReader.ready() 之前检查BufferedReader.readLine()

  • 使用单BufferedReader

    class Handler implements Runnable {
        private BufferedReader reader;
    
        Handler(Socket s) {
            socket = s;
            counter++;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
            try {
                while (socket.isConnected() && !socket.isClosed()) {
                    if(!reader.ready()){
                        continue;
                    }
                    //System.out.println("ready");
                    System.out.println(reader.readLine()); // This throw
                                                           // the
                } // IOExceptionnnnnnnnnnnnnnnnnnnnn...............
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("IOException in " + counter + "'s run method");
            }
        }
    
        private final Socket socket;
        private static int counter = 0;
    }
    

Abc.java:

  • 使用单PrintWriter

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        while (true)
            writer.println("HI from Abc");
    

DEf.java:

  • 使用单PrintWriter

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        while (true)
            writer.println("HI from Abc");