안드로이드 개발 질문/답변
(글 수 45,052)
이 소스 짧게 나마 분석 해주실분 있나여? ㅠㅠ
공부하고 있는데 이해가 너무 안되가지고요
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SocketEx extends Activity implements View.OnClickListener{
private final static String BR=System.getProperty("line.separator");
private final static String IP="192.168.1.181";
private SocketEx current;
private TextView lblReceive;
private EditText edtSend;
private Button btnSend;
private Socket socket;
private InputStream in;
private OutputStream out;
private final Handler handler=new Handler();
private String txtReceive;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
current=this;
RelativeLayout layout=new RelativeLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
setContentView(layout);
lblReceive=new TextView(this);
lblReceive.setId(1);
lblReceive.setText("");
lblReceive.setTextSize(16.0f);
lblReceive.setTextColor(Color.rgb(0,0,0));
RelativeLayout.LayoutParams param1=new RelativeLayout.LayoutParams(320,400);
lblReceive.setLayoutParams(param1);
layout.addView(lblReceive);
edtSend=new EditText(this);
edtSend.setId(2);
edtSend.setText("",TextView.BufferType.NORMAL);
RelativeLayout.LayoutParams param2=new RelativeLayout.LayoutParams(200,50);
param2.addRule(RelativeLayout.BELOW,1);
edtSend.setLayoutParams(param2);
layout.addView(edtSend);
btnSend=new Button(this);
btnSend.setText("전송");
btnSend.setOnClickListener(this);
RelativeLayout.LayoutParams param3=new RelativeLayout.LayoutParams(200,50);
param3.addRule(RelativeLayout.BELOW,1);
param3.addRule(RelativeLayout.RIGHT_OF,2);
btnSend.setLayoutParams(param3);
layout.addView(btnSend);
(new Thread() {public void run(){
try{
connect (IP,8080);
} catch(Exception e) {
}
}}).start();
}
private void connect(String ip,int port){
int size;
byte[] w=new byte[1024];
txtReceive="";
try{
socket=new Socket (ip,port);
in=socket.getInputStream();
out=socket.getOutputStream();
while(socket !=null && socket.isConnected()) {
size=in.read(w);
if(size<=0)continue;
txtReceive=new String(w,0,size,"UTF-8");
handler.post(new Runnable(){
public void run(){
lblReceive.setText(
lblReceive.getText() +txtReceive+BR);
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
public void run() {
SocketEx.showDialog(current,"","통신 에러입니다.");
}
});
}
}
public void onClick(View v) {
if(v==btnSend) {
try {
if(socket !=null&&socket.isConnected()) {
String str="김근태"+edtSend.getText().toString();
byte [] w=edtSend.getText().toString().getBytes("UTF8");
out.write(w);
out.flush();
edtSend.setText("",TextView.BufferType.NORMAL);
}
} catch (Exception e) {
handler.post(new Runnable(){
public void run(){
SocketEx.showDialog(current,"","통신 에러입니다.");
}
});
}
}
}
public static void showDialog(final Activity activity, String title, String text) {
AlertDialog.Builder ad=new AlertDialog.Builder(activity);
ad.setTitle(title);
ad.setMessage(text);
ad.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
activity.setResult(Activity.RESULT_OK);
}
});
ad.create();
ad.show();
}
}
2012.07.27 18:10:36
안드로이드 소켓통신채팅 클라이언트쪽 소스네요.
lblReceive=new TextView(this);
lblReceive.setId(1);
lblReceive.setText("");
lblReceive.setTextSize(16.0f);
lblReceive.setTextColor(Color.rgb(0,0,0));
RelativeLayout.LayoutParams param1=new RelativeLayout.LayoutParams(320,400);
lblReceive.setLayoutParams(param1);
....
....
btnSend.setLayoutParams(param3);
layout.addView(btnSend);
//레이아웃 code로 만든거고, 텍스트뷰, 에디트텍스트창, 버튼 세가지.
(new Thread() {public void run(){
try{
connect (IP,8080);
} catch(Exception e) {
}
}}).start();
// 스레드 시작, ip주소랑 8080포트로 connect
//connect 함수는 소켓객체 생성하고, input, output스트림 얻고, while안에서 수신해주고, 텍스트뷰에 뿌려주고
// Click 하면 에디트창에 입력한 내용 송신, 'String str="김근태"+edtSend.getText().toString();' 는 쓸데없는 내용이네요
// socket 연결 안됐을때 에러처리는 showDialog




소켓 통신을 이용한 채팅인가 본데요?
소스 분석은 본인이 해야죠...