因此。我正在写这个简单的聊天程序,基本上它是如何工作的,因为客户端和服务器轮流编写消息。现在我希望客户端和服务器能够连续发送多条消息,而不是等待对应的消息。
有关如何发送多条消息以及收到多条消息的任何建议。
我是网络应用程序的新手。
客户端的运行方法:
public void run(){
while (true){
System.out.println("Write message to server:");
String besked = scanner.nextLine();
oos.writeObject(besked);
System.out.println("MEssage from server: " + (String)ois.readObject());
oos.flush();
}
}
服务器的运行方法:
public void run() {
while(true){
String s = (String)ois.readObject();
System.out.println("Message from client: " +s);
System.out.println("Write back:");
String returbesked = scanner.nextLine();
oos.writeObject(returbesked);
oos.flush();
}
}
答案 0 :(得分:1)
您可以使用两个线程(对于连接的每一侧),一个从套接字读取数据,另一个将数据写入套接字。当服务器接受客户端连接时,它会创建两个线程(在代码示例中,它们被称为 Reader 和 Writer 线程)。当writer线程不断从源获取一些数据并将数据输出到套接字时,读取器线程同时继续从套接字读取数据。套接字连接的输入和输出流彼此独立,因此可以同时使用它们。
在下面的代码中,我试图实现服务器端,但对于客户端来说几乎是一样的:
public class Test {
public static void main(String[] args) {
Server server = new Server();
server.start(100); // server port no 100
}
}
class Server {
/**
* Thread that continuously reads data from socket.
*/
private Thread reader;
/**
* Thread that continuously writes data to socket.
*/
private Thread writer;
/**
* Start server
* @param port
* @throws IOException
*/
void start(int port) throws IOException {
ServerSocket srv = new ServerSocket(port);
// Wait for client connection
Socket clientSocket = srv.accept();
// Client connected
startReadingAndWritingData(clientSocket);
}
/**
* Starts reader and writer threads.
*
* @param socket
* client socket
* @throws IOException
*/
private void startReadingAndWritingData(Socket socket) throws IOException {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(
socket.getInputStream()));
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
ReaderThread reader = new ReaderThread(ois);
WriterThread writer = new WriterThread(oos);
reader.start();
writer.start();
}
}
class WriterThread extends Thread {
private ObjectOutputStream oos;
/**
* Constructor.
*
* @param oos
*/
WriterThread(ObjectOutputStream oos) {
super();
this.oos = oos;
}
public void run() {
while (true) {
try {
String output = getNextOutput();
oos.writeObject(output);
Thread.sleep(1000); // Wait before sending next String
} catch (Exception e) {
/*
* Socket IO or serialization error
*/
e.printStackTrace();
break;
}
}
}
/**
* Get output String from somewhere eg. file.
*
* @return output
*/
private String getNextOutput() {
// TODO get output String from somewhere
}
}
/**
* Reader thread.
*
*/
class ReaderThread extends Thread {
private ObjectInputStream ois;
/**
* Constructor.
*
* @param ois
*/
ReaderThread(ObjectInputStream ois) {
super();
this.ois = ois;
}
public void run() {
while (true) {
try {
String input = (String) ois.readObject();
handleInput(input);
} catch (Exception e) {
/*
* Socket IO or deserialization error
*/
e.printStackTrace();
break;
}
}
}
/**
* Handle received input String.
*
* @param input
*/
private void handleInput(String input) {
// TODO handle input
}
}
答案 1 :(得分:0)
您需要两个连接,一个连接客户端,另一个连接服务器,或者您需要使用非阻塞连接,即NIO。