ورود

View Full Version : مشکل کد چیه؟؟؟؟؟؟؟؟؟



s a h a r70
چهارشنبه 10 دی 1393, 22:10 عصر
سلام.من یه کد اماده واسه سرور و کلاینت گرفتم که روش کارش اینطوریه که کلاینت تکست رو تایپ میکنه و دکمه رو فشار میده و توی سرور نمایش داده میشه..
برنامه نصب میشه ,دو دستگاه هم به هم متصل میشن و سرور به کلاینت ای پی میده ولی وقتی تکست رو تایپ میکنم و دکمه رو فشار میدم توی سرور نشون داده نمیشه...

سرور:
public class Server extends Activity {

private ServerSocket serverSocket;

Handler updateConversationHandler;

Thread serverThread = null;

private TextView text;

public static final int SERVERPORT = 6000;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.server);

text = (TextView) findViewById(R.id.text2);

updateConversationHandler = new Handler();

this.serverThread = new Thread(new ServerThread());
this.serverThread.start();

}

@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

class ServerThread implements Runnable {

public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {

try {

socket = serverSocket.accept();

CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();

} catch (IOException e) {
e.printStackTrace();
}
}
}
}

class CommunicationThread implements Runnable {

private Socket clientSocket;

private BufferedReader input;

public CommunicationThread(Socket clientSocket) {

this.clientSocket = clientSocket;

try {

this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream ()));

} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {

while (!Thread.currentThread().isInterrupted()) {

try {

String read = input.readLine();

updateConversationHandler.post(new updateUIThread(read));

} catch (IOException e) {
e.printStackTrace();
}
}
}

}

class updateUIThread implements Runnable {
private String msg;

public updateUIThread(String str) {
this.msg = str;
}

@Override
public void run() {
text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}
}
}



کلاینت:
public class Client extends Activity {

private Socket socket;

private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "192.168.43.1";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);

new Thread(new ClientThread()).start();
}

public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

class ClientThread implements Runnable {
@Override
public void run() {

try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr,SERVERPORT);

} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}
}

s a h a r70
چهارشنبه 10 دی 1393, 23:20 عصر
لطفا کمک کنید

s a h a r70
پنج شنبه 11 دی 1393, 09:58 صبح
Please.........

s a h a r70
جمعه 12 دی 1393, 07:45 صبح
یه کمک..............

rubiks.kde
جمعه 12 دی 1393, 12:07 عصر
قبلا در این مورد چند مثال گذاشته شده است.
در زمان ایجاد پست هم از عناوین مناسب تری استفاده کنید.