ソケット通信 [通信]
例えば、Bitmapのやり取りの場合。
//クライアント側
ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(format, quality, os); byte[] bytedata =os.toByteArray(); Socket socket = null; BufferedOutputStream out = null; try { socket = new Socket(); socket.connect(new InetSocketAddress("192.168.0.10", 8888)); out = new BufferedOutputStream(socket.getOutputStream()); out.write(bytedata, 0, bytedata.length); if (out != null) out.close(); if (socket != null) socket.close(); } catch (Exception ex) { //TODO }
//サーバー側
ServerSocket servsock = null; Socket sock = null; BufferedInputStream in = null; try { servsock = new ServerSocket(8888); while (true) { sock = servsock.accept(); in = new BufferedInputStream(sock.getInputStream()); bitmap = BitmapFactory.decodeStream(in); in.close(); sock.close(); } } catch (IOException e) { } finally { if (servsock != null) { try { servsock.close(); } catch (IOException e) { } } }
ただ、ソケットが張れない場合、クソ重くなってしまうので、そこは工夫が要る。
【メール便配送可能】美玲さんの生活。super! 桐谷美玲 著/集英社
- ショップ: トップカルチャーnetクラブ
タグ:ソケット通信
2011-09-05 15:21
nice!(0)
コメント(0)
トラックバック(0)
コメント 0