Sending data through WiFi (Windows) and receive it in Java (Android)

Discussion in 'Programming and Homework Questions' started by Rotary Heart, May 9, 2013.

Thread Status:
Not open for further replies.
  1. Rotary Heart

    Rotary Heart Notebook Evangelist

    Reputations:
    253
    Messages:
    613
    Likes Received:
    213
    Trophy Points:
    56
    I'm trying to make a simple Windows software that will send data through WiFi to Java (Android) but I can't find to make it work. I hope that someone can help me out here.

    Windows (C#) code

    Code:
            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.3"), 8222);
    
                    clientSocket.Send(Encoding.UTF8.GetBytes("Esto es una prueba"));
                }
                catch (Exception ex)
                {
                }
            }
    Android (Java) code

    Code:
    		localIPAddr = Utils.getIPAddress(true);// = getLocalIPAddress();
    
    		texto.setText(localIPAddr);
    		
    		Log.d("MIGUELL", localIPAddr);
    
    		new Thread(new Runnable() {
    			public void run() {
    
    				while (true) {
    					try {
    					    if (serverSocket == null) {
    					        serverSocket = new ServerSocket(8222, 4);
    					        workerSocket = serverSocket.accept();
    					        
    					        Log.d("MIGUELL", "serverSocket == null");
    					        
    					    }
    					    socketInputStream = new DataInputStream(
    					                workerSocket.getInputStream());
    					 
    					    byte[] rvdMsgByte = new byte[socketInputStream.available()];
    					 
    					    for (int i = 0; i < rvdMsgByte.length; i++)
    					        rvdMsgByte[i] = socketInputStream.readByte();
    					 
    					    String rvdMsgTxt = new String(rvdMsgByte);
    					 
    					    texto.setText(rvdMsgTxt);
    					} catch (Exception ex) {
    					}
    				}
    			}
    		});
    I get an error from windows application that says:
    Code:
    No connection could be made because the target machine actively refused it 192.168.1.3:8222
    NOTE: I can accept the Windows code in Visual Basic too.
     
  2. Rotary Heart

    Rotary Heart Notebook Evangelist

    Reputations:
    253
    Messages:
    613
    Likes Received:
    213
    Trophy Points:
    56
    Solved

    Windows (C#)

    Code:
    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.3"), 8222);
    
                clientSocket.Send(Encoding.UTF8.GetBytes("Esto es una prueba"));
                clientSocket.Disconnect(true);
                clientSocket.Close();
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //You need to close the send code
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    Android (Java)

    Code:
    private void runTcpServer() {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(TCP_SERVER_PORT);
            Log.d("TcpServer", ss.getInetAddress()+"");
            //ss.setSoTimeout(10000);
            //accept connections
            Socket s = ss.accept();
            Log.i("TcpServer", "Receiving");
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            //BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            //receive a message
            Log.i("TcpServer", in.readLine());
            final String incomingMsg = in.readLine() + System.getProperty("line.separator");
            Log.i("TcpServer", "received: " + incomingMsg);
            runOnUiThread(new Runnable() {
                public void run() {
                    texto.append("received: " + incomingMsg);
                }
            });
    
            s.close();
        } catch (InterruptedIOException e) {
            //if timeout occurs
            e.printStackTrace();
            Log.e("TcpServer", ""+e);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("TcpServer", ""+e);
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("TcpServer", ""+e);
                }
            }
        }
    }
     
  3. Anrich

    Anrich Newbie

    Reputations:
    0
    Messages:
    0
    Likes Received:
    0
    Trophy Points:
    0
    I'm not very good with C#,I only know java. If I convert the C# code to java will still be able to work.

    Second question: Is there a way to make a java program to detect a sound from a specific program? (like when a game makes a loud dong sound)

    I'm trying to make a notification for an android app to notify me when a loading is done a quest is complete etc
     
  4. Rotary Heart

    Rotary Heart Notebook Evangelist

    Reputations:
    253
    Messages:
    613
    Likes Received:
    213
    Trophy Points:
    56
    Hello

    Yes, you should be able to use the code if you change it to java.

    I have never tried comparing sounds so I don't have any idea, but I think that it can be possible by comparing sound frequence.
     
  5. Anrich

    Anrich Newbie

    Reputations:
    0
    Messages:
    0
    Likes Received:
    0
    Trophy Points:
    0
    I'm really struggling to change it to java, I'm not asking for much but it will really help if you can show me how its done.
    BTW really love your work
     
  6. Rotary Heart

    Rotary Heart Notebook Evangelist

    Reputations:
    253
    Messages:
    613
    Likes Received:
    213
    Trophy Points:
    56
    Ok lets start with an explanation. What do you want to do? Use a java program on your pc to communicate with your android device?
     
  7. Anrich

    Anrich Newbie

    Reputations:
    0
    Messages:
    0
    Likes Received:
    0
    Trophy Points:
    0
    Okay, I'm writing a program that will that will accept a game on my PC from my phone (ignore the detect sound for now) in order to do that I must send a message from my PC to my phone when the game is ready to accept. When the phone receives the message the user can either press accept or decline that will send a message("accept" or "decline") back to the PC, I have used this code to send a message from android to PC how to Send string from Android to PC over wifi - Stack Overflow.
    And both Client and server are using java.
     
Loading...
Thread Status:
Not open for further replies.

Share This Page