【问题标题】:send a hello message from android client to c# server从 android 客户端向 c# 服务器发送 hello 消息
【发布时间】:2016-11-22 09:05:20
【问题描述】:

我正在尝试创建这个简单的客户端-服务器。我有一个 android 客户端和 c#server。只是一个简单的程序,它向服务器发送 hello 消息,但没有发送消息。

我的java代码:

Thread t= new Thread()
{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                Socket myClient= new Socket("192.167.01.123",7000);
                DataOutputStream dos= new DataOutputStream(myClient.getOutputStream());
                dos.writeBytes("Hello");
                dos.flush();
                dos.close();
                myClient.close();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                System.out.println("unknown host");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("IOExxception");
            }
        }


    };
    t.start();
    Toast.makeText(this," Message sent" , Toast.LENGTH_SHORT).show();
}

和c#代码:

    static void Main(string[] args)
    {
        TcpListener serverSocket = new TcpListener(7000);

        TcpClient clientSocket = new TcpClient();
        serverSocket.Start();
        Console.WriteLine("Server started.");
        clientSocket = serverSocket.AcceptTcpClient();
        Console.WriteLine("Accept conns from client.");

        while (true)
        {
            try
            {

                NetworkStream networkStream = clientSocket.GetStream();
                Byte[] bytes = new Byte[10025];
                networkStream.Read(bytes, 0, (int)clientSocket.ReceiveBufferSize);
                string dataClient = System.Text.Encoding.ASCII.GetString(bytes);
                Console.WriteLine("data from client: " + dataClient);


                networkStream.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        clientSocket.Close();
        serverSocket.Stop();
        Console.WriteLine("EXIT");
        Console.ReadKey();
    }
}

    }

【问题讨论】:

  • 安卓端有异常吗?
  • nooo... 没有例外.. nothingg... 只是消息没有发送
  • 未发送或未收到?请使用日志语句来查看会发生什么。 “消息已发送”吐司没有发生?如果确实发生了,那就是错误的消息。您唯一可以说的是“线程已启动”。
  • “消息已发送”我可以看到。但是仍然没有将“hello”消息发送到服务器。另外,我正在尝试通过远程设备而不是模拟器发送它。

标签: c# java android


【解决方案1】:

应该在while循环内

clientSocket = serverSocket.AcceptTcpClient();

【讨论】:

    【解决方案2】:

    试试这个:

    接收消息的c#服务器类:

    class Server
    {
        int BUFSIZE = 100;
        int servPort = 1551;
        public static String ClientOrder = "";
        public static int bytesRcvd;
        public void RunServer()
        {
            TcpListener listener = null;
            TcpClient client = null;
            NetworkStream netStream = null;
            listener = new TcpListener(IPAddress.Any, servPort);
            listener.Start();
            byte[] rcvBuffer = new byte[BUFSIZE];
            while (true)
            {
                client = listener.AcceptTcpClient(); // Get clien
                netStream = client.GetStream();
                {
                    rcvBuffer = new byte[BUFSIZE];
                    bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length);
                    ClientOrder = (Encoding.ASCII.GetString(rcvBuffer)).Substring(0, bytesRcvd);
                    netStream.Close();
                    client.Close();
                }
            }
        }
    }
    

    发送消息的Android客户端类:

    public class Send_Message_To_Server implements Runnable {
    private String mMsg,Server;
    private Socket client;
    private PrintWriter printwriter;
    public Send_Order_To_Server(String msg,String IP) {
        mMsg = msg;
        Server=IP;
    }
    public void run() {
        try {
            client = new Socket(Server, 1551);  //connect to server
            printwriter = new PrintWriter(client.getOutputStream(),true);
            printwriter.write(mMsg);  //write the message to output stream
            printwriter.flush();
            printwriter.close();
            client.close();   //closing the connection
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-13
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多