0

I have a server written in C# and a client side in Android. If I send a message from client (Android) to server (c#) and from server to client, everything works fine. If I try to send one message from client , one from server, another from client, the client remains stuck at reading the message from the server. What could be the problem? My client code is:

 sendBytes("HELLOX".getBytes());
 readBytes(byDataReceived);//here it gets stucked
...
        try
        {
            int nrsend=sendBytes("HELLOX".getBytes()); 
            readBytes(byDataReceived);
        }
        catch (Exception se)
        {
            Log.d("IdealLog","Exception: "+se.getMessage()+" ");
            Toast.makeText(context, se.getMessage()+" " , 10000).show();
           // MessageBox.Show(se.Message);
            return false; 
        }
...
 public static int readBytes(byte[] myByteArray) throws IOException 
 {
          Log.d("IdealLog","readBytes-begin");
        InputStream in = socket.getInputStream();
        BufferedReader buffreader = new BufferedReader(new InputStreamReader(in)); 
        String finalText = "";
        String text = "";
        while ((text = buffreader.readLine()) != null) 
        {
            finalText += text;
        }  
        myByteArray=new byte[myByteArray.length];
        myByteArray=EncodingUtils.getAsciiBytes(finalText);
        Log.d("IdealLog","Input Stream: "+finalText);
        Log.d("IdealLog","TEST: "+EncodingUtils.getAsciiString(myByteArray));
        Log.d("IdealLog","readBytes-end");

        byDataReceived=myByteArray;
        buffreader.close();
        return myByteArray.length;//myByteArray.length;
 }//readBytes end
  public static int sendBytes(byte[] myByteArray) throws IOException 
    {
        return sendBytes(myByteArray, 0, myByteArray.length);
    }//sendBytes end

    public static int sendBytes(byte[] myByteArray, int start, int len) throws IOException 
    {
        if (len < 0)
        {
            throw new IllegalArgumentException("Negative length not allowed");
        }
        if (start < 0 || start >= myByteArray.length)
        {
            throw new IndexOutOfBoundsException("Out of bounds: " + start);
        }
        OutputStream out = socket.getOutputStream(); 
        DataOutputStream dos = new DataOutputStream(out);
       // dos.writeInt(len);
        if (len > 0) 
        {
            dos.write(myByteArray, start, len);
        }
        int size=dos.size();
        dos.flush();
       return size;
    }//sendBytes end

My server code:

static void Main(string[] args)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, 1408);
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        socket.Bind(ip);
        socket.Listen(10);
        Console.WriteLine("Waiting for a client...");
        Socket client = socket.Accept();
        IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
        Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);



        string welcome = "HELLO&";
        byte[] data = new byte[200];
        client.Receive(data);
        Console.WriteLine("Received data from CLIENT TEST1: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data));

        ASCIIEncoding asen = new ASCIIEncoding();
        byte[] data2 = new byte[200];
        data2 = asen.GetBytes(welcome);
        client.Send(data2, data2.Length, SocketFlags.None);



 //if i comment out from this 3 lines, everything is working fine
        byte[] data3 = new byte[200];//this
        client.Receive(data3);//this
        Console.WriteLine("Received data from CLIENT TEST2: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data3));//this



        Console.WriteLine("Disconnected from {0}", clientep.Address);
        client.Close();
        socket.Close();

        Console.ReadLine();
    }
Kai
  • 38,985
  • 14
  • 88
  • 103
DevtoLoper
  • 49
  • 1
  • 12

1 Answers1

0

Modify into this:

   //if i comment out from this 3 lines, everything is working fine
   byte[] data3 = new byte[200];//this 
   client.Receive(data3);//this    
   Console.WriteLine("Received data from CLIENT TEST2: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data3));//this 

   client.Send(data2, data2.Length, SocketFlags.None); 

   Console.WriteLine("Disconnected from {0}", clientep.Address); 
   client.Close();       
   socket.Close();       
   Console.ReadLine(); 
}
xyz
  • 211
  • 1
  • 3
  • in the server window i get the following: "Received data from CLIENT TEST1: HELLOX" – DevtoLoper Jan 04 '12 at 14:04
  • in the server window i get the following: "Received data from CLIENT TEST1: HELLOX" and in the android log: readBytes-Begin – DevtoLoper Jan 04 '12 at 14:05
  • i have found the solution for my problem: http://stackoverflow.com/questions/8758189/server-remains-hang-when-sending – DevtoLoper Jan 07 '12 at 10:17