通过UDP连接接收的数据不会显示在文本视图中

时间:2013-11-04 12:04:23

标签: c# android textview udp

编辑:我用工作代码更新了帖子。

我尝试将文本从我的电脑上的文本文件发送到手机并在文本视图中显示。

在PC上用C#制作服务器,它完成工作没有任何错误。此外,手机上的代码没有任何错误。

我认为问题在于编码,文本视图只是空白,它甚至不显示“Hello world”示例文本。

此部分在PC上

    class Program
{
    static int port = 11000;




    static void Main(string[] args)
    {
        ServerThread();
    }

    static public void ServerThread()
    {
        Thread t = new Thread(Recieve);
        t.Start();
    }

    static public void Recieve()
    {
        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.10"), port);
        UdpClient client = new UdpClient(port);

        while(true)
        {
            Console.WriteLine("Server listening");
            client.Connect(ipEndPoint);

            Byte[] recieveBytes = client.Receive(ref ipEndPoint);

            if (Convert.ToInt16(Encoding.ASCII.GetString(recieveBytes))==0)
            {
                Console.WriteLine("Shut down");
                break;
            }

            Console.WriteLine("Contact");
            Thread.Sleep(1000);

            Console.WriteLine("Sending file");
            string text = File.ReadAllText(@"C:\Users\.............\kviz.txt");

            Byte[] sendBytes = Encoding.Unicode.GetBytes(text);
            client.Connect(ipEndPoint);
            client.Send(sendBytes, sendBytes.Length);


         }

        Console.WriteLine("Server closing");
        client.Close();
    }

}

这部分是电话

    public class MainActivity extends Activity {

private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView)findViewById(R.id.textView1);

    new ClientAsyncTask().execute();
    //new ServerAsyncTask().execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class ClientAsyncTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {

        String result = "";
        try {

            Send();
            result = Recieve();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        text.setText(result);
    }

}

public void Send()
{
    String s = "1";
    int port = 11000;
    byte[] message = s.getBytes();
    int length = s.length();

    try {
        InetAddress local  = InetAddress.getByName("192.168.0.11");

        DatagramSocket socketSend = new DatagramSocket(port);
        DatagramPacket p = new DatagramPacket(message, length, local, port);
        socketSend.send(p);

        socketSend.close();

    } catch (SocketException e) {

        e.printStackTrace();
    } catch (UnknownHostException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


}

public String Recieve()
{
    String recieveString = "";

     try {
            InetAddress local  = InetAddress.getByName("192.168.0.11");
            byte[]recieveMessage = new byte[1000];

            int port = 11000;

            DatagramSocket socketRecieve = new DatagramSocket(port);
            DatagramPacket recievePacket = new DatagramPacket(recieveMessage, recieveMessage.length, local, port);

            socketRecieve.receive(recievePacket);
            recieveString = new String(recievePacket.getData());


            Log.i("InfoTest", recieveString);

        } catch (SocketException e) {

            e.printStackTrace();
        } catch (UnknownHostException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

     return recieveString;
}
}

0 个答案:

没有答案