简体   繁体   English

数据包因网络损坏

[英]Packets getting corrupted with netty

I'm making a secure VOIP program in java, and I chose to use netty to simply my networking. 我正在用Java开发一个安全的VOIP程序,我选择使用netty来简单地进行网络连接。 So far it's actually been the biggest cost of time for the project.. The VOIP works fine on localhost, but when I go to another computer on the network, something strange happens. 到目前为止,这实际上是该项目花费的最大时间。.VOIP在localhost上可以正常工作,但是当我转到网络上的另一台计算机时,会发生一些奇怪的事情。 Right now there are only two packets, 50 for chat and 51 for a voice sample. 目前只有两个数据包,其中50个用于聊天,而51个用于语音样本。 The program runs fine for a few frames, then I receive random packet numbers, and invalid sizes. 该程序可以正常运行几帧,然后我收到随机的数据包编号和无效的大小。 I'm not sure what is causing this.. 我不确定是什么原因造成的。

Here is the class that transmits the packets: 这是传输数据包的类:

package com.io;

import com.gui.VoiceCallFrame;
import com.net.Session;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

/**
 * @author Colby
 */
public class VoiceTransmitHandler implements VoiceIOHandler {

    public VoiceTransmitHandler(String name,  Session remote) {
        this.remote = remote;

        VoiceCallFrame frame = new VoiceCallFrame(name, this);
        frame.setVisible(true);
    }
    private Session remote;
    private boolean running;

    public void start() {
        running = true;

        new Thread() {

            @Override
            public void run() {
                try {
                    AudioFormat format = new AudioFormat(16000.0f, 16, 1, true, true);
                    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

                    TargetDataLine microphone = (TargetDataLine) AudioSystem.getLine(info);
                    try {
                        microphone.open(format);
                        microphone.start();

                        byte[] buf = new byte[4000];
                        do {
                            int len = microphone.read(buf, 0, buf.length);

                            ByteBuf packet = ByteBufAllocator.DEFAULT.buffer();
                            packet.writeByte(51);
                            packet.writeShort(len);
                            packet.writeBytes(buf, 0, len);
                            System.out.println("Send: " + packet.readableBytes());
                            remote.writeTCP(packet);

                        } while (running);

                    } finally {
                        microphone.close();
                    }
                } catch (LineUnavailableException e) {
                    running = false;
                    e.printStackTrace();
                }

            }
        }.start();
    }

    public void stop() {
        running = false;
    }
}

Here is where the packets are decoded: 这是数据包解码的位置:

@Override
public void readTCP(ByteBuf msg) {
    if (opcode == -1) {
        if (msg.readableBytes() < 3) {
            return;
        }
        opcode = msg.readUnsignedByte();
        length = msg.readUnsignedShort();
    }

    if (msg.readableBytes() < length) {
        return;
    }

    byte[] data = new byte[length];
    msg.readBytes(data);
    try {
        System.out.println("Packet received " + opcode + ":" + length);

        switch(opcode) {

            case 51://Voice received
                if(vrh == null) {
                    vrh = new VoiceReceiveHandler(host.getHostAddress());
                    vrh.start();
                }
                vrh.playLater(data);
                break;
        }

    } finally {
        opcode = length = -1;
    }
}

Where that is called from in my ChannelInboundHandlerAdapter 在我的ChannelInboundHandlerAdapter中调用的位置

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    session.readTCP((ByteBuf) msg);
}

I don't see where my packets could be getting messed up at all. 我完全看不到我的数据包在哪里弄乱了。 It looks to me as I am transmitting and decoding them correctly. 在我正确传输和解码它们时,对我来说似乎如此。 If there is any more relevant code needed just leave a message. 如果需要更多相关代码,请留言。

I recommend check errors on your (nic) network interface : 我建议您在(nic)网络接口上检查错误:

# netstat -i
Kernel Interface table
Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   0 199549124      0      0      0 153882468      0      0      0 BMRU
eth1       1500   0 138357627      0    630      0 151312724      0      0      0 BMRU
lo        16436   0        0      0      0      0        0      0      0      0 LRU

Or else download and install the following tool to check the status http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz 否则,下载并安装以下工具以检查状态http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM