2 renrenai35 renrenai35 于 2014.02.21 21:49 提问

有关于Socket的问题,简单的服务器程序与客户程序之间的通信

问题是这样的,我写了两个简单 的服务器程序,其中一个与一个简单的客户程序可以连续发多次信息,而另一个则不可以,发一次它就会不能继续再发了,程序并没有停止运行,而是服务器收不到它的信息了,求大侠帮忙看下啦~以下为两个服务器程序代码。因为我用的是同一个客户程序测试,所以跟client无关
吧。

//这个是可以连续发送信息的服务器
package Test20140210;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {
private int port=8001;
private ServerSocket serverSocket;

public EchoServer() throws IOException{
    serverSocket=new ServerSocket(port);
    System.out.println("Server Started .");
}

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

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 void service(){
    while (true){
        Socket socket=null;
        try{
            socket=serverSocket.accept();                   //等待客户连接
            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("NEW Line : "+msg);                  //在服务器程序上打印
                pw.println(echo("My message : "+msg));                  //在客户程序上打印
                if(msg.equals("bye"))
                    break;
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        finally{
            try{
                if(socket!=null)
                    socket.close();
            }
            catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

public static void main(String[] args) throws IOException {
    new EchoServer().service();
}

}

//以下这个是不可以连续发的服务器
package Test20140221;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EchoServer {
private int port = 8001;
private ServerSocket serverSocket;
private ExecutorService executorService;
private final int POOL_SIZE=4;

public EchoServer() throws IOException{
    serverSocket=new ServerSocket(port);
    executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
    System.out.println("numbers of CPU : "+Runtime.getRuntime().availableProcessors());
    System.out.println("Server started.");
}

public void service(){
    while(true){
        Socket socket=null;
        try{
            socket=serverSocket.accept();
            executorService.execute(new Handler(socket));
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
public static void main(String[] args) throws IOException{
    new EchoServer().service();
}
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); 
    }
    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(){
        try{
            System.out.println("New connection accepted"
                    +socket.getInetAddress()+ " : "+socket.getPort());
            BufferedReader br=getReader(socket);
            PrintWriter pw=getWriter(socket);

            System.out.println("this is Handler!");
            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) {
                e.printStackTrace();
            }
        }
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片