Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:46:38+00:00 2026-06-14T13:46:38+00:00

I have a very strange behaviour during writing to a socket. In my mobile

  • 0

I have a very strange behaviour during writing to a socket.
In my mobile client I’m using a socket which is initialized as follows:

private void initSocket()
{
    socket = new Socket();
    socket.connect(new InetSocketAddress(host, port));

    os = new DataOutputStream(socket.getOutputStream());
    is = new DataInputStream(socket.getInputStream());
}

then periodically (every 60 seconds) I read and write some data to this socket (the code here is a little bit simplified):

if(!isSocketInitialized())
{
    initSocket();
}

byte[] msg = getMessage();

os.write(msg);
os.flush();

int bytesAvailable = is.available( );
if(bytesAvailable>0)
{
    byte[] inputBuffer = new byte[bytesAvailable];

    int numRead = is.read(inputBuffer, 0, bytesAvailable);
    processServerReply(inputBuffer, numRead);
}

And it works. But… Sometimes (very infrequently, maybe 1 or 2 times per day) my server don’t receive data. My client log looks like:

Written A
Written B
Written C
Written D
Written E

and so on. But on the server side it looks like:

Received A
Received E

B,C,D data records were not received, despite of fact that on the client side it looks like all data was sent without any exceptions!

Such gaps can be small (2-3 minutes) which is not very bad, but sometimes they can be very big (1-2 hours = 60-120 cycles) and it is really a problem for my customers.

I really have no idea what can be wrong. The data seems to be sent by client, but it never arrives on the server side. I’ve checked it also with a proxy.

I have only logs and I can’t reproduce this issue (but it happens to my customer more then one time every day) and in logs sometimes I see that the connection is broken with an Exception “sendto failed: ECONNRESET (Connection reset by peer)”. After that the program closes the socket, reinitializes it:

// close
is.close();
os.close();
socket.close();

// reinitialize
initSocket();

and tries to write the data again as described above. Then I see the problem: connection established, writing successful, but NO DATA arrived on the server!

May be it has something to do with ECONNRESET may be not, but I want to mention this because may be it is important.

I would be very grateful for any ideas and tips.

P.S. Maybe it plays some role: the client code runs on an Android mobile device which is moving (it is in a car). The internet connection is established through GPRS.


UPD: I can reproduce it! At least partially (the client send A,B,C,D,E and the server receives only A). It happens every time if:

  1. The connection is established, the client reads and writes -> OK

  2. The connection is lost (I turn off my WLAN router :)), I became IOException, I close the streams and socket -> OK

  3. I turn on my router, the connection is back, I initialize the socket again, the program executes write() without exceptions, but… no data arrives at the server.

BTW since the connection is back again available() returns always 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:46:39+00:00Added an answer on June 14, 2026 at 1:46 pm

    The cause of this strange behavior was a not closed socket on the server side. I could reproduce it with small client and server. Both consist of couple of lines of code, which do the following:

    1. connect to the server
    2. simulate a dead spot (e.g. turn off your WiFi)
    3. close the socket on client side
    4. DON’T CLOSE the socket on server side
    5. turn on your WiFi
    6. establish a new connection from client
    7. write data to this connection

    Voila! The client writes data without any errors, but the server doesn’t receive it…

    But if the server closes the socket too, then the server can read this data. So the solution should be to close the socket after a timeout on the server side.

    Unfortunately in my case it is not viable, because the server is a proprietary third-party software.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very strange behaviour of Java Date class: System.out.println(new Date().toGMTString()); long l =
I have run into a very strange behaviour I can’t make sense of. I
I have a very strange behaviour in the Show Promotions Dialog in BizTalk 2006.
In my app I have a very strange scrollbar behaviour: The bottom scrollbar changes
A client has reported repeated instances of Very strange behaviour when executing a stored
I'm fighting with a very strange behaviour in my Android app. I have a
I have a very simple application with a very strange behaviour. It's essentially the
I'm trying to visualize Mandelbrot set with OpenGL and have found very strange behaviour
We have a very strange behaviour of a WPF application. Sometimes (not very ofen),
This is a very strange behaviour. If I have something like this (time interval

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.