繁体   English   中英

通过Socket从C#到Android。 找不到我的错误

[英]From C# to Android via Socket. Can't find my mistake

问题-我无法使此功能正常工作。 我在LogCat中看到套接字已连接到C#服务器,但没有看到接收到的数据。 我做错了什么?

这是带有MessageBoxes的c#函数,用于检查:

private void receiveConnection(){
        Socket myHandler = null;
        bool isConnected = false;
        while (true)
        {
            if (!isConnected)
            {
                myHandler = wSocket.Accept();
                isConnected = true;
                MessageBox.Show("We have client!");
            }

            if (sendDataToAndroid)
            {  
                try
                {
                    sendDataToAndroid = false;   
                    NetworkStream stream = new NetworkStream(myHandler);
                    StreamWriter sw = new StreamWriter(stream);

                    string myMsg = "";
                    myMsg += temp_F.Length + " ";

                    temp_F[0] = 3.151F;
                    temp_F[1] = 1.415F;
                    temp_F[2] = 5.572F;
                    temp_F[3] = 6.320F;

                    for (int i = 0; i < temp_F.Length; i++)
                    {
                        myMsg += temp_F[i] + " ";
                    }
                    byte[] msg = Encoding.ASCII.GetBytes(myMsg);

                    MessageBox.Show("Data to send: " + myMsg);

                    try
                    {
                        myHandler.Send(msg);
                        MessageBox.Show("Data has been sent!");
                    }
                    catch (Exception e) {
                        MessageBox.Show("Error while sending data!");
                    }
                }

                catch (Exception e)
                {
                    isConnected = false;
                    myHandler.Close();
                    myHandler = null;
                    MessageBox.Show("Error while sending data...");
                }
            }
        } 
    }

这是始终尝试接收数据的Android函数:

public class SendThread implements Runnable {
    public void run() 
    {
        Socket socket = null;
        BufferedReader in = null;

        while (true)
        {
            // Loop until connected to server
            while (socket == null){
                try{
                    socket = new Socket ("192.168.137.1", 808);
                }
                catch (Exception e) {
                    socket = null;
                }
            }

            // Get from the server   
            try {
                Log.d("Connection: ", "connected");
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = null;


                while ((line = in.readLine()) != null) {
                    Log.d("Socket:", line);

                    NumberFormat nf = new DecimalFormat ("990,0");

                    String[] tokens = line.split(" ");

                    int currTempSize = Integer.parseInt(tokens[0]);
                    currentTemp = new double[currTempSize];

                    for (int i = 0; i < currTempSize; i++)
                        currentTemp[i] = (Double) nf.parse(tokens[i+1]);

                    //Toast.makeText(getApplicationContext(), "Received data:", duration)
                    for (int i = 0; i < currTempSize; i++){
                        Log.d("Converted data: currentTemp["+i+"] = ", currentTemp[i]+"");
                    }
                }
            } 
            catch (Exception e) {
                    socket = null;
                    in = null;
                    Log.d("Connection: ", "lost.");
            }
        }
    }
}

您的Java代码一次读取一行,但是您从未从C#发送行终止符。

您可以手动将其附加到myMsg ,也可以在StreamWriter上使用sw.WriteLine()方法(否则该方法似乎没有使用)。 调用sw.WriteLine()您可能必须调用sw.Flush()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM