夜月号角 2020-08-14 16:00 采纳率: 0%
浏览 534
已结题

nginx tcp代理后服务端能收到客户端消息,也能正常发小心,但客户端收不到服务端返回的消息

最近在用Java NIO做迁移文件,本地测试是可以的,但是本地连接测试环境,并使用Nginx转发时(本地连本地,用Nginx转发也不行),出现服务端能收能发,但是客户端收到的是null(如果是对象字符串,所有的属性都是null),调试了好久不知道怎么弄,请大神们帮我

Nginx配置如下:
stream {

#-------------transferfile------------------------
upstream transferfile {
    hash $remote_addr consistent;
    server 192.168.1.91:1234  max_fails=3 fail_timeout=30s;
}

server {
    listen 8091;
    proxy_connect_timeout 30s;
    proxy_timeout 600s;
    proxy_pass transferfile;
}

}

服务端发送信息:
private void responseData(SocketChannel socketChannel, String response) {
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
try {
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}

客户端接收信息
public String receiveData(SocketChannel socketChannel) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String response = "";
try {
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] bytes;
int count = 0;
while ((count = socketChannel.read(buffer)) >= 0) {
buffer.flip();
bytes = new byte[count];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
}

        bytes = baos.toByteArray();
        response = new String(bytes, "UTF-8");
    } finally {
        try {
            baos.close();
        } catch (Exception ex) {
        }

        if(socketChannel != null){
            socketChannel.close();
        }
    }


    return response;
}
  • 写回答

3条回答 默认 最新

  • threenewbee 2020-08-14 16:29
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值