พอดีผมจะทำให้ Android ติดต่อกับ Android ง่ายๆคือ ให้เป็น Server ก่ะ Client แล้วมันติดตรง Server มันรับไม่ผ่านอ่ะครับ

นี่โค้ดฝั่ง Server ที่มีปัญหาครับ

package android.server.androidserver;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements Runnable {
private TextView mTextView ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
}
public void run () {
try {
ServerSocket serverSocket = new ServerSocket(12345);
Socket client = null;
System.out.println(“Server Started”);
String message = “Goodbye”;

while (true){
client = serverSocket.accept();
System.out.println(“Client accept : ” + client);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
System.out.println(“read: ” + str);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
client.getOutputStream())), true);

out.println(message);
out.close();
in.close();
} catch (Exception e) {
client.close();

System.out.println(“close”);
System.out.println(e.getMessage());
e.printStackTrace();
}
}
} catch (Exception e ) {
System.out.println(e.getMessage());
}
}

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

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
โค้ดฝั่ง Client รันได้ไม่มีปัญหา

package com.kku.cs;
import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidClient extends Activity {
private final String DEBUG_TAG = “AndroidClient”;
private TextView mTextView = null;
private EditText mEditText = null;
private Button mButton = null;
private Socket socket = null;
/** Called when the activity is first created. */
//** kmutnb */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.Button01);
mTextView = (TextView) findViewById(R.id.TextView01);
mEditText = (EditText) findViewById(R.id.EditText01);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String message = mEditText.getText().toString() + “rn”;
try {
socket = new Socket (“10.30.195.106”, 12345);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
BufferedReader br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String msg = br.readLine();
if (msg !=null){
mTextView.setText(mTextView.getText() +”n”+msg);
} else{
mTextView.setText(“Data eror !”);
}
out.close();
br.close();
socket.close();
} catch (Exception e){
Log.e(DEBUG_TAG, e.toString());

}
}
});
}
}

ช่วยผมทีครับ