数据包被netty破坏了

时间:2015-02-13 03:23:01

标签: java networking netty voip packet

我正在用java制作一个安全的VOIP程序,我选择使用netty来简化我的网络。到目前为止,它实际上是该项目最大的时间成本.. VOIP在localhost上工作正常,但是当我去网络上的另一台计算机时,会发生一些奇怪的事情。现在只有两个数据包,50个用于聊天,51个用于语音样本。该程序可以运行几帧,然后我收到随机数据包号码和无效的大小。我不确定是什么导致了这个......

以下是传输数据包的类:

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;
    }
}

以下是数据包的解码位置:

@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;
    }
}

在我的ChannelInboundHandlerAdapter中调用它的地方

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

我看不出我的数据包在哪里搞砸了。它在我看来,因为我正在正确地传输和解码它们。如果需要更多相关代码,请留言。

1 个答案:

答案 0 :(得分:0)

我建议您在(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

或者下载并安装以下工具以检查状态  http://nchc.dl.sourceforge.net/project/nicstat/nicstat-1.92.tar.gz