Krzysztof Kaczor
Krzysztof Kaczor

Reputation: 5588

Problem with using socket to connect with PC

I'm trying to connect my phone with my notebook using WiFi. Both devices are connected to one network. Notebook has IP 192.168.1.35 so i tried to connect with that IP through smart phone (firewall is off). Here's the code:

package org.me.androidapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        try {
            Toast.makeText(this.getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
            Socket socket = new Socket("192.168.1.35", 9999);
            PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
            out.println("dupa");
            socket.close();
        } catch (UnknownHostException ex) {
            Toast.makeText(this.getApplicationContext(), "Nie odnaleziono hosta", Toast.LENGTH_LONG).show();
            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Toast.makeText(this.getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

Im using this code (written in python) as server:

import socket

_connector = None
_running = True

_host = 'localhost'
_port = '9999'
_maxClient = 999

debug = True
_policyFile = '<?xml version="1.0" encoding="UTF-8"?><cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd"><allow-access-from domain="*" to-ports="*" secure="false" /><site-control permitted-cross-domain-policies="master-only" /></cross-domain-policy>'

## Trace debugging messages.
#  @param aString String to be printed.
def printd( aString ):
    if debug:
        print aString

_connector = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
_connector.bind ( ( str(_host), int(_port) ) )
_connector.listen ( int(_maxClient) )
_running = True

while _running:
  printd('Running on port ' + _port + ' ... ')
  channel, details = _connector.accept()

  if _running:
      printd( 'New connection with: ' + str(details) )
      channel.setblocking( 1 )
      recvData = channel.recv(2000)

      if("policy-file-request" in recvData):
         channel.send(_policyFile)

      printd( 'host: ' + str(details[0]) )
      printd( 'port: ' + str(details[1]) )
      printd( 'port: ' + str(int(details[1])) )

      channel.close()

Im testing this app on my phone because im not sure how to configure emulator (any help? :) ). Phone shows error "Java.Net.ConnectionError localhost/192.168.1.1:9999 - connection refused". Im wondering also why it shows IP 192.168.1.1...

Please help, Chris

Upvotes: 0

Views: 363

Answers (1)

Dan D.
Dan D.

Reputation: 74655

your python code is binding to localhost which means it's not listening on the network but only on loopback thus you should change it to have it listen on the network address (i.e. 192.168.1.35) of the computer rather than on 127.0.0.1

Upvotes: 5

Related Questions