Android中的套接字仍然卡住

时间:2012-01-04 13:32:32

标签: java android tcp

我有一个用C#编写的服务器和一个Android客户端。如果我从客户端(Android)向服务器(c#)发送消息,从服务器发送到客户端,一切正常。如果我尝试从客户端发送一条消息,一条来自服务器,另一条来自客户端,则客户端仍然停留在从服务器读取消息。可能是什么问题呢? 我的客户代码是:

 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

我的服务器代码:

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();
    }

1 个答案:

答案 0 :(得分:0)

修改为:

   //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(); 
}