我正在使用java中的VOIP应用程序。我尝试将我的代码从多个套接字修改为只有一个,我得到以下错误: run error
我有一个主要课程,我在哪里运行发送者和接收者。
提到的那一行是:
sending_socket.send(AP);
代码如下:
package DatagramSocket2Mitigated;
/*
* TextSender.java
*/
/**
*
* @author abj
*/
import CMPC3M06.AudioRecorder;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.LineUnavailableException;
import uk.ac.uea.cmp.voip.DatagramSocket2;
public class SenderThread implements Runnable {
DatagramSocket2 sending_socket;
AudioRecorder recorder;
private int headerSize;
private int pID;
private int bufferIndex;
private final int bufferSize;
DatagramPacket[] sendingBuffer;
public SenderThread(int bufferSize, int headerSize)
throws LineUnavailableException {
pID = 0;
bufferIndex = 0;
this.bufferSize = bufferSize;
this.headerSize = headerSize;
sendingBuffer = new DatagramPacket[bufferSize];
}
public void start() {
Thread thread = new Thread(this);
thread.start();
}
public void run() {
//***************************************************
//Port to send to
int PORT = 55555;
//IP ADDRESS to send to
InetAddress clientIP = null;
try {
clientIP = InetAddress.getByName("localhost"); //CHANGE localhost to IP or NAME of client machine
} catch (UnknownHostException e) {
System.out.println("ERROR: TextSender: Could not find client IP");
e.printStackTrace();
System.exit(0);
}
//***************************************************
//***************************************************
//Open a socket to send from
//We dont need to know its port number as we never send anything to it.
//We need the try and catch block to make sure no errors occur.
//DatagramSocket sending_socket;
try {
recorder = new AudioRecorder();
sending_socket = new DatagramSocket2();
} catch (SocketException e) {
System.out.println("ERROR: TextSender: Could not open UDP socket to send from.");
e.printStackTrace();
System.exit(0);
} catch (LineUnavailableException ex) {
Logger.getLogger(SenderThread.class.getName()).log(Level.SEVERE, null, ex);
}
//Main loop.
boolean running = true;
DatagramPacket packet;
byte[] block;
while (!Thread.interrupted()) {
try {
block = recorder.getBlock();
packet = addHeader(block);
sendingBuffer[interleave(bufferIndex)] = packet;
// Increment packet id and the buffer index
pID++;
bufferIndex++;
// Send packets if buffer is full
if (bufferIndex >= bufferSize) {
for (DatagramPacket ap : sendingBuffer) {
sending_socket.send(ap);
}
// Reset the buffer
sendingBuffer = new DatagramPacket[bufferSize];
bufferIndex = 0;
}
//Make a DatagramPacket from it, with client address and port number
//DatagramPacket packet = new DatagramPacket(block, block.length, clientIP, PORT);
//Send it
// sending_socket.send(packet);
} catch (IOException e) {
System.out.println("ERROR: TextSender: Some random IO error occured!");
e.printStackTrace();
}
}
recorder.close();
//Close the socket
sending_socket.close();
//***************************************************
}
/**
*
* @param audioBlock
* @return
* @throws IOException
*/
public DatagramPacket addHeader(byte[] audioBlock) throws IOException {
byte[] sendingBlock = new byte[audioBlock.length + headerSize];
sendingBlock[0] = (byte) this.pID;
sendingBlock[1] = (byte) (((this.pID) >> 8) & 0xFF);
sendingBlock[2] = (byte) (((this.pID) >> 16) & 0xFF);
sendingBlock[3] = (byte) (((this.pID) >> 24) & 0xFF);
// Add packetID to block
//sendingBlock[0] = (byte)pID;
System.arraycopy(audioBlock, 0, sendingBlock, headerSize, audioBlock.length);
// Create datagram packet
return new DatagramPacket(sendingBlock, sendingBlock.length);
}
public int interleave(int pNo) {
// size 2 interleaver
int size = 2;
return ((pNo % size) * size) + (pNo / size);
}
}
问题是当我测试多个套接字时代码有效。我只删除了一些确定套接字的if语句。