Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = new Server();
- mythread = new Thread(runnable);
- mythread.start();
- public class Server implements Runnable{
- ServerSocket servsock;
- String fname;
- @Override
- public void run() {
- int filesize=6022386;
- long start = System.currentTimeMillis();
- int bytesRead;
- int current = 0;
- try {
- // create socket
- servsock = new ServerSocket(5554);
- while (true) {
- System.out.println("Waiting...");
- Socket sock = servsock.accept();
- System.out.println("Accepted connection : " + sock);
- // receive file
- byte [] mybytearray = new byte [filesize];
- InputStream is = sock.getInputStream();
- bytesRead = is.read(mybytearray,0,mybytearray.length);
- current = bytesRead;
- fname = new String(mybytearray,0,256).replace(" ", "");
- do {
- bytesRead =
- is.read(mybytearray, current, (mybytearray.length-current));
- if(bytesRead >= 0) current += bytesRead;
- } while(bytesRead > -1);
- FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/data/" + f);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- bos.write(mybytearray, 0, current);
- bos.flush();
- long end = System.currentTimeMillis();
- System.out.println(end - start);
- bos.close();
- sock.close();
- }
- }
- catch(Exception e) {
- Log.e("TCP", "S: Error", e);
- }
- finally {
- try {
- servsock.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //@Override
- protected void onDestroy() {
- // super.onDestroy();
- if (servsock != null) {
- try {
- servsock.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement