我正在尝试构建一个程序,该程序能够在一个端口上连接到客户端的TCP服务器,同时还能够在其他端口上传递和侦听UDP数据包中的数据。
我充当TCP服务器并从TCP客户端获取命令,然后通过UDP将它们传递给其他设备。我还必须在UDP中侦听来自设备的响应,然后将它们传递回TCP客户端。我一直试图通过创建2个线程来实现这一点,一个用于连接到TCP客户端,同时在另一个线程上使用不同的端口持续监听UDP数据包。
从TCP客户端获取数据并将其发送到UDP设备时,它可以正常工作。 但是当我尝试从UDP设备收集结果并将它们发送回TCP客户端时。该程序无法立即发回数据。它等待TCP客户端发送数据。这意味着我有一个填充结果的向量队列,但我必须等待TCP客户端与我通信才能发回结果。
当向量中有数据时,有没有办法通知TCP线程发送数据?
public class TCPUDP2 {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
final protected static int udplistenport=1002;
final protected static int tcplistenport=1001;
private static Vector<String> udprecv=new Vector(); //UDP result queue
public static String bytesToHex(byte[] bytes) { //convert byte[] to string
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexStringToByteArray(String s) { //Convert string to byte[]
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static void main(String[] args) throws IOException {
Thread udplisten = new Thread() {
public void run() {
try {
DatagramSocket dsocket = new DatagramSocket(udplistenport);
System.out.println("UDP Listening");
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
byte[] newbuffer= new byte[packet.getLength()];
for(int i =0;i<packet.getLength();i++)
newbuffer[i]=buffer[i];
System.out.println(" UDP from "+packet.getAddress()+" "+bytesToHex(newbuffer)+" "+new java.util.Date());
udprecv.add(bytesToHex(newbuffer));
System.out.println(udprecv);
//String msg = new String(buffer, 0, packet.getLength());
//System.out.println(msg);
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
}
};
udplisten.start();
Thread tcpplisten = new Thread() {
public void run() {
System.out.println("TCP Listening");
ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
try{
ServerSocket ss = new ServerSocket(tcplistenport); // establish tcp server
for(;;) {
Socket sc = ss.accept();
System.out.println(sc.getLocalSocketAddress().toString()+" "+new java.util.Date());
OutputStream os = sc.getOutputStream();
InputStream in = sc.getInputStream();
int len=0;
byte[] buf=new byte[2048];
while( !(udprecv.isEmpty())){
String first=udprecv.firstElement();
byte[] tcpsend=hexStringToByteArray(first);
os.write(tcpsend);//Send to Client
os.flush();
System.out.println("Sent by TCP "+bytesToHex(tcpsend)+" "+new java.util.Date());
udprecv.removeElementAt(0);
}
while ((len = in.read(buf))>=0)
{
serverinput.write(buf, 0, len);
System.out.println("From TCP "+bytesToHex(serverinput.toByteArray())+" "+sc.getLocalSocketAddress().toString());
MessageCreator cannonball=new MessageCreator("02","003F", bytesToHex(serverinput.toByteArray()));
byte[] cmd= cannonball.create();
String udpip="127.0.0.1";
int udpport=8888;
UDPSender cannon =new UDPSender(udpip,udpport,cmd);
System.out.println("Sent by UDP "+bytesToHex(cmd)+" "+udpip+":"+udpport);
cannon.fire();
serverinput.reset();
while( !(udprecv.isEmpty())){
String first=udprecv.firstElement();
byte[] tcpsend=hexStringToByteArray(first);
os.write(tcpsend);//Send to Client
os.flush();
System.out.println("Sent by TCP "+bytesToHex(tcpsend));
udprecv.removeElementAt(0);
}
}
// os.close(); // Close stream
// sc.close(); // Shutdown TCP server
}
}catch(Exception e){
System.err.println(e);
}
}
};
tcpplisten.start();
}
}