I've got a simple Java server running on my laptop that opens a ServerSocket
. A simple client for the android tries to connect to this server (using my laptop's IP address and the port I specified in the ServerSocket
), and send it a string. The client hangs on:
client = new Socket(IP_ADDRESS, DEST_PORT);
I'm using a Samsung Galaxy S3 connected to my laptop. Note that I try to establish the connection from the client side in an AsyncTask
.
I've been hammering away at this for a while, and have the feeling I'm missing something simple.
So the question is: How do I get my android phone to recognise/see the PC? Thanks in advance!
Here is my Java server code:
// Imports etc
public class Main
{
private static final int PORT = 4444;
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args)
{
try
{
serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost());
System.out.println("IP: " + serverSocket.getInetAddress() + " Port: " + serverSocket.getLocalPort());
} catch (IOException e)
{
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true)
{
try
{
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex)
{
System.out.println("Problem in message reading");
}
}
}
}
Here is my android client code:
// Imports etc
public class SimpleClientActivity extends Activity
{
private EditText textField;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textField = (EditText) findViewById(R.id.editText1); // reference to the text field
button = (Button) findViewById(R.id.button1); // reference to the send button
// Button press event listener
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
new ConnectToServerTask().execute(textField);
}
});
}
}
Here is my AsyncTask code:
// Imports etc
public class ConnectToServerTask extends AsyncTask<View, Integer, Socket>
{
private static final String IP_ADDRESS = "192.168.56.1"; // Toshiba laptop
private static final int DEST_PORT = 4444;
private EditText mTextField;
/**
* Store provided views (used later in onPostExecute(...)).
*
* Create socket to communicate with server (blocking call).
*/
protected Socket doInBackground(View... params)
{
// Store provided views.
if (params.length != 1)
throw new IllegalArgumentException();
mTextField = (EditText) params[0];
// Create socket.
Socket client = null;
try
{
client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return client;
}
/**
* Write to server.
*/
protected void onPostExecute(Socket client)
{
try
{
PrintWriter printwriter;
String messsage;
messsage = mTextField.getText().toString(); // get the text message on the text field
mTextField.setText(""); // Reset the text field to blank
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Thanks to @zapl, check the IP address of the PC. Had 192.168.56.1 when the correct address was 192.168.1.56.