ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [socket] 서버 <-> 클라이언트 간단예제
    ANDROID 2018. 5. 9. 17:38
    반응형

    목적 : android 에서 보내는 socket 데이터가 서버에 정상적으로 들어오는지 디버깅용으로 만들었습니다.

    기능 : 데이터 echo 밖에 없습니다.



    1. socket server [리눅스 컴파일]


    리눅스에 -java 를 설치후 진행



    java -> class 컴파일

    javac -d . TCPServer.java


    컴파일된 class 파일 실행

    java -cp . TCPServer


    ==========================================================================================


    import java.io.BufferedReader;

    import java.io.BufferedWriter;

    import java.io.InputStreamReader;

    import java.io.OutputStreamWriter;

    import java.io.OutputStream;

    import java.io.DataOutputStream;

    import java.io.PrintWriter;

    import java.net.ServerSocket;

    import java.net.Socket;

    import java.io.BufferedOutputStream;

    public class TCPServer implements Runnable {

     

        public static final int ServerPort = 5555;

        @Override

        public void run() {

     

            try {

                System.out.println("S: Connecting...");

                ServerSocket serverSocket = new ServerSocket(ServerPort);

     

                while (true) {

                    Socket client = serverSocket.accept();

                    System.out.println("S: Receiving...");

                    try {

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

                        String str = in.readLine();

                        System.out.println("S: Received: '" + str + "'");

                        System.out.println("Server Received.len  " + String.valueOf(str.length()));

                         

                        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);

                        out.println("Server Received " + str);


                    } catch (Exception e) {

                        System.out.println("S: Error");

                        e.printStackTrace();

                    } finally {

                        client.close();

                        System.out.println("S: Done.");

                    }

                }

            } catch (Exception e) {

                System.out.println("S: Error");

                e.printStackTrace();

            }

        }

     

        public static void main(String[] args) {

     

            Thread desktopServerThread = new Thread(new TCPServer());

            desktopServerThread.start();

     

        }

     

    }


    ==========================================================================================


    2. socket client [ Android Studio ]



    ==========================================================================================


    private void socketFnc(final byte[] temp, final int step) {

    Log.e("socketFnc", "sss");

    //Send To Server Socket data

    //http://www.codejava.net/java-se/networking/java-socket-client-examples-tcp-ip

    new Thread() {

    public void run() {

    Log.e("socketFnc", "sss");

    try {

    byteToString = "";

    byteToString2 = "";

    Socket socket = new Socket("서버 IP", 5555);

    OutputStream os = null;

    OutputStreamWriter osw = null;

    BufferedWriter bw = null;

    //Send

    OutputStream output = socket.getOutputStream();

    //Log.e("bbb", "보낸 데이터 : " + bytesToHex(temp));

    output.write(temp);

    output.flush();

    StringBuilder sb = new StringBuilder();

    for (byte b : temp) {

    sb.append(String.format("0x%02X ", b));

    }

    Log.e("sb.length()", String.valueOf(sb.length()));

    int ff = sb.length() / 200;

    for (int x = 0; x < ff; x++) Log.e("bbb", "보낸 데이터 : " + sb.toString().substring(x * 200, x * 200 + 200));

    socket.close();

    }

    catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    .start();

    }//socketFnc


    ==========================================================================================







    반응형

    댓글

Designed by Tistory.