'[펌] 안드로이드 소켓통신'에 해당되는 글 1건

  1. 2010.07.30 [펌] 안드로이드 소켓통신
04.Anddoid2010. 7. 30. 14:21
반응형
출처는 명확하지 않음-_-;; 원작성자 분에게는 죄송합니다;
앞의 2009/03/10 - [Computer Science/Android] - [펌]Android에서의 TCP/IP 통신 글의 코드와 매우 유사하긴 하지만.. 뭔가 더 간단하고 예제는 많을 수록 좋으므로;(-_-)

SocketTest.java

Java:

public class SocketTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        Thread sThread = new Thread(new TCPServer());
        Thread cThread = new Thread(new TCPClient());
       
        sThread.start();
        try {
               Thread.sleep(1000);
          } catch (InterruptedException e) { }
       
          cThread.start();
    }
}


TCPServer.java

Java:

public class TCPServer implements Runnable{
     
    public static final String SERVERIP = "127.0.0.1";
    public static final int SERVERPORT = 4444;
         
    public void run() {
         try {
              Log.d("TCP", "S: Connecting...");
             
              ServerSocket serverSocket = new ServerSocket(SERVERPORT);
              while (true) {
                 Socket client = serverSocket.accept();
                 Log.d("TCP", "S: Receiving...");
                 try {
                      BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                      String str = in.readLine();
                      Log.d("TCP", "S: Received: '" + str + "'");
                    } catch(Exception e) {
                        Log.e("TCP", "S: Error", e);
                    } finally {
                         client.close();
                         Log.d("TCP", "S: Done.");
                    }

              }
             
         } catch (Exception e) {
           Log.e("TCP", "S: Error", e);
         }
    }
}


TCPClient.java

Java:

public class TCPClient implements Runnable {

     
    public void run() {
         try {
           
           InetAddress serverAddr = InetAddress.getByName(UDPServer.SERVERIP);
           
           Log.d("TCP", "C: Connecting...");
           Socket socket = new Socket(serverAddr, TCPServer.SERVERPORT);
           String message = "Hello from Client";
               try {
                Log.d("TCP", "C: Sending: '" + message + "'");
                PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
               
                out.println(message);
                Log.d("TCP", "C: Sent.");
                  Log.d("TCP", "C: Done.");
               
             } catch(Exception e) {
                 Log.e("TCP", "S: Error", e);
                } finally {
                  socket.close();
                }
         } catch (Exception e) {
              Log.e("TCP", "C: Error", e);
         }
    }
}




AndroidManifest.xml

XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.e2esp.socket.test">

    <application android:icon="@drawable/icon">
        <activity class=".SocketTest" android:label="@string/app_name">
            <intent-filter>
                <action android:value="android.intent.action.MAIN" />
                <category android:value="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>



출처 : http://plucky.tistory.com/13
Posted by 1010