HELP..... in socket programming

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

HELP..... in socket programming

Postby b3n_hysteria » Sun May 10, 2009 2:20 pm

first of all.. i'm really sorry.. my english not very well..
i attach image my problem...

i'm use example from gamefaroq(thank's bro)

this TCP server :
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class TCPDesktopServer implements Runnable{

public static final String SERVERIP = "127.0.0.1";
public static final int SERVERPORT = 1234;

public void run() {
try {
System.out.println("TCP S: Connecting...");

ServerSocket serverSocket = new ServerSocket(SERVERPORT);
while (true) {
Socket client = serverSocket.accept();
System.out.println("TCP S: Receiving...");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
System.out.println("TCP S: Received: '" + str + "'");
} catch(Exception e) {
System.out.println("TCP S: Error");
} finally {
client.close();
System.out.println("TCP S: Done.");
}

}

} catch (Exception e) {
System.out.println("TCP S: Error");
}
}


public static void main (String a[]) {
new Thread(new TCPDesktopServer()).start() ;
}
}

the client :
package Socket.Test;

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 {
Socket TCPServer = new Socket();
@Override
public void run() {
try {

InetAddress serverAddr = InetAddress.getByName("127.0.0.1");//TCPServer.SERVERIP

Log.d("TCP", "C: Connecting...");
;
Socket socket = new Socket(serverAddr, TCPServer.getPort());
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);
}
}

}

package Socket.Test;

import android.app.Activity;
import android.os.Bundle;

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 sThread = new Thread(new UDPServer());
// Thread cThread = new Thread(new UDPClient());
// Thread sThread = new Thread(new TCPServer());
Thread cThread = new Thread(new TCPClient());

/*sThread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }*/

cThread.start();

}
}


the manifest :
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Socket.Test" android:versionCode="1" android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SocketTest" 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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

can you help me??.........
thank you...
Attachments
untitled1.JPG
untitled1.JPG (66.42 KiB) Viewed 3271 times
b3n_hysteria
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu May 07, 2009 1:38 pm

Top

Postby padde » Sun May 10, 2009 4:49 pm

use localhost instead of 127.0.0.1
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby b3n_hysteria » Tue May 12, 2009 1:14 pm

first.. thx for reply..

but i don't understand which one i must replace with localhost.. so i try 3 way.. but it's same like before..
this 3 error message..

second..

now i success use socket programming, when i use ip from my vm ware..
(expect 127.0.0.1) if i use another ip.. it's work.. so.. this is issue??bug?? or something wrong with my computer??or source??

thx..
Attachments
client and server (l).JPG
client use localhost
and server use localhost
client and server (l).JPG (69.63 KiB) Viewed 3219 times
client (l), and server (127.0.0.1).JPG
client use localhost
and server use 127.0.0.1
client (l), and server (127.0.0.1).JPG (69.43 KiB) Viewed 3218 times
client (127.0.0.1)  and server (l).JPG
client use 127.0.0.1
and server use 127.0.0.1
client (127.0.0.1) and server (l).JPG (82.59 KiB) Viewed 3218 times
b3n_hysteria
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu May 07, 2009 1:38 pm

Top

Return to General

Who is online

Users browsing this forum: No registered users and 3 guests