我对客户端服务器和 Java 编程都比较陌生。有一个任务要做,我坚持使用这个程序来用 java 编写一个 echo 客户端和服务器。我总是在这里找到我的答案,所以我再次求助于 stackoverflow。
这是服务器:
public class Server_select {
public static void main(String[] args) throws IOException {
int port = 4666;
String address = "localhost";
String channelserver = "Server Channel";
String channelclient = "Client Channel";
String typechannel = "Channel Type";
ArrayList<Server_sockets> socketlist = new ArrayList<Server_sockets>();
Iterator<Server_sockets> socketiterator = socketlist.iterator();
ServerSocketChannel serverchannel = ServerSocketChannel.open();
serverchannel.bind(new InetSocketAddress(address,port));
serverchannel.configureBlocking(false);
Selector selector = Selector.open();
SelectionKey newconnectionkey = serverchannel.register(selector, SelectionKey.OP_ACCEPT);
socketlist.add(new Server_sockets(serverchannel, newconnectionkey, channelserver));
ArrayList<SelectionKey> selectedkeys = new ArrayList<SelectionKey>();
Iterator<SelectionKey> keyiterator = selectedkeys.iterator();
ByteBuffer buffer = ByteBuffer.allocate(4096);
StringBuffer message = null;
int count;
for(;;){
System.out.println("Entering the infinite for loop");
if(selector.select() == 0){
continue;
}
selectedkeys = (ArrayList<SelectionKey>) selector.selectedKeys();
while(keyiterator.hasNext()){
SelectionKey tempkey = keyiterator.next();
while(socketiterator.hasNext()){
if(socketiterator.next().key.equals(tempkey)){
if(socketiterator.next().channeltype == channelserver){
SocketChannel clientchannel = socketiterator.next().serverchannel.accept();
SelectionKey clientkey = clientchannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE);
socketlist.add(new Server_sockets(clientchannel,clientkey,channelclient));
System.out.println("Client connection established");
}
if(socketiterator.next().channeltype == channelclient){
if(socketiterator.next().key.isReadable()){
buffer.clear();
message = new StringBuffer("");
while((count = socketiterator.next().clientchannel.read(buffer))>0){
buffer.flip();
message.append(Charset.defaultCharset().decode(buffer));
}
System.out.println("Server here " + message);
buffer.clear();
while(!(socketiterator.next().key.isWritable())){
buffer.wrap(message.toString().getBytes());
while(buffer.hasRemaining()){
socketiterator.next().clientchannel.write(buffer);
}
buffer.clear();
}
}
}
}
}
}
}
}
}
这是客户:
public class Client_select {
SocketChannel clientchannel;
int port = 4666;
String address = "localhost";
String message = "Hey there!";
Selector selector = null;
SelectionKey key = null;
Client_select() throws IOException{
this.clientchannel = SocketChannel.open();
this.clientchannel.configureBlocking(false);
this.clientchannel.connect(new InetSocketAddress(this.address,this.port));
this.selector = Selector.open();
this.key = this.clientchannel.register(this.selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE);
}
public void write_to_socket() throws IOException{
ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
while(buffer.hasRemaining()){
this.clientchannel.write(buffer);
}
}
public void read_from_socket() throws IOException{
ByteBuffer buffer = ByteBuffer.allocate(4096);
StringBuffer message = new StringBuffer("");
int count=0;
while((count = this.clientchannel.read(buffer)) > 0){
buffer.flip();
message.append(Charset.defaultCharset().decode(buffer));
}
System.out.println("Read From Socket" + message);
}
public static void main(String[] args) throws Exception {
Client_select obj = new Client_select();
ArrayList<SelectionKey> keylist = new ArrayList<SelectionKey>();
Iterator<SelectionKey> iterator = keylist.iterator();
for(;;){
System.out.println("Entering Infinite loop Client");
if(obj.selector.select() == 0){
continue;
}
keylist = (ArrayList<SelectionKey>) obj.selector.selectedKeys();
while(iterator.hasNext()){
if(iterator.next().isWritable()){
obj.write_to_socket();
}
if(iterator.next().isReadable()){
obj.read_from_socket();
}
}
}
}
}
错误是服务器因此错误而终止:线程“main”中的异常 java.lang.ClassCastException: sun.nio.ch.Util$2 cannot be cast to java.util.ArrayList at Server_select.main(Server_select.爪哇:40)
我只是想知道其余的都好吗?我想澄清我对此的一些其他疑问。提前致谢 :)