Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. s = new Server();
  2. mythread = new Thread(runnable);
  3. mythread.start();
  4.  
  5. public class Server implements Runnable{
  6. ServerSocket servsock;
  7. String fname;
  8.  
  9. @Override
  10. public void run() {
  11. int filesize=6022386;
  12.  
  13. long start = System.currentTimeMillis();
  14. int bytesRead;
  15. int current = 0;
  16.  
  17. try {
  18. // create socket
  19. servsock = new ServerSocket(5554);
  20. while (true) {
  21. System.out.println("Waiting...");
  22. Socket sock = servsock.accept();
  23. System.out.println("Accepted connection : " + sock);
  24.  
  25. // receive file
  26. byte [] mybytearray = new byte [filesize];
  27. InputStream is = sock.getInputStream();
  28.  
  29. bytesRead = is.read(mybytearray,0,mybytearray.length);
  30. current = bytesRead;
  31.  
  32. fname = new String(mybytearray,0,256).replace(" ", "");
  33.  
  34.  
  35. do {
  36. bytesRead =
  37. is.read(mybytearray, current, (mybytearray.length-current));
  38. if(bytesRead >= 0) current += bytesRead;
  39. } while(bytesRead > -1);
  40.  
  41. FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/data/" + f);
  42. BufferedOutputStream bos = new BufferedOutputStream(fos);
  43.  
  44. bos.write(mybytearray, 0, current);
  45. bos.flush();
  46. long end = System.currentTimeMillis();
  47. System.out.println(end - start);
  48. bos.close();
  49. sock.close();
  50.  
  51. }
  52. }
  53. catch(Exception e) {
  54. Log.e("TCP", "S: Error", e);
  55. }
  56. finally {
  57. try {
  58. servsock.close();
  59. } catch (IOException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. //@Override
  66. protected void onDestroy() {
  67. // super.onDestroy();
  68. if (servsock != null) {
  69. try {
  70. servsock.close();
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement