无法将数据从C#发送到Java(Android)程序. [英] Unable to send data from C# to Java (Android) program.

查看:64
本文介绍了无法将数据从C#发送到Java(Android)程序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Wifi通过TCP连接将数据从C#.NET(Windows应用程序)程序发送到Java(Android应用程序)程序,反之亦然.到目前为止,我已经成功地将数据从Java发送到C#,但是无法将数据从C#发送到Java.

以下是Java代码,我用来创建连接并接收数据:

I am trying to send data from a C#.NET (Windows Application) program to a Java (Android App) program and vice versa, via TCP connection through Wifi. Till now I am success to send data from Java to C#, but unable to do so from C# to Java.

Following is the Java code, I used to create a connection and receive data:

ServerSocket serverSocket = null;
DataInputStream socketInputStream;
while (true) {
        try {
            String localIPAddr = getLocalIPAddress();
            InetSocketAddress ipEndPoint = new InetSocketAddress(
                    InetAddress.getByName(localIPAddr), 8222);
            serverSocket = new ServerSocket();
            serverSocket.bind(ipEndPoint, 4);
            workerSocket = serverSocket.accept();

            socketInputStream = new DataInputStream(
                    workerSocket.getInputStream());
            inputText.setText(socketInputStream.readUTF());
        } catch (Exception ex) {
            throw ex;
        }
    }



这里的getLocalIPAddress()方法返回Android设备的IP地址.

以下是Windows应用程序中的C#代码,用于连接到Android的IP地址(192.168.1.6)并向其发送数据:



Here getLocalIPAddress() method returns the IP Address of the Android Device.

Following is the C# code in Windows Application to connect to the Android''s IP Address (192.168.1.6) and send data to it:

Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clientSocket.Connected)
                    clientSocket.Connect(IPAddress.Parse("192.168.1.6"), 8222);
                clientSocket.Send(Encoding.UTF8.GetBytes(txtInput.Text));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



好吧,客户端(C#)无法连接到服务器(Java),这意味着数据没有从客户端离开.但是,如果连接了,它会.

请告诉我我想念的是什么,我在哪里弄错了. :)



Well, client (C#) is failing to connect to the server (Java) That means data is not leaving from client. But it will, if it get connected.

Please tell me what am I missing and where am I mistaken. :)

推荐答案

不会在不断创建连接和流时将Java代码运行到堆空间异常中吗?
doesn''t the Java code run into a heap space exception as you are constantly creating connections and streams??


好吧,我自己解决了这个问题.

在下面查看已解决的Java代码:
Well, I have solved this myself.

Check out the solved Java code below:
ServerSocket serverSocket = null;
Socket workerSocket;
DataInputStream socketInputStream;
try {
    if (serverSocket == null) {
        // No need to get local IP address and to bind InetSocketAddress.
        // Following single line make it very simple.
        serverSocket = new ServerSocket(8222, 4);
        workerSocket = serverSocket.accept();
    }
    // When data are accepted socketInputStream will be invoked.
    socketInputStream = new DataInputStream(
                workerSocket.getInputStream());

    /* Since data are accepted as byte, all of them will be collected in the
    following byte array which initialised with accepted data length. */
    byte[] rvdMsgByte = new byte[socketInputStream.available()];

    // Collecting data into byte array
    for (int i = 0; i < rvdMsgByte.length; i++)
        rvdMsgByte[i] = socketInputStream.readByte();

    // Converting collected data in byte array into String.
    String rvdMsgTxt = new String(rvdMsgByte);

    // Setting String to the text view.
    receivedMsg.setText(rvdMsgTxt);
} catch (Exception ex) {
    throw ex;
}


请注意,将使用一个单独的线程在后台运行此代码.


Note that a separate thread is to be used to run this code in background.


这篇关于无法将数据从C#发送到Java(Android)程序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