java socket server

用java编写的一个socket服务端,通过一个tcp测试工具测试这个服务端,发现发送数据给服务端,服务端接收不到,当tcp测试工具断开连接后,测试工具发送的字符串才被服务端接收到,这是为什么?

java socket服务端代码
[code="java"]
package com.myserver.server;

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

public class MultiThreadServer {

private int port = 6666;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE = 10;//单个CPU线程池大小

public MultiThreadServer() throws IOException {
    serverSocket = new ServerSocket(port);
    //Runtime的availableProcessor()方法返回当前系统的CPU数目.
    executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE);
    System.out.println("server start");
}

public void service() {
    while (true) {
        Socket socket = null;
        try {
            //接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
            socket = serverSocket.accept();
            executorService.execute(new Handler(socket));
            Count.add();
            System.out.println("client number " + Count.num);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

}

class Handler implements Runnable {

private Socket socket;

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

public void run() {
    try {
        System.out.println("a client connect " + socket.getInetAddress() + ":" + socket.getPort());
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println("connect success");
        writer.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String readLine = reader.readLine();
        while (true) {
            if (readLine != null) {
                System.out.println(readLine);
                readLine = reader.readLine();
            } else {
                break;
            }
        }
        writer.close();
        reader.close();
    } catch (IOException e) {
        System.out.println("a client leave");
        Count.sub();
        System.out.println("client number " + Count.num);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

class Count {
public static int num = 0;

public static void add() {
    num += 1;
}

public static void sub() {
    num -= 1;
}

}
[/code]

java服务端状态输出如下
server start
client number 1
a client connect /127.0.0.1:50437
affff

tcp测试工具截图
[img]http://dl.iteye.com/upload/attachment/526919/4726dc79-31b2-3216-8887-de70efdd80cb.jpg[/img]

补充说明:
tcp测试工具能够正常连接到服务端,但是文本数据发送,服务端不会实时显示,只有当测试工具断开连接时,这时,服务端才显示刚才测试工具发送的文本数据

1个回答

不要在网络中使用readline
这个方法要判断换行符才结束的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问