Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. public class Servic extends Service
  2. {
  3. private boolean isRunning = false;
  4. private static final String SERVER_IP = "192.168.43.112";
  5. private static final int SERVERPORT = 6000;
  6. private Socket socket;
  7. Thread ClientThread = null;
  8. String st = null;
  9. String path = null;
  10.  
  11. public void onCreate()
  12. {
  13.  
  14. }
  15.  
  16. public int onStartCommand(Intent intent, int flags, int startId)
  17. {
  18. super.onStartCommand(intent, flags, startId);
  19.  
  20. path=(String) intent.getExtras().get("path");
  21.  
  22. isRunning = true;
  23. this.ClientThread = new Thread(new ClientThread());
  24. this.ClientThread.start();
  25.  
  26. return START_STICKY;
  27. }
  28.  
  29. public void onDestroy()
  30. {
  31. super.onDestroy();
  32. isRunning = false;
  33. }
  34.  
  35. public class ClientThread implements Runnable
  36. {
  37. public void run()
  38. {
  39. try
  40. {
  41. InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  42.  
  43. socket = new Socket(serverAddr,SERVERPORT);
  44.  
  45. File file = new File(path);
  46. byte[] bytes = new byte[8192];
  47. FileInputStream fis = new FileInputStream(file);
  48. BufferedInputStream bis = new BufferedInputStream(fis);
  49. OutputStream out = socket.getOutputStream();
  50. out.write((int)file.length());
  51. int count,file_size;
  52.  
  53. while ((count = bis.read(bytes)) > 0) {
  54. System.out.println(count);
  55. out.write(bytes, 0, count);
  56. }
  57. out.flush();
  58. out.close();
  59. fis.close();
  60. bis.close();
  61. }
  62. }
  63. }
  64.  
  65. @Override
  66. public IBinder onBind(Intent intent)
  67. {
  68. return null;
  69. }
  70. }
  71.  
  72. int main(int argc, char *argv[])
  73. {
  74. WSADATA wsa;
  75. socklen_t addrlen;
  76. SOCKET s , new_socket;
  77. char buffer[256];
  78. struct sockaddr_in server , client;
  79. int c,n;
  80. int result;
  81.  
  82. if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  83. {
  84. printf("ERROR initlizaing winsock2 dll : %d",WSAGetLastError());
  85. return 1; // exit program with error.
  86. }
  87.  
  88. printf("Initialised.n");
  89.  
  90. s = socket(AF_INET,SOCK_STREAM, 0);
  91. if(s == INVALID_SOCKET)
  92. {
  93. printf("error at creating socket :%d" ,WSAGetLastError());
  94. WSACleanup();
  95. return 1;
  96. }
  97.  
  98. printf("socket createdn");
  99.  
  100. memset(&server, 0, sizeof server);
  101. server.sin_family = AF_INET;
  102. server.sin_addr.s_addr = INADDR_ANY;
  103. server.sin_port = htons(6000);
  104.  
  105. if (bind(s , (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
  106. {
  107. printf("Bind fialed with errror code : %d",WSAGetLastError());
  108. closesocket(s);
  109. WSACleanup();
  110. return 1;
  111. }
  112.  
  113. listen(s , 3);
  114.  
  115. printf("waiting for incoming connectionsn");
  116. c = sizeof(struct sockaddr_in);
  117. new_socket = accept(s , (struct sockaddr *)&client,&c);
  118. if(new_socket == INVALID_SOCKET)
  119. {
  120. printf("accept fialed with error code : %d" ,WSAGetLastError());
  121. closesocket(s);
  122. WSACleanup();
  123. return 1;
  124. }
  125.  
  126. printf("Connection acceptëd");
  127.  
  128. //as per my knowledge, file accepting code should be somewhere here.
  129.  
  130. getchar();
  131. closesocket(s);
  132. WSACleanup();
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement