안드로이드로 서버를 만들어 갤럭시탭으로 포팅을 하고

 

안드로이드로 클라이언트를 만들어 갤럭시s로 포팅을 해서

 

서버가 돌아가고 있을때 클라이언트 프로그램이 실행되면 간단한 문자를 서버에서 보내서 클라이언트를 그 문자를 toast하게 되는데요

 

소스문제인지 다른 문제인지는 모르겟지만 안되더라고요..(eclipse에서 시뮬레이터에서 서버를 돌리고 클라이언트만 갤럭시s에 포팅해서 시도해봄)

 

추후에 사진파일을 전송시켜야 하는데 일단 소스 첨부해볼게용

 

 

 

 

먼저 클라이언트

 

package com.tcp;                                                                                                                                            

 

import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class NameClient extends Activity{
 
 private static String msg;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if(msg!=null){
         Toast t = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
            t.show();
        }
    }
   
 public static void main(String[] args) throws Exception {
  Socket s = null;    
  InputStream is = null;                       
        BufferedReader br = null;
         try {
          s = new Socket("192.168.123.2", 8988);   //우리집 pc아이피와 사용할 포트넘버
          is = s.getInputStream();       // Socket으로부터 얻은 InputStream                                        
          br = new BufferedReader(new InputStreamReader(is));
          msg = br.readLine(); 
          System.out.println(msg);                                                                                                                            
         }
         catch(IOException ie) {
          ie.printStackTrace();          
         }
         finally {
          try {
           if (is != null)
            is.close();
           if (br != null)
            br.close();
           if (s != null)
            s.close();
          }
          catch(Exception e) {
           e.printStackTrace();
          }
         }
 }
}

 

 

그다음 서버

 

package com.tcp;

import android.app.Activity;
import android.os.Bundle;
import java.io.*;
import java.net.*;

public class NameServer extends Activity {
    /*클라이언트에게 메시지를 날리는 서버
     * Tcp포트를 listen하여 클라이언트가 tcp 연결을 하면 메시지를 클라이언트에게 보낸다.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
   public static void main(String[] args)
   {
    ServerSocket ss=null;
    Socket s = null;
    OutputStream os = null;
    PrintStream ps= null;
   
    try{
      ss = new ServerSocket(8988);
      s = ss.accept();
      os = s.getOutputStream();
      ps = new PrintStream(os);
      ps.println("가자 메시지야");
      ps.flush();
      }
      catch(IOException ie){
       ie.printStackTrace();
      }
    finally{
     try{
      if(ss!=null)
       ss.close();
      if (os != null)
       os.close();
      if(ps!= null)
       ps.close();
      if(s!=null)
       s.close();
      }
     catch(Exception e){
      e.printStackTrace();
     }
    }
    }
}

 

이상입니다 조언 부탁드립니다.