I'm making a secure VOIP program in java, and I chose to use netty to simply my networking. 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. Right now there are only two packets, 50 for chat and 51 for a voice sample. 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
@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 :
# 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.