0

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

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

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

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

这部分在电脑上

    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;
}
}
4

0 回答 0