开发者

socket read issue in multithreaded JAVA application

开发者 https://www.devze.com 2023-03-06 10:51 出处:网络
We have created a socket in a multithreaded JAVA application on Windows, which has to read data in sequential form. But we are facing issue in reading the data from the inputstream

We have created a socket in a multithreaded JAVA application on Windows, which has to read data in sequential form. But we are facing issue in reading the data from the inputstream

For example , data order is

1-100 (1,2,3,4,...100)
101-200
201-200
and so on..

We are able to re开发者_开发百科ad from 1-100 in proper sequential manner, but not able to read next set of data, i.e. 101-200. But can read further set of data properly.

The data is coming at our end properly (as seen in etheral log's), but stream is not able to read it.

Here is the code snippet

socket.getInputStream.read(buffer);

socket is of Socket class type.

What can be the possible reason behind the same? Please share your views.


Here is the code snippet

> socket.getInputStream.read(buffer);

No it isn't. That snippet doesn't compile. And the correct snippet is trivially obvious.

The only ways that can miss data are (i) if another thread is reading the socket at the same time, or (ii) if you are using BufferedInputStream or BufferedReader at one time and not at another.


You mention this is a multithreaded application so without seeing your code my suspicions would lie in another thread possibly manipulating your Socket or InputStream. Threading issues can be incredibly difficult to spot and debug.


Thanks for your comments. The issue was with multithreading synchronization, i handled the same and it worked fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消