1 클라이언트와 1 서버가있는 소켓 프로그래밍을 사용하여 채팅 응용 프로그램을 개발했습니다. 2 개의 프로젝트를 실행 중입니다. 클라이언트는 1이고 서버는 1입니다. 클라이언트 응용 프로그램이 에뮬레이터에 설치되지만 아이콘을 클릭하면 실행되지 않습니다. 는 오류가 없습니다 :내 앱이 안드로이드 에뮬레이터에 설치되지만 실행에 실패하고 오류가 발생하지 않습니다.
package com.client.chat;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
import java.net.*;
import java.io.*;
public class ClientActivity extends Activity{
/** Called when the activity is first created. */
private String serverIpAddress = "10.0.2.2";
private static final int TCP_SERVER_PORT = 6000;
private Button bt;
private Socket s;
private TextView tv;
String error;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// bt=(Button)findViewById(R.id.cbtn);
tv=(TextView)findViewById(R.id.clienttext);
tcpclient();
}
private void tcpclient(){
try{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
s = new Socket(serverAddr, TCP_SERVER_PORT);
}catch(UnknownHostException e){e.printStackTrace(); error=e.toString();
tv.setText(error);
}catch(IOException e){e.printStackTrace();
error=error.toString();
tv.setText(error);
}
try{
EditText et = (EditText) findViewById(R.id.clientedit);
String str = et.getText().toString();
BufferedWriter out = null;
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String outMsg=str+System.getProperty("line.separator");
// String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(str);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
Log.d("Client", "Client sent message");
}catch(UnknownHostException e){e.printStackTrace();
error=e.toString();
tv.setText(error);
}catch(IOException e){e.printStackTrace();
error=e.toString();
tv.setText(error);
}
//accept server response
BufferedReader in=null;
try{
in= new BufferedReader(new InputStreamReader(s.getInputStream()));
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
}catch(UnknownHostException e){error=e.toString();
tv.setText(error);
}catch(IOException e){error=e.toString();
tv.setText(error);
}
finish();
}
}
이 도와주세요!
"ADB 로그 캣"을 실행 해보십시오, 또는 이클립스에서 실행중인 경우 로그 캣 탭을 확인,이 네 .. 마무리 고맙습니다 알 D 문제를 생성 한 – crobicha