Android Story

[ Android ] Socket 통신 - 이미지 전송

WhiteDuck 2016. 1. 19. 11:43

Socket 통신으로 객체 직렬화(byte[])를 사용하여 데이터를 보내고자 한다. 소스는 아래와 같다.


 Test.java in Cilent_PC 


package transeTest;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.net.Socket;

public class Test {

public static void main(String[] args) {

SocketClient s = new SocketClient("192.168.1.25", 8000);

s.setPath("/Users/myo/Pictures");

s.read();

s.close();

}

}

class SocketClient {

private String path;

private Socket socket;

public SocketClient(String ip,int port) {

try {

// 1. socket open

socket = new Socket(ip, port);

}

catch (IOException e) {

}

}

public void setPath(String path) {

/**

                   * 이미지를 저장할 path 설정

                   */

this.path = path;

}

public void read() {

try {

System.out.println("Connect");

// 1. inputSteam set

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

System.out.println("reading name");

String name = ois.readUTF();

System.out.println("reading picture");

byte[] data = (byte[]) ois.readObject();

ois.close();

System.out.println("reading_end");

// 2. outputString set - File

System.out.println("Name : " + name);

FileOutputStream fos = new FileOutputStream(path+"/"+name);

System.out.println("writing");

fos.write(data);

fos.flush();

System.out.println("written");

}

catch (IOException e) {

System.out.println("IOException");

}

catch (ClassNotFoundException e) {

System.out.println("ClassNotFoundException");

}

}

public void close() {

try {

socket.close();

}

catch (IOException e) {

}

}

}





  MainActivity.java in Server_Android



private void startTransfer() {

/**

         * TCP 소켓 전송

         */

// 1) 소켓 오픈

SocketServer socketServer = new SocketServer(SERVERSIDE_WPORT);

// 2) 이미지 파일 설정

       // < !-- filePathManager is cumstomObject --! >

String filename = filePathManager.get(fileUuid)[0];

// [ 0 : filename, 1 : filepath ]

String filepath = filePathManager.get(fileUuid)[1];

socketServer.setPath(filename, filepath);

socketServer.start();

try {

Thread.sleep(5000);

}

catch (InterruptedException e) {

e.printStackTrace();

}

if(socketServer.accept())

           Toast.makeText(getApplicationContext(), "accepting", Toast.LENGTH_LONG).show();

}




!!! Socket연결을 메인쓰레드에서 돌게 하지 않는다.

SocketServer.java in Server_Android


import android.util.Log;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

public class SocketServer extends Thread {

private ServerSocket ss;

private Socket s;

private String name;

private String path;

public SocketServer(int port) {

try {

// 1. ServerSocket open

ss = new ServerSocket(port);

Log.d("ss","accept");

}

catch (IOException e) {

Log.d("SocketServer","Error : IOException");

}

}

public boolean accept() {

return s !=null;

}

public void setPath(String name, String path) {

/**

         * 전송할 이미지 path 설정

         */

this.name = name;

this.path = path;

}

public void run() {

try {

s = ss.accept();

// 1. file read

FileInputStream fis = new FileInputStream(path);

byte[] data = new byte[fis.available()];

fis.read(data);

// 2. send data

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

oos.writeUTF(name);

oos.flush();

oos.writeObject(data);

oos.flush();

oos.close();

}

catch (IOException e) {

Log.d("SocketClient","Error : IOException");

}

}

public void close() {

try {

s.close();

}

catch (IOException e) {

Log.d("SocketServer","Error : IOException");

}

}

}







AndroidManifest.xml in Server_Android


<uses-permission android:name="android.permission.INTERNET" />



반응형