Hello what i am doing is creating a program that will let me allow two
android phones to communicate over TCP. The code that I have is
Server:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TCPServer extends Activity {
/** Called when the activity is first created. */
public static TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
tv = new TextView(this);
tv.setText("Main - server");
setContentView(tv);
Thread sThread = new Thread(new TCPServerTest());
sThread.start();
}
}
class TCPServerTest implements Runnable{
public static final String SERVERIP = "127.0.0.1";
public static final int SERVERPORT = 4444;
public void run() {
try {
Log.d("TCP", "S: Connecting...");
ServerSocket serverSocket = new ServerSocket
(SERVERPORT);
while (true) {
Socket client = serverSocket.accept();
Log.d("TCP", "S: Receiving...");
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(client.getInputStream()));
String str = in.readLine();
Log.d("TCP", "S: Received: '" + str + "'");
TCPServer.tv.setText(str);
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
client.close();
Log.d("TCP", "S: Done.");
}
}
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
}
}
}
Client:
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TCPClient extends Activity {
/** Called when the activity is first created. */
public TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
tv = new TextView(this);
tv.setText("Main - client");
setContentView(tv);
Thread cThread = new Thread(new TCPClientTest());
cThread.start();
}
}
class TCPClientTest implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName
("127.0.0.1");
Log.d("TCP", "C: Connecting...");
Socket socket = new Socket(serverAddr, 4444);
String message = "Hello from Client";
try {
Log.d("TCP", "C: Sending: '" + message + "'");
PrintWriter out = new PrintWriter( new BufferedWriter
( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
}
The problem is when I run the client on the emulator and then the
server on the phone the client produces the following error. Note the
server is running first and both programs have Internet permissions in
the mainifest.
D/TCP ( 700): C: Connecting...
E/TCP ( 700): C: Error
E/TCP ( 700): java.net.ConnectException: /127.0.0.1:4444 -
Connection refused
E/TCP ( 700): at
org.apache.harmony.luni.net.PlainSocketImpl.connect
(PlainSocketImpl.java:237)
E/TCP ( 700): at
org.apache.harmony.luni.net.PlainSocketImpl.connect
(PlainSocketImpl.java:199)
E/TCP ( 700): at java.net.Socket.startupSocket(Socket.java:734)
E/TCP ( 700): at java.net.Socket.<init>(Socket.java:245)
E/TCP ( 700): at com.budasmj.TCPClientTest.run(TCPClient.java:
41)
E/TCP ( 700): at java.lang.Thread.run(Thread.java:1058)
My aim is to use two phone where a phone is the server and the client
is also on a phone. Any help would be much appreciated.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en