)
I would like a Server out, and a Client in Android. The server seem to function but the Client ...
Server:
Using java Syntax Highlighting
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TCPDesktopServer implements Runnable{
- public static final String SERVERIP = "127.0.0.1";
- public static final int SERVERPORT = 4444;
- public void run() {
- try {
- System.out.println("S: Connecting...");
- ServerSocket serverSocket = new ServerSocket(SERVERPORT);
- while (true) {
- Socket client = serverSocket.accept();
- System.out.println("S: Receiving...");
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- String str = in.readLine();
- System.out.println("S: Received: '" + str + "'");
- } catch(Exception e) {
- System.out.println("S: Error");
- e.printStackTrace();
- } finally {
- client.close();
- System.out.println("S: Done.");
- }
- }
- } catch (Exception e) {
- System.out.println("S: Error");
- e.printStackTrace();
- }
- }
- public static void main (String a[]) {
- Thread desktopServerThread = new Thread(new TCPDesktopServer());
- desktopServerThread.start();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Android:
Start:
Using java Syntax Highlighting
- import android.app.Activity;
- import android.os.Bundle;
- public class SocketTest extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- Thread cThread = new Thread(new TCPClient());
- cThread.start();
- }
- }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Client:
Using java Syntax Highlighting
- import java.io.BufferedWriter;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- import android.util.Log;
- public class TCPClient 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 android emulator";
- 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);
- }
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Manifest:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.e2esp.socket.test"
- android:versionCode="1"
- android:versionName="1.0.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name="SocketTest">
- <uses-permission android:name="android.permission.INTERNET" />
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
I think that Ip or Port is the problem (also because the rest of the code is a post paste
:)
The Client's IP is my IP, and the Port 4444 work.


