loosing messages during synchron communication

Last post 04-23-2008 5:23 by Taggert. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 04-23-2008 5:23

    loosing messages during synchron communication

    Hi folks,

    I need your help once again. This time I have a problem with the communication of a mobile device and a java server. The server is doing pl/sql commands for me and sends back the results to the mobile device. Unfortunately sometimes the server sends a message and the mobile device is not receiving it, although it was send and the WLAN network is stable. I have actually no clue what happens.

    I was searching the internet already for a solution of the subject. Here is the code, maybe you can help me with it The class Connectionhandler is a singelton, so the constructor is private and the sending and receiving I put in an extra thread, so I can close it, if there are problems. However this does not change anything on the given problem, that each 15th message (in average) is lost.

    Thank you very much for your help
    Best regards
    Taggert

    private TcpClient socketForServer = null;
    private NetworkStream netStream = null;
    private System.IO.StreamReader reader = null;
    private System.IO.StreamWriter writer = null;
    private static ConnectionHandler einzigeInstanz = null;
    private int numberOfSends = 0;
    private static int BUFSIZE = 4096;
    private static byte[] idr = new byte[BUFSIZE];
    private static bool readready = false;
    private Thread sendreceive = null;
    private string send_message = "";
    private int readSize = 0;
    private ConnectionHandler()
    {
    connect();
    }
    private void closeConnection()
    {
    try
    {
    netStream.Close();
    socketForServer.Close();
    socketForServer = null;
    netStream = null;
    }
    catch (Exception ex)
    {
    }
    }
    public void connect()
    {
    if (socketForServer != null)
    {
    closeConnection();
    }
    try
    {
    socketForServer = null;
    netStream = null;
    reader = null;
    writer = null;

    socketForServer = new TcpClient(DataContainerAllgemein.getInstanz().getIPAddress(), 51101);
    Thread.Sleep(50);
    netStream = socketForServer.GetStream();
    }
    catch (Exception ex)
    {
    if (socketForServer != null)
    {
    closeConnection();
    }
    }
    }

    private bool sendMessage(string message)
    {
    sendreceive = new Thread(new ThreadStart(sendreceivethread));
    readready = false;
    sendreceive.Priority = ThreadPriority.AboveNormal;
    sendreceive.Name = "sendreceive";
    send_message = message;
    sendreceive.Start();
    return true;
    }

    private void sendreceivethread()
    {
    try
    {
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(send_message);
    if (netStream == null)
    {
    connect();
    Thread.Sleep(200);
    }
    if (netStream.CanWrite)
    {
    netStream.Write(data, 0, data.Length);
    }
    else
    {
    readready = true;
    }

    readready = false;
    this.readSize = 0;
    int tries = 0;
    while (tries
    {
    if (netStream.DataAvailable && netStream.CanRead)
    {
    this.readSize = netStream.Read(idr, 0, idr.Length);
    break;
    }
    Application.DoEvents();
    Thread.Sleep(100);
    tries++;
    }
    readready = true; // static
    }
    }
    Filed under: ,
Page 1 of 1 (1 items)