我正在尝试构建一个服务器客户端应用程序。
客户机可以连接到服务器并正确注册。当客户机将数据写入连接客户机和服务器的通道时,服务器无法读取任何数据。selectorkey isreadable触发负责存储输入消息的方法。但它总是空的。知道为什么吗?
客户:
package Client;
import com.google.gson.GsonBuilder;
import routing.RoutingEntry;
import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.*;
public class Client {
private final String clientName;
SocketChannel connection;
ByteBuffer readBuffer;
ByteBuffer writeBuffer;
int serverPort;
Selector selector;
Set<RoutingEntry> routingTable = new HashSet<>();
public Client(int serverPort, String clientName) {
this.clientName = clientName;
this.serverPort = serverPort;
this.readBuffer = ByteBuffer.allocate(4096);
this.writeBuffer = ByteBuffer.allocate(4096);
}
public static void main(String[] args) {
Client client = new Client(5001, "Client_1");
client.connect();
while (true) client.sendMessage();
}
public void connect() {
try {
connection = SocketChannel.open(new InetSocketAddress(InetAddress.getLocalHost(), serverPort));
System.out.println(connection);
selector = Selector.open();
connection.configureBlocking(false);
connection.register(selector, SelectionKey.OP_READ);
routingTable.add(new RoutingEntry(InetAddress.getLocalHost().getHostName(), connection.socket().getLocalPort(), clientName, 0, null));
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(routingTable));
while (!connection.finishConnect()) {
System.out.println("Connecting...");
Thread.sleep(1000);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public void sendMessage() {
System.out.print("Message to send: ");
Scanner sc = new Scanner(System.in);
String msg = sc.nextLine().trim();
try {
writeBuffer.put(msg.getBytes());
connection.write(writeBuffer);
writeBuffer.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器:
package server;
import com.google.gson.GsonBuilder;
import routing.RoutingEntry;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
public class Server {
LinkedBlockingQueue<String> incomingMessages = new LinkedBlockingQueue<>();
Set<RoutingEntry> routingTable;
int serverPort;
String serverIP;
String serverName;
Selector selector;
ServerSocketChannel ssc;
ByteBuffer buffer = ByteBuffer.allocate(4096);
public Server(int serverPort, String serverName) {
this.serverPort = serverPort;
this.serverName = serverName;
}
public static void main(String[] args) {
Server server = new Server(5001, "Server_1");
server.run();
}
public void run() {
initRoutingTable();
try {
selector = Selector.open();
ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(serverIP, serverPort));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
System.out.println("In loop");
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iter = selectedKeys.iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
if (key.isAcceptable()) {
registerNewClient();
}
if (key.isReadable()) {
storeMessage(key);
}
iter.remove();
}
}
} catch (IOException ignore) {}
}
public void storeMessage(SelectionKey key) {
// read message
System.out.println("in storeMessage");
try {
SocketChannel client = (SocketChannel) key.channel();
int count = client.read(buffer);
System.out.println(count);
if (count == -1)
{
client.close();
return;
}
String msg = new String(buffer.array(), 0, buffer.limit());
System.out.println("Message: " + msg);
incomingMessages.put(msg);
buffer.clear();
} catch (IOException e) {
key.cancel();
System.out.println("Lost connection to client.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void registerNewClient() {
// establish connection to client
try {
SocketChannel client = ssc.accept();
System.out.println("new client connected: " + client.getRemoteAddress().toString().replace("/", ""));
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
System.out.println("connection fully done");
} catch (IOException e) {
e.printStackTrace();
}
}
private void initRoutingTable() {
routingTable = new HashSet<>();
try {
serverIP = InetAddress.getLocalHost().getHostAddress();
routingTable.add(new RoutingEntry(serverIP, serverPort, serverName, 0, null));
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(routingTable));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
输出 storeMessage()
:“消息:”
暂无答案!
目前还没有任何答案,快来回答吧!