package com.example.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.AbsoluteLayout;
import android.widget.Toast;
public class Start_Activity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_);
AbsoluteLayout start_layout = (AbsoluteLayout)findViewById(R.id.start);
start_layout.setBackgroundResource(R.drawable.start_view);
Thread thread = new Thread(null, new TCPClient("zz"));
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_start_, menu);
return true;
}
}
---------------------------------------------Client-------------------------------------------
package com.example.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.util.Log;
public class TCPClient implements Runnable {
private static final String SERVERIP="210.118.64.177";
private static final int Port=9000;
private String msg;
private String return_msg;
public TCPClient(String _msg){
this.msg = _msg;
}
public void run() {
// TODO Auto-generated method stub
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Socket socket = new Socket(serverAddr, Port);
// Socket socket = new Socket(SERVERIP, Port);
Log.d("TCP", "C: Connecting...");
try {
Log.d("TCP", "C: Sending: '" + msg + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(msg);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return_msg = in.readLine();
Log.d("TCP", "C: Server send to me this message -->" + return_msg);
} catch(Exception e) {
Log.e("TCP", "C: Error1", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error2", e);
}
}
}
'Android' 카테고리의 다른 글
버튼 누름 효과 (0) | 2013.01.25 |
---|---|
Splash 효과 (0) | 2013.01.16 |
윈도우에서 안드로이드 NDK 개발환경 설치 (0) | 2013.01.01 |
안드로이드 개발 팁 (0) | 2012.12.29 |
Activity 화면 전환 효과 (0) | 2012.12.25 |