Java - 仅在客户端停止时响应服务器

时间:2015-07-27 20:45:45

标签: java multithreading client server

我最近编写了一个简单的Java服务器和一个客户端来测试服务器。服务器应该从客户端接收消息,并发回消息的随机子串。问题是:当我使用客户端程序发送消息时,服务器不响应。然后,当我终止客户端程序时,服务器跳跃到行动,并尝试将数据发送回客户端。服务器正确读取数据,但只有在我停止客户端程序时才开始处理它。

这是客户端代码:

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

class ServerTest{

public static void main(String args[]) throws Exception{

    Socket clientSocket = new Socket(myIpAdress, 8001);

    //Send the message to the server
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

    String sendMessage = "randSubstring:StackOverflowIsAwsome";

    bw.write(sendMessage);
    bw.flush();
    System.out.println("Message sent: "+sendMessage);


    String message = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())).readLine();

    System.out.println("Message received from the server : " +message);

    clientSocket.close();

    }

}

我的服务器代码包含两个类。这个是听众:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerListener {
public static void main(String args[]) throws Exception {
    String clientSentence;
    ServerSocket socket = new ServerSocket(8001);

    while(true) {
        Socket connectionSocket = socket.accept();
        BufferedReader input = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        //DataOutputStream output = new DataOutputStream(connectionSocket.getOutputStream());
        clientSentence = input.readLine();

        if(clientSentence.startsWith("randSubstring:")){

            Thread connection = new Thread(new ServerConnection(connectionSocket, clientSentence));
            connection.start();
        }

        Thread.sleep(300);
    }

}
}

这是在客户端停止之前不会启动的线程:

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Random;

public class ServerConnection implements Runnable{

private Socket serverConnection;
private String sentence;

public ServerConnection(Socket connection, String clientSentence){

serverConnection = connection;
sentence = clientSentence;

}

@Override
public void run() {
    Random r = new Random();
    String substring = sentence.substring(0, r.nextInt(sentence.length()));

try {
        OutputStream os = serverConnection.getOutputStream();
        OutputStream out = new BufferedOutputStream(os);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        bw.write(substring);
        bw.close();
        out.close();
        os.close();


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

我正在使用带优胜美地的Macintosh。这是因为我试图在同一台计算机上运行程序,或者如果程序是从不同的计算机运行会出现问题吗?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

在服务器中,你执行readLine(..),这意味着它将等待行尾字符。

但是在您的发件人代码中,您只需发送一个没有行结尾的字符串。

因此,要么你确保你也发送了行尾char或你的服务器等待其他东西作为" delimiter"

答案 1 :(得分:0)

你正在读一条线,但你不是在写一条线。在发送的消息中添加行终止符。否则readLine()将不会返回,直到对等方关闭连接。

NB接受后try块中的I / O应该在Runnable中,而不是在Runnable中。不要在接受循环中执行I / O.