我正在使用套接字连接从Android手机向Netty服务器发送一些字节。当Netty服务器获得一些字节时,它将使用DataInputStream
.readUTF
从正在读取的服务器端发送PONG消息
这是我的服务器处理程序代码
package com.nearbuy.fbb.netty.server.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nearbuy.fbb.netty.server.models.MetaDetails;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
public class ServerHandler extends ChannelInboundHandlerAdapter {
private static final String CONNECTION_CLOSE_COMMAND = "image_streaming_close";
private String imageFile = "";
private static HashMap<String, byte[]> fileBytesMap = new HashMap<>();
private static HashMap<String, Integer[]> fileWHDetailsMap = new HashMap<>();
private static HashMap<String, String> fileNameMap = new HashMap<>();
private boolean isMetaReceived = false;
private int PROCESS_ID_LENGTH = "PROCESS_ID".length();
private MetaDetails metaDetails = new MetaDetails();
private static final String META_DATA_ACKNOWLEDGE = "META_DATA_RECEIVING";
private static final String CLOSE_CONNECTION = "CLOSE_CONNECTION";
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf inBuffer = (ByteBuf) msg;
byte[] packetBytes = new byte[inBuffer.readableBytes()];
inBuffer.readBytes(packetBytes);
// some logics with bits
writePongBackToClient(ctx);
}
private void writePongBackToClient(ChannelHandlerContext ctx){
ChannelFuture cf = ctx.write(Unpooled.copiedBuffer("PONG", CharsetUtil.UTF_8));
ctx.flush();
if (!cf.isSuccess()) {
System.out.println("Send failed: " + cf.cause());
}
System.out.println(cf.isDone());
}
}
在Android方面,我与具有上述处理程序的Bootstrapserver建立了连接。它从Android获取字节,但是当我将字节写回客户端时,Datainputstream无法获取字节。 这是客户端
private void createSocketConnection(){
try{
socket = new Socket(IP_ADDRESS, 9999);
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new DataInputStream(socket.getInputStream());
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean writeToSocketStream(byte [] byteArray) throws InterruptedException {
try {
Log.i("Writing bytes", "New byte came of Byte length is "+ byteArray.length);
outputStream.write(byteArray);
outputStream.flush();
String pong = inputStream.readUTF();
Log.i("Response ", pong);
Log.i("Written bytes ", "Last bytes are not buffered, just written");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
但是它永远不会从Datainputstream中获取任何字符串,它只是挂在那一行:( 我无法确定实际问题在哪里。服务器不写消息还是客户端有问题。 如果您需要服务器启动器代码,那么我可以将其放在这里:) 我使用ChannelFuture从服务器端检查了服务器回写,但Android无法获取
ChannelFuture cf = ctx.write(Unpooled.copiedBuffer("PONG", CharsetUtil.UTF_8));
ctx.flush();
if (!cf.isSuccess()) {
System.out.println("Send failed: " + cf.cause());
}
System.out.println(cf.isDone());
它打印真实