Java:通过TCP发送/接收数据,从UDP接收图像



我正在使用来自c#的代码,我试图在java中还原它。基本上在第一步中,我通过TCP发送和接收一些数据命令到IP摄像机(这是完成的),在第二步中,我必须通过UDP接收图像。问题是第二步:从UDP接收图像。我试了互联网上的每一个代码,但没有一个有效。

c#的原始代码是这样的:

* c#中服务器连接的示例

Socket _ServerUdp = null; 
Socket _ServerTcpIp = null; 
IPEndPoint _EndpointUdp = null; 
TcpClient _ClientUdp; 
/// <summary> 
/// Connect the socket 
/// <summary> 
public bool Connect(string IpAddr) 
{ 
if (!_VideoPortConnect(IpAddr, 8501)) 
return false; 
if (!_CommandPortConnect(IpAddr, 8500)) 
return false; 
return true; 
} 
/// <summary> 
/// Udp Connect 
/// </summary> 
bool _VideoPortConnect(string IpAddr, int VideoPort) 
{ 
try 
{ 
_ServerUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
_EndpointUdp = new IPEndPoint(IPAddress.Parse(IpAddr), VideoPort); 
_ClientUdp = new TcpClient(); 
_ClientUdp.Connect(_EndpointUdp); 
return true; 
} 
catch { return false; } 
} 
/// <summary> 
/// Tcp Ip Connect 
/// </summary> 
bool _CommandPortConnect(string IpAddr, int CommandPort) 
{ 
_ServerTcpIp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
IPEndPoint _RemoteEndPoint = new IPEndPoint(IPAddress.Parse(IpAddr), CommandPort); 
_ServerTcpIp.ReceiveTimeout = 5000; 
_ServerTcpIp.SendTimeout = 5000; 
try 
{ 
_ServerTcpIp.Connect(_RemoteEndPoint); 
return true; 
} 
catch { return false; } 
*}* 

发送命令和接收图像的示例该命令将通过TCP/IP发送。

byte[] DataReceived = new byte[20000];

/// <summary> 
/// Send Command Bar Code Reader 
/// </summary> 
/// <returns></returns> 
bool SendCommand() 
{ 
byte[] _Send = new byte[3]; 
_Send[0]= 20;    //CMD BAR CODE READER 
_Send[1] = 2;    //EXT CMD GET DATA 
_Send[2] = 2;    //SEND IMAGE 
if (!SendEth(_Send, 3)) 
return false; 
int _Ndati = ReadEth(); 
if (_Ndati == -1) 
return false; 
if (DataReceived[4] != 20) 
return false; 
GetImage(); 
} 
/// <summary> 
/// Send Data 
/// </summary> 
/// <param name="_Val"></param> 
/// <param name="_Len"></param> 
bool SendEth(byte[] _Val, Int32 _Len) 
{ 
try 
{ 
byte[] _Send = new byte[_Len + 4]; 
int _Value = _Len; 
Array.Copy(_Val, 0, _Send, 4, _Len); 
_Send[3] = (byte)_Value; 
_Value <<= 8; 
_Send[2] = (byte)_Value; 
_Value <<= 8; 
_Send[1] = (byte)_Value; 
_Value <<= 8; 
_Send[0] = (byte)_Value; 
_ServerTcpIp.Send(_Send, _Len + 4, SocketFlags.None); 
} 
catch { return false; } 
return true; 
}
/// <summary> 
/// Read Data from Ethernet 
/// </summary> 
/// <param name="LenDati"></param> 
/// <returns>array dati</returns> 
internal int ReadEth() 
{ 
int _Ndati; 
try 
{ 
_Ndati = _ServerTcpIp.Receive(DataReceived, SocketFlags.None); 
DataPointer =0; 
int _Len = GetInt(); 
if(_Len!=_Ndati-4) 
{ 
int _Diff = (_Ndati - 4) - _Len; 
byte[] BuffRx = new byte[_Diff]; 
int _NrDati = 0; 
int _PuntRx = _Ndati; 
while (_PuntRx < _Diff) 
{ 
_NrDati = _ServerTcpIp.Receive(BuffRx, _Diff, SocketFlags.None); 
for (int n = 0; n < _NrDati; n++) 
DataReceived[_PuntRx++] = BuffRx[n]; 
} 
} 
return _Ndati; 
} 
catch { return -1; } 
} 
/// <summary> 
/// Get Image 
/// </summary> 
/// <returns></returns> 
internal ImageSource GetImage() 
{ 
//get image 
NetworkStream _Stream = _ClientUdp.GetStream(); 
byte[] _Data = (byte[])_Formatter.Deserialize(_Stream); 
MemoryStream _ImgStream = new MemoryStream(_Data); 
ImageSource _Image = BitmapFrame.Create(_ImgStream, BitmapCreateOptions.None, 
BitmapCacheOption.OnLoad); 
return _Image; 
} 

