본문 바로가기

Study/Programming

안드로이드 서버 클라이언트 통신 예제

굿모닝


안녕하세요 꼬로미입니다


안드로이드 프로그래밍 네트워크 서버/클라이언트 통신 예제입니다


서버는 Java Project를 생성해 작성

클라이언트, XML은 안드로이드 프로젝트에서 작성



서버 (Java Project)

public class ServerTest {

public static void main(String[] args) throws IOException{

ServerSocket serverSocket = null;

Socket clientSocket = null;

PrintWriter out = null;

BufferedReader in = null;

serverSocket = new ServerSocket(5555);

try {

clientSocket = serverSocket.accept();

System.out.println("Client connect");

out = new PrintWriter(clientSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

while (true) {

String inputLine = null;

inputLine = in.readLine();

System.out.println("클라이언트로부터 받은 문자열 : " + inputLine);

out.println(inputLine);

if (inputLine.equals("quit"))

break;

}

out.close();

in.close();

clientSocket.close();

serverSocket.close();

} catch (Exception e){

e.printStackTrace();

}

}


}



클라이언트 (Android Project)

public class MainActivity extends Activity {

private Socket socket;

BufferedReader socket_in;

PrintWriter socket_out;

EditText input;

Button button;

TextView output;

String data;


@Override

public void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

input = (EditText) findViewById(R.id.input);

button = (Button) findViewById(R.id.button);

output = (TextView) findViewById(R.id.output);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

String data = input.getText().toString();

Log.w("NETWORK", " " + data);

if(data != null) {

socket_out.println(data);

}

}

});

Thread worker = new Thread() {

public void run() {

try {

socket = new Socket("222.104.147.163", 5555);

socket_out = new PrintWriter(socket.getOutputStream(), true);

socket_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

} catch (IOException e) {

e.printStackTrace();

}

try {

while (true) {

data = socket_in.readLine();

output.post(new Runnable() {

public void run() {

output.setText(data);

}

});

}

} catch (Exception e) {

}

}

};

worker.start();

}


@Override

protected void onStop() {

// TODO Auto-generated method stub

super.onStop();

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}



XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >


    <EditText

        android:id="@+id/input"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text=""

        android:ems="10"

        android:singleLine="true" >


        <requestFocus />

    </EditText>


    <Button

        android:id="@+id/button"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="INPUT" />


    <TextView

        android:id="@+id/output"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView" />


</LinearLayout>





실행화면


123을 입력해 INPUT 버튼을 눌러 콘솔창에서 123이란 문자열을 클라이언트로부터 받음