Server 소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import java.lang.*; import java.io.*; import javax.obex.*; import javax.microedition.io.*; /** * Create a server that will respond to GET requests for the * default vCard. */ public class OBEXServer extends ServerRequestHandler { public OBEXServer() { } public static void main(String[] args) { SessionNotifier notify = null; try { notify = (SessionNotifier) Connector.open("tcpobex://:5005"); } catch (IOException e) { System.out.println("Unable to create notifier"); return; } // Process each request for (;;) { try { // Wait for a client to connect Connection server = notify.acceptAndOpen(new OBEXServer()); } catch (IOException e) { System.out.println("Transport Error"); } } } public int onGet(Operation op) { try { // Get the type of object that is being // requested HeaderSet head = op.getReceivedHeaders(); String type = (String) head.getHeader(HeaderSet.TYPE); // Determine if it is a vCard or not if ((type == null) || (!type.equals("text/vCard"))) { return ResponseCodes.OBEX_HTTP_FORBIDDEN; } DataOutputStream out = op.openDataOutputStream(); // Open the file to read InputConnection conn = (InputConnection) Connector.open("file://BobSmith.vcd"); // Return the name of the vCard head = createHeaderSet(); head.setHeader(HeaderSet.NAME, "BobSmith.vcd"); op.sendHeaders(head); // Read from the file DataInputStream in = conn.openDataInputStream(); int data; while ((data = in.read()) != -1) { out.write((byte) data); } // Close the open connections in.close(); out.close(); op.close(); return ResponseCodes.OBEX_HTTP_OK; } catch (IOException e) { return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; } } } | cs |
Client 소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import java.lang.*; import java.io.*; import javax.obex.*; import javax.microedition.io.*; /** * This is a sample application that uses the OBEX API defined in this chapter * to CONNECT and then GET the server's * vCard. */ public class OBEXClient { public static void main(String[] args) { ClientSession conn = null; StreamConnection file = null; // Connect to the server try { conn = (ClientSession) Connector.open("tcpobex://12.123.155.12:5005"); } catch (IOException e) { System.out.println("Unable to connect to server"); return; } // Issue a CONNECT command to connect to the OBEX // server try { HeaderSet response = conn.connect(null); if (response.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) { System.out.println("Request Failed"); conn.close(); return; } } catch (IOException e) { System.out.println("Transport failed"); return; } // Issue a GET command to the OBEX server and // write the object to a file try { // Set the name of the object to retrieve HeaderSet head = conn.createHeaderSet(); head.setHeader(HeaderSet.TYPE, "text/vCard"); // Issue the request Operation op = conn.get(head); // Get the correct streams to process the request InputStream in = op.openInputStream(); // Open the file to write to head = op.getReceivedHeaders(); file = (StreamConnection) Connector.open((String) head.getHeader(HeaderSet.NAME)); OutputStream out = file.openOutputStream(); // Read and write the data int data = in.read(); while (data != -1) { out.write((byte) data); data = in.read(); } // End the operation out.close(); file.close(); in.close(); op.close(); // DISCONNECT from the server conn.disconnect(null); } catch (IOException e) { System.out.println("Unable to read/write file"); } finally { // Close the transport layer connection try { conn.close(); } catch (Exception e) { } } } } | cs |
'프로그래밍 > Java, Android' 카테고리의 다른 글
(안드로이드) Bluetooth를 이용한 Image 파일 전송하는 Simple Application (0) | 2016.07.10 |
---|---|
(안드로이드) ImageView와 onTouchEvent를 이용한 Simple Application (0) | 2016.07.10 |
(이클립스) 유용한 단축키 (0) | 2016.07.10 |
(이클립스) javax 라이브러리 추가하는 방법 (0) | 2016.07.10 |
(Java) jsr 확인 하는 곳 (0) | 2016.07.10 |