JAVA代码:

公共类TFinale {

static byte[] DataReceived = new byte[20000];
public static void main(String[] args) throws Exception {
String[] args8500 = new String[2];
args8500[0] = "10.0.0.123";
args8500[1] = "8500";
int port8500 = Integer.parseInt(args8500[1]);
String[] args8501 = new String[2];
args8501[0] = "10.0.0.123";
args8501[1] = "8501";
int port8501 = Integer.parseInt(args8501[1]);
Socket clientSocket8501 = TFinale.createSocket(args8501[0], port8501);
if (clientSocket8501 == null) {
System.err.println("Unable to create socket to " + args8501[0] + ":" + port8501 + ".");
System.exit(1);
}
Socket clientSocket8500 = TFinale.createSocket(args8500[0], port8500);
if (clientSocket8500 == null) {
System.err.println("Unable to create socket to " + args8500[0] + ":" + port8500 + ".");
System.exit(1);
}
byte[] _Send = new byte[3];
_Send[0] = 22;    //CMD BAR CODE READER 
_Send[1] = 2;    //EXT CMD GET DATA 
_Send[2] = 2;    //SEND IMAGE
int _Len1 = 3;
byte[] SendEth1 = SendEth(_Send, _Len1);
DataOutputStream serverOut = null;
try {
serverOut = new DataOutputStream(clientSocket8500.getOutputStream());
serverOut.write(SendEth1, 0, 7);
System.out.println("serverOut writed!");
} catch (IOException ex) {
Logger.getLogger(TFinale.class.getName()).log(Level.SEVERE, null, ex);
}
DataInputStream in = null;
try {
byte[] messageByte = new byte[1000];
boolean end = false;
String dataString = "";
in = new DataInputStream(clientSocket8500.getInputStream());
int bytesRead = 0;
int f = 0;
//System.out.println("Please type 8500 clientSocket......");
for (f = 0; f < 7; f++) {
messageByte[f] = in.readByte();
System.out.println("messageByte 8500[" + f + "] " + messageByte[f]);
}
ByteBuffer byteBuffer = ByteBuffer.wrap(messageByte, 0, f);
int bytesToRead = byteBuffer.getShort();
System.out.println("About to read " + bytesToRead + " octets");
} catch (IOException ex) {
Logger.getLogger(TCPClient1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(TCPClient1.class.getName()).log(Level.SEVERE, null, ex);
}
}
timeDelay(1000);
try {
serverOut.close();
System.out.println("serverOut closed!");
in.close();
System.out.println("in closed!");
clientSocket8501.close();
System.out.println("clientSocket8501 closed!");
clientSocket8500.close();
System.out.println("clientSocket8500 closed!");
} catch (IOException ex) {
System.out.println("error:" + ex);
}
}
private static Socket createSocket(final String hostname, final int port) {
try {
Socket clientSocket = new Socket(hostname, port);
System.out.println(hostname + ":" + port + " socket created!");
return clientSocket;
} catch (UnknownHostException e) {
System.err.println(" " + hostname + " cannot be resolved as a network host.");
return null;
} catch (IOException e) {
System.err
.println("An exception occurred while communicating with the TCPServer: "
+ e.getMessage());
e.printStackTrace();
return null;
}
}

private static void timeDelay(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
}
}
private static byte[] SendEth(byte[] _Val, int _Len) {
byte[] _Send = new byte[_Len + 4];
int _Value = _Len;
System.arraycopy(_Val, 0, _Send, 4, _Len);
//Array.Copy(_Val, 0, _Send, 4, _Len); 
_Send[3] = (byte) _Value;
_Value <<= 8;
_Send[2] = (byte) _Value;
_Value <<= 8;
_Send[1] = (byte) _Value;
_Value <<= 8;
_Send[0] = (byte) _Value;
return _Send;
}
}

谢谢大家的帮助!

@Federico111,您应该使用java.net.DatagramSocket而不是Socket来使用UDP。

最新更新