簡體   English   中英

數據包因網絡損壞

[英]Packets getting corrupted with netty

我正在用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);
}

我完全看不到我的數據包在哪里弄亂了。 在我正確傳輸和解碼它們時,對我來說似乎如此。 如果需要更多相關代碼,請留言。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM