Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:

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


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:

C#
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;
            }
        }


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. :)
Posted
Comments
[no name] 20-Feb-12 7:55am    
I got the solution myself and now it is receiving data from C#. But it is still freezing at last line: inputText.setText(socketInputStream.readUTF()) and not moving ahead. So now just want to know how to get String from the byte that sent by C# code.

doesn't the Java code run into a heap space exception as you are constantly creating connections and streams??
 
Share this answer
 
Comments
[no name] 20-Feb-12 2:28am    
Yes, there is an exception at workerSocket = serverSocket.accept(); line in Java code and the app is freezing in Android device.
TorstenH. 20-Feb-12 5:00am    
so YOU ARE running into Heap Space Exceptions? There you go. Don't use an infinite, never ending while-loop to create connections.

I would infact make it a Singleton and use a method getInstance() to retrieve the same object for the connection each time - otherwise you'll end up with a big stack of unclosed connections waiting for the timeout...
Link for Singeltons explanation: http://java.sun.com/developer/technicalArticles/Programming/singletons/
[no name] 20-Feb-12 8:05am    
I got the solution myself and now it is receiving data from C#. But it is still freezing at last line: inputText.setText(socketInputStream.readUTF()) and not moving ahead. So now just want to know how to get String from the byte that sent by C# code.
Well, I have solved this myself.

Check out the solved Java code below:
Java
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.
 
Share this answer
 
Comments
ganeshkumar66 7-Sep-13 8:20am    
HiI use this code but data is getting in java
UniqueDesign_01 25-Aug-18 7:13am    
hican we also share the files using this code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900