使用java和socket进行vocie聊天,在局域网上连接时延迟
我正在使用socket和java编写语音聊天代码,两个客户端通话成功,但延迟1-2秒。我不知道为什么。导致网络速度或我的代码 线程播放器使用java和socket进行vocie聊天,在局域网上连接时延迟,java,sockets,delay,Java,Sockets,Delay,我正在使用socket和java编写语音聊天代码,两个客户端通话成功,但延迟1-2秒。我不知道为什么。导致网络速度或我的代码 线程播放器 public class player extends Thread{ public DataInputStream din = null; public SourceDataLine audio_out; byte byte_buff[] = new byte[256]; @Override public void run(){
public class player extends Thread{
public DataInputStream din = null;
public SourceDataLine audio_out;
byte byte_buff[] = new byte[256];
@Override
public void run(){
try {
while(din.read(byte_buff)!= -1){
audio_out.write(byte_buff, 0, byte_buff.length);
}
audio_out.drain();
audio_out.close();
} catch (IOException ex) {
Logger.getLogger(player.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
din.close();
} catch (IOException ex) {
Logger.getLogger(player.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
线程记录器
public class recorder extends Thread{
public DataOutputStream dout = null;
public TargetDataLine audio_in = null;
byte byte_buff[] = new byte[256];
@Override
public void run(){
try {
while(true){
int data = audio_in.read(byte_buff, 0, byte_buff.length);
//audio_out.write(byte_buff, 0, byte_buff.length);
System.out.println("sending");
dout.write(byte_buff);
dout.flush();
}
} catch (IOException ex) {
Logger.getLogger(recorder.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
dout.close();
} catch (IOException ex) {
Logger.getLogger(recorder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
螺纹转移
public class transfer extends Thread{
DataInputStream din;
DataOutputStream dout;
byte byte_buff[] = new byte[256];
@Override
public void run(){
try {
while(din.read(byte_buff)!= -1){
dout.write(byte_buff, 0, byte_buff.length);
System.out.println("tranferring");
}
} catch (IOException ex) {
System.out.println("voice disconnect");
} finally {
try {
din.close();
} catch (IOException ex) {
Logger.getLogger(transfer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
运行没有问题,但延迟约1-2秒,两个客户端在LAN上连接。
请帮助我,还有java的实时协议RTP,我相信它就是为了解决这类问题而设计的。对于初学者来说,TCP可能有很大的抖动,这是它从不用于语音聊天的主要原因之一。你应该改用UDP。谢谢,我照你说的做了,成功非常感谢:)