안녕하세요(__) 책을 구매해서 안드로이를 공부하고있는 학생입니다..
이 소스는 제가 구매한 책에 나와있는 소켓 채팅 소스입니다.
다름이아니라 이 소스가 전송은 잘됩니다.
근대 저는 나: 어쩌고저쩌고 상대방: 어쩌고저쩌고 로 되는 채팅을 원했는데..
책에는 그런 부분이 잘 설명이 안되어있더라구요...
밑에 소스는 그냥 나,상대방 표시없이 어쩌고저쩌고만 출력이되서
제가 아무리해봐도 위와같이 원하는되로 안되서 이렇게 글을 올립니다..
"나"+ 와 "상대방"+ 를 어디에 삽입 시키는지 조언 좀 부탁드릴게요 선배님들...
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();//핸들러 (6)
private String txtReceive;//수신 텍스트
//초기화
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
current=this;
//레이아웃의 생성 (8)
RelativeLayout layout=new RelativeLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
setContentView(layout);
//수신 라벨의 생성
lblReceive=new TextView(this);
lblReceive.setId(1);// (9)
lblReceive.setText("");
lblReceive.setTextSize(16.0f);
lblReceive.setTextColor(Color.rgb(0,0,0));
RelativeLayout.LayoutParams param1=// (9)
new RelativeLayout.LayoutParams(320,400);
lblReceive.setLayoutParams(param1);
layout.addView(lblReceive);
//송신 텍스트 박스의 생성
edtSend=new EditText(this);
edtSend.setId(2);// (9)
edtSend.setText("",TextView.BufferType.NORMAL);
RelativeLayout.LayoutParams param2=// (9)
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=// (9)
new RelativeLayout.LayoutParams(200,50);
param3.addRule(RelativeLayout.BELOW,1);
param3.addRule(RelativeLayout.RIGHT_OF,2);
btnSend.setLayoutParams(param3);
layout.addView(btnSend);
//스레드 생성 (2)
(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 {
//소켓 접속 (3)
socket=new Socket(ip,port);
in =socket.getInputStream();
out=socket.getOutputStream();
//수신 반복 루프 (4)
while (socket!=null && socket.isConnected()) {
//데이터 수신 (5)
size=in.read(w);
if (size<=0) continue;
txtReceive=new String(w,0,size,"UTF-8");
//핸들러에 의한 사용자 인터페이스 조작 (6)
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 {
//데이터 송신 (7)
if (socket!=null && socket.isConnected()) {
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();
}
}




저도 고수는 아니지만 소켓통신을 구현하는거 공부중이라 ㅋ
이부분에서 데이터를 보내는데
//데이터 송신 (7)
if (socket!=null && socket.isConnected()) {
byte[] w=edtSend.getText().toString().getBytes("UTF8");<==========endSend라는 EditText로 부터 글자를 가져오고요
out.write(w); <==============특히 이부분이 보내는 부분이네요
out.flush();
edtSend.setText("",TextView.BufferType.NORMAL);
결론은 w라는거에 나:를 추가하면 되겠네요
그런데 무슨 책인가요 ㅋㅋ 제가 본거에는 소켓통신이 안나와있어서 인터넷으로만 검색해보고 있는데 ㅠ