0

我是套接字编程的新手。我想使用 C# 创建一个适用于 PC 的应用程序,一个适用于 android 手机的应用程序可以通过无线网络 (LAN) 与其他手机进行通信。

为了将数据发送到 android 应用程序,我在 PC 端使用此代码:

    private void sendButton_Click(object sender, EventArgs e)
    {
        try
        {
            Object objData = messageTextBox.Text;
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
            _socket.Send (byData);
        }
        catch(SocketException se)
        {
            MessageBox.Show (se.Message );
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.18"), 2001);
            _socket.Connect(_ipEndPoint);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

在 android 应用程序中,我想使用以下代码接收数据:

Handler myUpdateHandler = new Handler()
       {
            public void handleMessage(Message msg)
            {
                  Log.d(appTag, "setting textview");
                  TextView tv = (TextView) findViewById(R.id.messageText);
                  tv.setText(mClientMsg);              
            }
       };

class CommsThread implements Runnable
{
    public void run()
    {
        try
        {
            Socket s = null;
            try
            {
                ss = new ServerSocket(2001);
            }
            catch (IOException e)
            {
                Log.d(appTag, e.toString());
            }
            while (!Thread.currentThread().isInterrupted())
            {
                Message m = new Message();
                Log.d(appTag, "message m = new message()");

                try
                {
                    if (s == null) s = ss.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                    String line = null;
                    while ((line = in.readLine()) != null)
                    {

                        Log.d(appTag, line);

                        mClientMsg = line;
                    }

                    myUpdateHandler.sendMessage(m);
                }
                catch (IOException e)
                {
                    Log.d(appTag, e.toString());
                }
            }
        }
        catch (Exception e)
        {
            Log.d(appTag, e.toString());
        }
    }
}

但是android应用程序不会收到任何数据。

谁能帮我解决我的问题?我在套接字编程方面非常菜鸟。

抱歉英语不好。谢谢。

4

1 回答 1

0

如果您将阅读器更新为只阅读字符,然后将它们吐出,它会显示什么?例如

            try
            {
                if (s == null) s = ss.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

                char[] recvd = new char[1];
                while (in.read(recvd, 1, 1) != -1)
                {
                    String line = new String(recvd);
                    Log.d(appTag, line);

                    mClientMsg = line;
                }

                myUpdateHandler.sendMessage(m);
            }
            catch (IOException e)
            {
                Log.d(appTag, e.toString());
            }
于 2012-08-20T17:05:24.983 回答