Reputation: 1462
Hi I'm trying to do a simple socket connection between my computer and my android application (at the moment in the emulator) on the localhost. My computer acts as a server and the android application is the client. For some weird reason I cannot get a connection. I've tried many ways to do this but i can't manage to do it. I don't get any errors/exceptions so I really don't know how to come further in my project.
Server code (desktop program):
serverSocket = new ServerSocket(6789);
while (true) {
try {
messageArea.append("\nNow acts as server and waits for mobile to connect.");
Socket socket = serverSocket.accept(); //blocking state
messageArea.append("\nMOBILE DEVICE CONNECTED.");
messageArea.append("\nconnection accepted by:\t :IP" + socket.getInetAddress() + "\t Port:" + socket.getPort() + "\t LocalPort:" + socket.getLocalAddress());
try {
createNewStreamsAndListening(socket);
} catch (ParseException ex) {
Logger.getLogger(Desktop.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SocketException e) {
}
}
The server code should not be the problem.
Client (Android application):
-MainActivity.java
public class MainActivity extends Activity
{
private Socket clientSocket = null;
private PrintWriter out;
private BufferedReader in;
boolean run = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
connect();
createStreamsAndListening();
}
public void connect(){
try {
clientSocket = new Socket("127.0.0.1", 6789);
} catch (UnknownHostException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
if (clientSocket != null) {
run = true;
}
}
public void createStreamsAndListening(){
Thread t = new Thread(
new Runnable() {
@Override
public void run(){;
try {
out = new PrintWriter(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException ex) {
System.err.println("Error in creating Streams:" + ex.toString());
return;
}
String msg = "";
while (run) {
out.println("Message from client");
out.flush();
/*try {
msg = in.readLine();
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}*/
}
}
});
t.start();
}
}
-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Connect" >
</Button>
</LinearLayout>
-AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connect"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The client doesn't even connect to the server at all. I appreciate any kind of help!
Upvotes: 3
Views: 8423
Reputation: 809
Looks like you're missing a few parts about creating a socket connection. Not well versed on Java sockets, but that new Socket call is trying to connect to '127.0.0.1', otherwise known as localhost. You need to put in the address of your server. Right now it is set to connect to itself, not the server on a desktop.
public void connect(){
try {
clientSocket = new Socket("127.0.0.1", 6789); //<--- this ip address needs
// to be fixed.
} catch (UnknownHostException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
if (clientSocket != null) {
run = true;
}
}
Upvotes: 3