-1

i have one java server socket and i connected with other java client but with android i can't i dont know why. my code: and i have in manifest the internet access

//public class MainActivity extends Activity {
private ObjectInputStream sInput; // to read from the socket
private ObjectOutputStream sOutput; // to write on the socket
public Socket socket;  
// the server, the port and the username
private String username;
private String password;
private int port = 1500;
private String server = "localhost";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connect();
}

public void connect() {
    Runnable test = new Runnable() {

        public void run() {

            InetAddress serverAddr = null;
            try {
                serverAddr = InetAddress.getByName(server);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("ClientActivity", "C: Connecting...");
            try {
                socket = new Socket(server, port);
            }

            catch (Exception ec) {
                Log.i("ERRORR", "!!!!###Error connectiong to server:" + ec);
            }

            // String msg = "Connection accepted " + socket.getInetAddress()
            // +
            // ":" +
            // socket.getPort();
            // Log.i("ERRORR", msg);

            /* Creating both Data Stream */
            try {
                sInput = new ObjectInputStream(socket.getInputStream());
                sOutput = new ObjectOutputStream(socket.getOutputStream());
            } catch (IOException eIO) {
                Log.i("ERRORR", "Exception creating new Input/output Streams: " + eIO);

            }
            Log.i("ERRORR", "CONNECTING TO THE SERVER !!!!!!");
            // creates the Thread to listen from the server

            Log.i("ERRORR", "CONNECTING TO THE SERVER !!!!!!");
        }
    };

    new Thread(test).start();
}
}

my LogCat :

06-11 16:12:20.672: I/ERRORR(2863): !!!!###Error connectiong to server:java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 1500): connect failed: ECONNREFUSED (Connection refused)
06-11 16:12:20.672: W/dalvikvm(2863): threadid=11: thread exiting with uncaught exception (group=0xb3a97ba8)
06-11 16:12:20.682: E/AndroidRuntime(2863): FATAL EXCEPTION: Thread-153
06-11 16:12:20.682: E/AndroidRuntime(2863): Process: com.example.acardsocket, PID: 2863
06-11 16:12:20.682: E/AndroidRuntime(2863): java.lang.NullPointerException
06-11 16:12:20.682: E/AndroidRuntime(2863):     at com.example.acardsocket.MainActivity$1.run(MainActivity.java:64)
06-11 16:12:20.682: E/AndroidRuntime(2863):     at java.lang.Thread.run(Thread.java:841)
m0skit0
  • 25,268
  • 11
  • 79
  • 127
xristof
  • 1
  • 2

2 Answers2

0

Try changing localhost to the ip address of your desktop computer. Remember the Android device is a separate machine, so it won't recognise "localhost" as your pc (unlike your Java client). Dont use 127.0.0.1 either, just connect the Android device to your network and use the pc's ip.

ojf
  • 163
  • 1
  • 6
0

thanks all ojf it was my fault :P

i put one null in one value object and it was fixed :P thanks all and thanks m0skit0

xristof
  • 1
  • 2