I'm working in a code that come from C# and I'm trying to traduce it in java.
Basically in first step I send and receive some data-command to an IP camera through TCP (this is done) and in second step I have to receive the image through UDP. The problem is with second step: receive image from UDP.
I try every code in internet but no one work.
The originally code in C# is this:
*Example to server connection in 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; }
*}*
Example for send command and receive image
The command will be send by 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;
}
My JAVA code:
public class 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;
}
}
Thanks everybody can help me!
#Federico111, you should use java.net.DatagramSocket instead of Socket to work with UDP.
Related
Alright so I am looking at this piece of code that is supposed to get an array of bytes which represents an image and send it piece by piece to a server. The server needs to be told when the image transmission is done, this ending message is "CLOSE". Everything works fine but unless I uncomment Thread.sleep the end message isn't sent. Also the delay needs to be quite big for some reason, 100 ms for example doesn't work. If anyone could provide an explanation for this behaviour I would be grateful since I don't have a very good understanding of java.
private class NetTask extends AsyncTask<Void, Void, Void>
{
private String ip;
private byte[] to_send;
public NetTask(String ip, byte[] to_send)
{
this.ip = ip;
this.to_send = to_send;
}
#Override
protected Void doInBackground(Void...params)
{
try {
Log.i(dTag, "" + to_send.length);
Socket sck = new Socket(ip, 1234);
DataOutputStream dOut = new DataOutputStream(sck.getOutputStream());
dOut.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(to_send.length).array());
Log.d(dTag, "" + to_send.length);
int x = 500;
int len = to_send.length;
for (int i = 0; i < len - x + 1; i += x)
dOut.write(Arrays.copyOfRange(to_send, i, i + x));
if (len % x != 0)
dOut.write(Arrays.copyOfRange(to_send, len - len % x, len));
/*try {
Thread.sleep(1000);
}
catch (Exception ex) {
Log.d(dTag, "thread sleep error");
}*/
dOut.write("CLOSE".getBytes());
dOut.flush();
dOut.close();
sck.close();
}
catch (IOException ex) {
Log.d(dTag, ex.getMessage());
}
return null;
}
}
The server is in c#, here is the code:
while (ok)
{
sck.Listen(1000);
Socket accepted = sck.Accept();
buffer = new byte[accepted.SendBufferSize];
int bytesRead = -1;
bool reading = true;
int im_size = -1;
int index = 0;
byte[] image = null;
while (reading)
{
bytesRead = accepted.Receive(buffer);
if (bytesRead == 5)
Console.WriteLine(bytesRead);
string strData = "YADA";
byte[] formatted = new byte[bytesRead];
if (bytesRead == 5)
{
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = buffer[i];
}
strData = Encoding.ASCII.GetString(formatted);
}
if (strData == "CLOSE")
{
Console.WriteLine("GOT CLOSE MESSAGE");
Image im = Image.FromStream(new MemoryStream(image));
im.Save(#"D:\im1.bmp");
}
else
{
if (im_size == -1)
{
im_size = BitConverter.ToInt32(buffer, 0);
image = new byte[im_size];
Console.WriteLine(im_size);
}
else
{
for (int i = 0; i < bytesRead && index < im_size; i++)
{
image[index++] = buffer[i];
}
}
}
}
accepted.Close();
}
I have developed a java socket, but it takes too many cpu and virtual memory usages.
Can you tell me what is the problem in my code?
private void listen() {
try {
serverSocket = new ServerSocket(port);
System.out.println("Server socket listening on port: " + port);
System.out.println("Waiting for connections...");
while(true) {
// accept the connection
Socket socket = serverSocket.accept();
socket.setSoTimeout(30000);
System.out.println("Got connection");
// start processing the connection
Thread connectionManager = new Thread(new Elevator(socket, socket.getInputStream()));//new Thread(new ConnectionManager(socket, odometer));
connectionManager.start();
}
} catch (IOException exc) {
System.out.println(Listener.class.getName() + ": " + exc.getMessage());
}
}
in Elevator class I have this:
public class Elevator implements Runnable
{
private String imei;
private Socket socket;
private InputStream is;
private PrintWriter out;
private OutputStream ds;
private int packetL;
private long timestamp;
dbElevatorManipulate dbElevator;
private String[] allCards = null;
private String[] insCards = null;
private String[] upddelCards = null;
private String[] config = null;
public Elevator(Socket socket, InputStream is) {
this.socket = socket;
this.is = is;
initializeOutputStream(socket);
}
private void initializeOutputStream(Socket socket) {
try {
ds = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
out = new PrintWriter(ds, true);
}
#Override
public void run(){
int codecL = 1;
int imeiL = 16;
String codecID = "";
String imeiFromBoard = "";
byte[] codecBuffer = new byte[codecL];
byte[] imeiBuffer = new byte[imeiL];
try{
// Read codec ID.
is.read(codecBuffer, 0, codecL);
codecID = byteToString(codecBuffer);
//System.out.println("Codec ID : " + codecID);
// Read imei.
is.read(imeiBuffer, 0, imeiL);
imeiFromBoard = byteToString(imeiBuffer);
if (codecID.equals("2")) {
byte[] crc = new byte[2];
is.read(crc);
byte[] cnnpacket = new byte[codecL + imeiL];
cnnpacket[0] = codecBuffer[0];
for (int i = 1; i < cnnpacket.length; i++) {
cnnpacket[i] = imeiBuffer[i-1];
}
if(DataLayer.checksum(cnnpacket, crc))
{
try {
ds.write(1);
ds.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
try {
ds.close();
is.close();
dbElevator.disconnect();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
}
catch(Exception ex)
{
System.out.println("Error: " + ex.getMessage());
}
imeiFromBoard = imeiFromBoard.substring(1, imeiFromBoard.length());
imei = imeiFromBoard;
System.out.println("got connection with imei:" + imei);
dbElevator = new dbElevatorManipulate(imei);
dbElevator.connect();
try{
while(true){
byte[] packet = new byte[1024];
int i = 0;
int byteread = is.read(packet);
if(byteread == -1) continue;
int cmdL = 1;
int dataLen = 2;
byte[] cmdBuffer = new byte[cmdL];
byte[] lengthBuffer = new byte[dataLen];
// Read and Print cmd.
cmdBuffer[0] = packet[0];
if(cmdBuffer[0] > 9 || cmdBuffer[0] < 0){
continue;
}
// Read and Print data length.
lengthBuffer[0] = packet[1];
lengthBuffer[1] = packet[2];
packetL = Integer.parseInt(DataLayer.byteToString(lengthBuffer));
// Reading a printing the packet .
byte[] packetBuffer = new byte[packetL];
int pcnt = 3;
while(packetL-- > 0)
{
packetBuffer[pcnt - 3] = packet[pcnt];
pcnt++;
}
String packetData = DataLayer.byteToString(packetBuffer);
// Reading and printing crc
int crcL = 2;
byte[] crcBuffer = new byte[crcL];
crcBuffer[0] = packet[pcnt];
crcBuffer[1] = packet[pcnt + 1];
if (DataLayer.checksum(packetBuffer, crcBuffer)) {
System.out.println("Packet: OK");
switch(cmdBuffer[0])
{
case 0:
int nrOfPackets = dbElevator.getNrOfTotalPackets();
int nCrc = DataLayer.crc16(new byte[] {(byte)nrOfPackets});
byte[] sendPacket = new byte[3];
sendPacket[0] = (byte)nrOfPackets;
sendPacket[1] = (byte)((nCrc >> 8) & 0xff); // hight byte of crc
sendPacket[2] = (byte)(nCrc & 0xff); // low byte of crc
ds.write(sendPacket);
ds.flush();
allCards = dbElevator.getTotalCards().split(",");
break;
case 1:
int PacketNo = Integer.parseInt(packetData);
if(allCards != null)
sendAllPacket(PacketNo);
break;
case 2:
int nrOfUpdPackets = dbElevator.getUpdDelCardsNo();
int updCrc = DataLayer.crc16(new byte[] {(byte)nrOfUpdPackets});
byte[] sendUpdPacket = new byte[3];
sendUpdPacket[0] = (byte)nrOfUpdPackets;
sendUpdPacket[1] = (byte)((updCrc >> 8) & 0xff); // hight byte of crc
sendUpdPacket[2] = (byte)(updCrc & 0xff); // low byte of crc
ds.write(sendUpdPacket);
ds.flush();
upddelCards = dbElevator.getUpdDelCards().split(",");
break;
case 3:
int updPacketNo = Integer.parseInt(packetData);
sendUpdDelPacket(updPacketNo);
break;
case 4:
int nrOfInsPackets = dbElevator.getInsertedCardsNo();
int insCrc = DataLayer.crc16(new byte[] {(byte)nrOfInsPackets});
byte[] sendInsPacket = new byte[3];
sendInsPacket[0] = (byte)nrOfInsPackets;
sendInsPacket[1] = (byte)((insCrc >> 8) & 0xff); // hight byte of crc
sendInsPacket[2] = (byte)(insCrc & 0xff); // low byte of crc
ds.write(sendInsPacket);
ds.flush();
insCards = dbElevator.getInsertedCards().split(",");
break;
case 5:
int insPacket = Integer.parseInt(packetData);
sendInsPacket(insPacket);
break;
case 6:
insertCheckInIntoDB(packetBuffer);
break;
case 7:
config = dbElevator.getConfig().split(",");
sendConfig(config);
break;
case 8: //log cards and close connection
try {
if(insCards != null && insCards.length > 2)
{
dbElevator.resetActionForIns(insCards);
dbElevator.LogSent_insCards(insCards);
}
if(upddelCards != null && upddelCards.length > 2)
{
dbElevator.resetActionForUpd(upddelCards);
dbElevator.LogSent_updCards(upddelCards);
}
if(allCards != null && allCards.length > 2)
{
dbElevator.resetActionForAll(allCards);
dbElevator.LogSent_allCards(allCards);
}
if(config != null && config.length > 0)
{
dbElevator.ConfigSent();
}
ds.close();
is.close();
dbElevator.disconnect();
System.out.println("database disconnected");
socket.close();
System.out.println("socket closed.");
return;
} catch (IOException e) {
e.printStackTrace();
}
break;
case 9:
int sendConfig = dbElevator.getSendConfig();
int sendConfigCRC = DataLayer.crc16(new byte[] {(byte)sendConfig});
byte[] sendConfigConfirm = new byte[3];
sendConfigConfirm[0] = (byte)sendConfig;
sendConfigConfirm[1] = (byte)((sendConfigCRC >> 8) & 0xff);
sendConfigConfirm[2] = (byte)(sendConfigCRC & 0xff);
ds.write(sendConfigConfirm);
ds.flush();
break;
}
}else{
//System.out.println("Packet: error");
ds.write(0x00);
ds.flush();
}
}
}catch(IOException e){
e.printStackTrace();
}
finally{
try {
ds.close();
System.out.println("ds closed.");
is.close();
System.out.println("is closed.");
dbElevator.disconnect();
System.out.println("db disconnected.");
socket.close();
System.out.println("socket closed.");
//break;
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private void sendConfig(String[] config) throws IOException {
byte[] packet = new byte[16*3];
for(int c = 0, i = 0; c<32; c++)
{
if(c<16){
int temp = Integer.parseInt(config[c],2);
packet[i++] = (byte)((temp >> 8) & 0xff);
packet[i++] = (byte)(temp & 0xff);
}
else
{
packet[i++] = (byte)(Integer.parseInt(config[c]) & 0xff);
}
}
int crc = DataLayer.crc16(packet);
byte[] txPacket = new byte[3*16 + 2];
for(int i = 0; i<16*3; i++)
{
txPacket[i] = packet[i];
}
txPacket[16*3] = (byte)((crc >> 8) & 0xff);
txPacket[16*3 + 1] = (byte)((crc) & 0xff);
ds.write(txPacket);
ds.flush();
}
private void insertCheckInIntoDB(byte[] packetData){
String values = "";
try{
for(int i = 0; i<packetData.length; i+=8)
{
byte[] siteCode = new byte[1];
siteCode[0] = packetData[i];
byte[] siteNo = new byte[2];
siteNo[0] = packetData[i + 1];
siteNo[1] = packetData[i + 2];
byte[] Floor = new byte[1];
Floor[0] = (byte)(packetData[i + 3] >> 4 & 0xf);
byte[] Valide = new byte[1];
Valide[0] = (byte)(packetData[i + 3] & 0xf);
byte[] timestamp = new byte[4];
timestamp[0] = packetData[i + 4];
timestamp[1] = packetData[i + 5];
timestamp[2] = packetData[i + 6];
timestamp[3] = packetData[i + 7];
if(Long.parseLong(DataLayer.byteToString(timestamp)) < 1410000000)
{
continue;
}
values += "(" + imei + ",";
values += DataLayer.byteToString(siteCode) + ",";
values += DataLayer.byteToString(siteNo) + ",";
values += DataLayer.byteToString(Floor) + ",";
values += DataLayer.byteToString(Valide) + ",";
values += DataLayer.byteToString(timestamp) + "),";
}
values = values.substring(0, values.length()-1);
if(dbElevator.insertCheckIn(values))
{
ds.write(1);
ds.flush();
return;
}
else
{
try {
ds.write(0);
ds.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch(Exception ex){
try {
ds.write(0);
ds.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendUpdDelPacket(int updPacketNo) throws IOException {
byte[] packet = new byte[38*6];
for(int i = 0, c = 38*4*updPacketNo; i<38*6; i+=6, c+=4)
{
if(c + 3 < upddelCards.length)
{
packet[i] = (byte)Integer.parseInt(upddelCards[c]);
//System.out.println(upddelCards[c+1]);
packet[i+1] = (byte)(Integer.parseInt(upddelCards[c+1]) >> 8 & 0xff);
packet[i+2] = (byte)(Integer.parseInt(upddelCards[c+1]) & 0xff);
packet[i+3] = (byte)(Integer.parseInt(upddelCards[c+2], 2) >> 8 & 0xff);
packet[i+4] = (byte)(Integer.parseInt(upddelCards[c+2], 2) & 0xff);
packet[i + 5] = (byte)Integer.parseInt(upddelCards[c + 3]);
}
else
{
packet[i] = 0;
packet[i+1] = 0;
packet[i+2] = 0;
packet[i+3] = 0;
packet[i+4] = 0;
packet[i+5] = 0;
}
}
int crc = DataLayer.crc16(packet);
byte[] txPacket = new byte[38*6 + 2];
for(int i = 0; i<38*6; i++)
{
txPacket[i] = packet[i];
}
txPacket[38*6] = (byte)((crc >> 8) & 0xff);
txPacket[38*6 + 1] = (byte)((crc) & 0xff);
ds.write(txPacket);
ds.flush();
}
private void sendInsPacket(int packetNo) throws IOException {
byte[] packet = new byte[46*5];
for(int i = 0, c = 46*3*packetNo; i< 46*5; i+=5, c+=3)
{
if(c + 2 < insCards.length)
{
packet[i] = (byte)Integer.parseInt(insCards[c]);
packet[i+1] = (byte)(Integer.parseInt(insCards[c+1]) >> 8 & 0xff);
packet[i+2] = (byte)(Integer.parseInt(insCards[c+1]) & 0xff);
packet[i+3] = (byte)(Integer.parseInt(insCards[c+2], 2) >> 8 & 0xff);
packet[i+4] = (byte)(Integer.parseInt(insCards[c+2], 2) & 0xff);
}
else
{
packet[i] = 0;
packet[i+1] = 0;
packet[i+2] = 0;
packet[i+3] = 0;
packet[i+4] = 0;
}
}
int crc = DataLayer.crc16(packet);
byte[] txPacket = new byte[46*5 + 2];
for(int i = 0; i<46*5; i++)
{
txPacket[i] = packet[i];
}
txPacket[46*5] = (byte)((crc >> 8) & 0xff);
txPacket[46*5 + 1] = (byte)((crc) & 0xff);
ds.write(txPacket);
ds.flush();
}
private void sendAllPacket(int packetNo) throws IOException {
byte[] packet = new byte[46*5];
for(int i = 0, c = 46*3*packetNo; i< 46*5; i+=5, c+=3)
{
if(c + 2 < allCards.length)
{
packet[i] = (byte)Integer.parseInt(allCards[c]);
packet[i+1] = (byte)(Integer.parseInt(allCards[c+1]) >> 8 & 0xff);
packet[i+2] = (byte)(Integer.parseInt(allCards[c+1]) & 0xff);
packet[i+3] = (byte)(Integer.parseInt(allCards[c+2], 2) >> 8 & 0xff);
packet[i+4] = (byte)(Integer.parseInt(allCards[c+2], 2) & 0xff);
}
else
{
packet[i] = 0;
packet[i+1] = 0;
packet[i+2] = 0;
packet[i+3] = 0;
packet[i+4] = 0;
}
}
int crc = DataLayer.crc16(packet);
byte[] txPacket = new byte[46*5 + 2];
for(int i = 0; i<46*5; i++)
{
txPacket[i] = packet[i];
}
txPacket[46*5] = (byte)((crc >> 8) & 0xff);
txPacket[46*5 + 1] = (byte)((crc) & 0xff);
ds.write(txPacket);
ds.flush();
}
private static String byteToString(byte[] buffer) {
StringBuilder s = new StringBuilder();
for (byte b : buffer) {
s.append((char) b);
}
return s.toString();
}
What can I do about this problem ?
Thanks in advance.
You appear to be doing a lot of small reads directly on a socket's input stream. You should get better performance if you wrap the input stream with a buffered input stream.
The use of buffered streams is discussed in the Oracle Java Tutorial.
The output side looks a bit better. You are assembling packets, and writing them in large(er) write calls. However, I'm still suspicious of that ... and the possibility that some of the flush calls are unnecessary.
As EJP points out, your I/O code is fragile because you don't take account of the possibility that the "other end" has closed the socket. This will cause read and equivalent calls to return without reading anything.
Note that the read methods return the number of bytes (or characters) that they have read, or -1 if they detect "end of stream". Your code is completely ignoring the return value.
It is possible that this is the cause of your performance problems; e.g. if a thread is repeatedly calling read on a socket that is in "end of stream" state.
Another possible problem with your code is that your listen method is creating a brand new Thread each time an accept() call succeeds.
If you have lots of clients connecting, there will be lots of threads, and this results in lots of resource usage.
If clients don't disconnect and/or the server doesn't notice that they have disconnected, then the resources will leak, and your server will never give them back.
If you are leaking threads, and those threads are sitting idle, then they are wasting memory. (A thread stack is typically 1Mb or so.) If they are not idle (e.g. because of a bug in your read code ... like not dealing with the "end of stream" condition properly) then you will be wasting CPU too.
#chanjaster suggested using an executor with a fixed sized thread pool. This might help in a couple of respects:
It prevents the application using an unbounded number of threads.
It recycles the threads after they are no longer active ... which reduces overheads.
However, if you have a resource leakage problem, a thread pool won't cure this. Indeed, what will probably happen is that new connections just freeze. (They go into the task queue waiting for a worker thread to pick them up. And that never happens.)
I have socket application that i use for communication with a device.
When i open socket i can read status outputs from the machine.
Machine sends some data which is separated by comma ',' and i need to parse only numbers.
The problem is when i parse the data i recieve numbers but i also recieve "empty" strings.
Here is my code:
void startListenForTCP(String ipaddress) {
Thread TCPListenerThread;
TCPListenerThread = new Thread(new Runnable() {
#Override
public void run() {
Boolean run = true;
String serverMessage = null;
InetAddress serverAddr = null;
BufferedWriter out = null;
int redni = 0;
try {
Socket clientSocket = new Socket(ipaddress, 7420);
try {
mc.pushNumbers("Connection initiated... waiting for outputs!"
+ "\n");
char[] buffer = new char[2];
int charsRead = 0;
out =
new BufferedWriter(new OutputStreamWriter(
clientSocket.getOutputStream()));
BufferedReader in =
new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
while ((charsRead = in.read(buffer)) != -1) {
String message = new String(buffer).substring(0, charsRead);
if (message.equals("I,")) {
mc.pushNumbers("\n");
} else {
String m = message;
m = m.replaceAll("[^\\d.]", "");
String stabilo = m;
int length = stabilo.length();
String result = "";
for (int i = 0; i < length; i++) {
Character character = stabilo.charAt(i);
if (Character.isDigit(character)) {
result += character;
}
}
System.out.println("Result:" + m);
}
}
} catch (UnknownHostException e) {
mc.pushNumbers("Unknown host..." + "\n");
} catch (IOException e) {
mc.pushNumbers("IO Error..." + "\n");
} finally {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
mc.pushNumbers("Connection refused by machine..." + "\n");
}
}
});
TCPListenerThread.start();
}
And the System.out.println(); returns this:
Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:26Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:Result:13
Result:Result:Result:Result:Result:Result:Result:Result:Result:
I just don't know why I can't parse only numbers, there is probably something that machine sends and it isn't parsed by m = m.replaceAll("[^\\d.]", "");
You're building your String incorrectly. It should be:
String message = new String(buffer, 0, bytesRead);
where 'bytesRead' is the count returned by the read() method. It's a byte count, not a char count.
I trying to write a code to stream from shoutcast server
and I use the below code and give me javax.sound.sampled.UnsupportedAudioFileException
how I can solve the exception
public static void streamSampledAudio(URL url)
throws IOException, UnsupportedAudioFileException,
LineUnavailableException
{
AudioInputStream ain = null; // We read audio data from here
SourceDataLine line = null; // And write it here.
try {
InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream( is );
ain=AudioSystem.getAudioInputStream(bis);
AudioFormat format = ain.getFormat( );
DataLine.Info info=new DataLine.Info(SourceDataLine.class,format);
if (!AudioSystem.isLineSupported(info)) {
AudioFormat pcm =
new AudioFormat(format.getSampleRate( ), 16,
format.getChannels( ), true, false);
ain = AudioSystem.getAudioInputStream(pcm, ain);
format = ain.getFormat( );
info = new DataLine.Info(SourceDataLine.class, format);
}
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
int framesize = format.getFrameSize( );
byte[ ] buffer = new byte[4 * 1024 * framesize]; // the buffer
int numbytes = 0; // how many bytes
boolean started = false;
for(;;) { // We'll exit the loop when we reach the end of stream
int bytesread=ain.read(buffer,numbytes,buffer.length-numbytes);
if (bytesread == -1) break;
numbytes += bytesread;
if (!started) {
line.start( );
started = true;
}
int bytestowrite = (numbytes/framesize)*framesize;
line.write(buffer, 0, bytestowrite);
int remaining = numbytes - bytestowrite;
if (remaining > 0)
System.arraycopy(buffer,bytestowrite,buffer,0,remaining);
numbytes = remaining;
}
line.drain( );
}
finally { // Always relinquish the resources we use
if (line != null) line.close( );
if (ain != null) ain.close( );
}
}
and give me an exception
Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio
input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at test.PlaySoundStream.streamSampledAudio(PlaySoundStream.java:40)
at test.PlaySoundStream.main(PlaySoundStream.java:21)
can help me to solve the exception
or tell me about away can stream by it from shoutcast
I try this code to download MP3 from shoutcast and then you can play sound
public class DownloadMP3 {
private boolean halt = false;
public static final String DEFAULT_HOST = "127.0.0.1";
protected String host = "url";
public static final int DEFAULT_PORT = 80;
protected int port = 8568;
public static final int DEFAULT_TOTAL_SIZE = 0;
protected long totalSize = 0L;
public static final int DEFAULT_CHUNK_SIZE = 0;
long chunkSize = 0L;
public static final String DEFAULT_OUTPUT_DIRECTORY = ".";
File outputDirectory = new File("D:\\");
public static void main(String[ ] args) throws Exception {
DownloadMP3 d = new DownloadMP3();
d.run();
}
public void run()
{
Socket localSocket = null;
PrintWriter localPrintWriter = null;
BufferedInputStream localBufferedInputStream = null;
FileOutputStream localFileOutputStream = null;
try
{
writeMessage("Opening connection to " + this.host + ":" + this.port);
localSocket = new Socket(this.host, this.port);
localPrintWriter = new PrintWriter(localSocket.getOutputStream(), true);
localBufferedInputStream = new BufferedInputStream(localSocket.getInputStream());
localPrintWriter.print("GET / HTTP/1.0\r\n\r\n");
localPrintWriter.flush();
byte[] arrayOfByte = new byte[1024];
long l1 = 0L;
long l2 = 0L;
int i = 1;
File localFile = null;
writeMessage("Host contacted, waiting for response...");
try
{
int k = 0;
int m;
writeMessage("Recieving Data....");
int j;
while ((j = localBufferedInputStream.read(arrayOfByte)) != -1) {
if ((localFileOutputStream == null) || ((this.chunkSize > 0L) && (l2 + j >= this.chunkSize))) {
m = findSync(arrayOfByte, 0, j);
if (m == -1) {
m = j;
}
if (localFileOutputStream != null)
{
localFileOutputStream.write(arrayOfByte, 0, m);
}
if (localFileOutputStream != null) {
localFileOutputStream.close();
}
while ((localFile = new File(this.outputDirectory, this.host + '-' + this.port + '-' + formatFileNum(i++) + ".mp3")).exists());
writeMessage("Saving to file: " + localFile);
localFileOutputStream = new FileOutputStream(localFile);
l2 = 0L;
localFileOutputStream.write(arrayOfByte, m, j - m);
l2 += j - m;
} else {
localFileOutputStream.write(arrayOfByte, 0, j);
l2 += j;
}
if ((this.totalSize > 0L) && (l1 >= this.totalSize)) {
writeMessage("Capture completed successfully.");
if (this.halt) {
writeMessage("Capture interruted.");
}
}
writeErrorMessage("Connection closed by host.");
return;
} catch (IOException localIOException2) {
if (this.halt)
writeMessage("Capture interruted.");
else {
writeErrorMessage(localIOException2.getMessage());
}
} finally {
if (localFileOutputStream != null)
localFileOutputStream.close();
}
}
catch (UnknownHostException localUnknownHostException) {
writeErrorMessage("Unknown host: " + this.host);
return;
} catch (IOException localIOException1) {
writeErrorMessage("Could not connect to " + this.host + " on port " + this.port);
}
finally {
if (localPrintWriter != null) {
localPrintWriter.close();
}
if (localBufferedInputStream != null)
try {
localBufferedInputStream.close();
}
catch (IOException localIOException3) {
}
if (localSocket != null)
try {
localSocket.close();
}
catch (IOException localIOException4)
{
}
}
}
private static int findSync(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
{
for (int i = paramInt1; i < paramInt2 - 1; i++) {
if (((paramArrayOfByte[i] & 0xFF) == 255) && ((paramArrayOfByte[(i + 1)] & 0xE0) == 224)) {
return i;
}
}
return -1;
}
private static String formatFileNum(int paramInt)
{
if (paramInt < 10)
return "00" + paramInt;
if (paramInt < 100) {
return "0" + paramInt;
}
return "" + paramInt;
}
protected void writeMessage(String paramString)
{
System.out.println(paramString);
}
protected void writeErrorMessage(String paramString)
{
System.err.println(paramString);
}
}
I am trying to send Data from the server to client 1 bit at a time. Client should respond back with an ACK whenever it received a bit from the server.
What is currently happening is that when client sends the initial requests to the server, data is getting passed to the server. When server is sending the required data to the client, the data is getting looped back to itself and client is left in an indefinite waiting hang.
I've attached the code below for client and server. Please have a look at it and advise on where I am going wrong.
CLIENT SIDE:
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
class UDPClient extends Thread implements Runnable
{
DatagramSocket clientSocket;
InetAddress IPAddress;
public static void main(String args[]) throws Exception
{
try
{
UDPClient t1 = new UDPClient();
Thread t = new Thread(t1);
t.start();
}
catch (Exception e)
{
System.out.println("Error!");
}
}
public UDPClient() throws Exception
{
this.clientSocket = new DatagramSocket();
this.IPAddress = InetAddress.getByName("Localhost");
clientSocket.setSoTimeout(5000);
}
public void run()
{
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String m ="1001";
String checksum = "1111";
String checksumSend = "";
String sentence="";
String sentence1="";
String dataRequired="";
try
{
dataRequired = dataRequired.concat(m+":Data");
sendData = dataRequired.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
checksumSend = checksumSend.concat(checksum+":checksum");
sendData = checksumSend.getBytes();
DatagramPacket sendPacket2 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket2);
Scanner in = new Scanner(System.in);
System.out.println("Enter the Window size: ");
int s = in.nextInt();
String WinSize="";
WinSize = WinSize.concat(s+":Windowsize");
sendData = WinSize.getBytes();
DatagramPacket sendPacket3 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket3);
String finished="Finished";
sendData = finished.getBytes();
DatagramPacket sendPacket6 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket6);
do
{
try
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println("I am getting executed");
clientSocket.receive(receivePacket); //After this step, nothing gets executed
sentence = new String(receivePacket.getData(),0,receivePacket.getLength());
System.out.println("Received from Server: " + sentence);
if(receivePacket != null)
sentence1 = "ACK";
sendData = sentence1.getBytes();
DatagramPacket sendPacket4 =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket4);
}
catch (SocketTimeoutException a)
{
System.out.println("Timed out!");
}
}while(sentence!=null);
}
catch (IOException e)
{
System.err.println(e);
}
finally
{
clientSocket.close();
}
}
}
SERVER SIDE:
import java.io.IOException;
import java.net.*;
public class UDPServer extends Thread implements Runnable
{
DatagramSocket serverSocket;
public static void main(String args[]) throws Exception
{
try
{
UDPServer t1 = new UDPServer();
Thread t = new Thread(t1);
t.start();
}
catch (Exception e)
{
System.out.println("Error!");
}
}
public UDPServer() throws Exception
{
this.serverSocket = new DatagramSocket(9876);
}
public void run()
{
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
String checksum="";
String Data = "";
String WinSize = "";
String carry = "0";
String output="";
String n,o;
String output1;
String sentence1="";
int i,j=0;
while(true)
{
String sentence="";
try
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress IPAddress = receivePacket.getAddress();
sentence = new String( receivePacket.getData(),0,receivePacket.getLength());
if(sentence.contains("checksum"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
checksum = sentence.substring(0,i);
}
}
System.out.println("Checksum as specified by client is: " + checksum);
}
else if(sentence.contains("Data"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
Data = sentence.substring(0,i);
}
}
System.out.println("Data requested by client is: " + Data);
}
else if(sentence.contains("Windowsize"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
WinSize = sentence.substring(0,i);
}
}
System.out.println("Window Size is: " + WinSize);
}
else if(sentence.contains("Finished"))
{
output1 = checksumAdd(carry,Data,checksum);
output = reverse(output1);
System.out.println("Checksum Addition before complementing digits = "+output);
output = complement(output);
output = reverse(output);
System.out.println("Checksum Addition after complementing digits = "+output);
int WindowSize = Integer.parseInt(WinSize);
int strlen = Data.length();
do
{
for(i=j;i<(WindowSize+j);i++)
{
if(i!=strlen)
{
String send = "";
n = Data.substring(i,i+1);
System.out.println("Value of n is: "+n);
send = send.concat(n+":");
o = output.substring(i,i+1);
System.out.println("Value of o is: "+o);
send = send.concat(o);
sendData = send.getBytes();
DatagramPacket sendPacket1 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
serverSocket.send(sendPacket1);
}
else
break;
}
j+=WindowSize;
DatagramPacket receivePacket2 = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket2);
sentence1 = new String( receivePacket2.getData(),0,receivePacket2.getLength());
System.out.println("sentence 1 is: "+sentence1); //To be removed. Used for testing purposes
}while(i!=strlen);
}
}
catch (IOException e)
{
System.err.println(e);
}
}
}
public static String complement(String output) {
String temp="";
int len = output.length();
for(int i=len;i>0;i--)
{
String t = output.substring(i-1,i);
if(t.equals("0"))
temp = temp.concat("1");
else if(t.equals("1"))
temp = temp.concat("0");
}
return temp;
}
public static String reverse(String output)
{
String temp="";
int len = output.length();
for(int i=len;i>0;i--)
{
String t = output.substring(i-1,i);
temp = temp.concat(t);
}
return temp;
}
public static String checksumAdd(String carry, String Data,String checksum)
{
int strlen = Data.length();
int flag=0;
String output="";
String output2="";
String n,che;
String sum = null;
for(int i=strlen;i>0;i--)
{
n=Data.substring(i-1,i);
che = checksum.substring(i-1,i);
if(n.equals("0") && che.equals("0") && carry.equals("0"))
{
sum = "0";
carry = "0";
}
else if(n.equals("0") && che.equals("0") && carry.equals("1"))
{
sum = "1";
carry = "0";
}
else if(n.equals("0") && che.equals("1") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("0") && che.equals("1") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("0") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && che.equals("0") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("1") && carry.equals("0"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("1") && carry.equals("1"))
{
sum = "1";
carry = "1";
}
output = output.concat(sum);
}
if(carry.equals("1"))
{
n = output.substring(0,1);
if(n.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1"))
{
sum = "0";
carry = "1";
}
output2 = output2.concat(sum);
for(int i=strlen-1;i>0;i--)
{
n=Data.substring(i-1,i);
if(n.equals("0") && carry.equals("0"))
{
sum = "0";
carry = "0";
}
else if(n.equals("0") && carry.equals("1"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
output2 = output2.concat(sum);
}
flag = 1;
}
if (flag==1)
return output2;
return output;
}
}
If your client doesn't receive a response from your server, it's probably because the server doesn't send a response.
serverSocket.send(sendPacket1);
is only called if(i!=strlen).
Have you tried to output some text in the console inside the if block to see if/when you enter that loop?