java - Thread sleeps after few iterations while reading response of socket -


I am developing the application in which I am writing / reading socket. However, it is only working 11 times, then it sleeps.

PollThread.java

  public class PollThread {static string result; Private Static Timer myTimer; Fixed string IP = "192.168.1.19"; Public Stable Zero Main (String [] Elggs) {// Todo Auto-Generated Method Stub System. Outprint ("Polothrade"); MyTimer = New Timer (); MyTimer.schedule (New TimerTask) {@Override Public Zero Run (ClientThread cThread = New ClientThread (IP); String position = cThread.readStatus (); System.out.println ("Stoss ::" + status); }}, 0, 2000); }}   

ClientThread.java

  public class ClientThread {string byte_to_hex, swap_status, result, ipadder; Public socket s; Public inputstream; Public OutputStream O; Integer position; Public ClientTrade (String IP) {// To-O Auto-Generated Constructor Stub this.ipAddr = ip; } Public int read stats (try {s = new socket (IPadder, 502); I = s.getInputStream (); O = s.getOutputStream (); Byte [0] 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, byte) 0x01, (byte) 0x00, (Byte) 0x00, (byte) 0x00, (byte) 0x19}; O.write (packet1); I.read (Data 1, 0, 1024); // Comment this byte_to_hex = bittohex (data1) Asbestring (18, 26); Four [] arr = byte_to_hex.toCarrere (); (Int i = 0; i & lt; arr.length - 1; i + = 2) {char temp = arr [i]; Arr [i] = arr [i + 1]; Arr [i + 1] = temporary; } Swapped_result = New string (arr); Results = HexStringTabBibri (Swap_Health, 32); Int counter = 0; For (int i = 0; i & lt; result.length (); i ++) {if (result.charAt (i) == '1') {counter ++; }} Position = counter; } Hold (unknownhostexception e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Return status; }}   

Result

  Stoss :: 1 stos :: 1 stos :: 1 staus :: 1 stoss :: 1 stos :: 1 staus :: 1 staus :: 1 staus :: 1 staus :: 1 stos :: 1   

If I comment i.read (data1, 0, 1024); Then it works fine, but I need this line to get results.

What could be the problem with this? Why is it running only 11 times?

UPDATE-1

  Reading status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ... Status: 1 Reading Status ...   

Update 2

I did this line cThread.start (); Even tried by adding the PollThread.java file to but still the result is the same.

As other commentators have mentioned, your timer is actually making multiple clients Connection , one every two seconds, unlike a connection which periodically tests. It is quite possible that your "server simulator" has a maximum connected number of 10 or 11. Try to move your client connection out of your timer task, so it is not created every time:

  public static zero main string [] args) {// TODO auto-generated Method stub system Outprint ("Polothrade"); MyTimer = New Timer (); Last ClientTread cThread = New ClientTrade (IP); MyTimer.schedule (New Timer Task) {@Override Public Zero Run () {String Status = cThread.readStatus (); System.out.println ("Stoss ::" + status);}}, 0, 2000); }   

Sorry only that you are creating a new socket inside the readStatus method, which is essentially the same multiple connection problem. Try moving that content into the manufacturer, such as:

  Public clientTrade (string IP) {// Tudo auto-generated constructor stub this.ipAddr = ip; Try {s = new socket (IPadder, 502); I = s.getInputStream (); O = s.getOutputStream (); // ...} public int readStatus () {try {// s = new socket (IPadder, 502); // i = s.getInputStream (); // o = s.getOutputStream ();    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -