Client

by aa on March 19th, 2012
No notes
Syntax: No syntax
Show lines - Hide lines - Show in textbox - Download
  1. package source;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7.  
  8. @SuppressWarnings("unused")
  9. public class Client
  10. {
  11.  
  12. private byte[] incomingData = new byte[512];
  13. private byte[] outgoingData = new byte[512];
  14.  
  15. private byte[] reserveBuffer = new byte[512];
  16.  
  17. private DatagramSocket clientSocket;
  18.  
  19. private InetAddress serverAddress = InetAddress.getByName("localhost");
  20. private final int serverPort = 9999;
  21.  
  22. private String data = null;
  23.  
  24. private final String date_format = "yyyy.MM.dd-HH.mm.ss";
  25.  
  26.  
  27. Client(int port, String filename) throws Exception
  28. {
  29. clientSocket = new DatagramSocket(port);
  30. data = filename;
  31. }
  32.  
  33. private void request(String mode) throws Exception
  34. {
  35. if(mode.equals("RRQ"))
  36. {
  37. sendRRQ();
  38. } else if(mode.equals("WRQ"))
  39. {
  40. sendWRQ();
  41. } else {
  42. System.out.println("Unknown operation mode. Pick between RRQ or WRQ.");
  43. }
  44. }
  45.  
  46.  
  47. private void sendWRQ() throws Exception
  48. {
  49. String rrq = "WRQ-" + data;
  50. outgoingData = rrq.getBytes();
  51.  
  52. DatagramPacket output = new DatagramPacket(outgoingData, outgoingData.length, serverAddress, serverPort);
  53. clientSocket.send(output);
  54. sendData();
  55. }
  56.  
  57. private void sendData() throws Exception
  58. {
  59. DatagramPacket dataTransfer = new DatagramPacket(reserveBuffer, reserveBuffer.length, serverAddress, serverPort);
  60. InputStream fis = new FileInputStream(new File(data));
  61.  
  62. int x;
  63. while((x = fis.read(reserveBuffer,0,512)) != -1)
  64. {
  65. dataTransfer.setLength(x);
  66. clientSocket.send(dataTransfer);
  67. Thread.sleep(5);
  68. }
  69.  
  70. fis.close();
  71. }
  72.  
  73.  
  74. private void sendRRQ() throws Exception
  75. {
  76. String rrq = "RRQ-" + data;
  77. outgoingData = rrq.getBytes();
  78.  
  79. DatagramPacket output = new DatagramPacket(outgoingData, outgoingData.length, serverAddress, serverPort);
  80. clientSocket.send(output);
  81. if(receiveResponse())
  82. {
  83. receiveData();
  84. }
  85. }
  86.  
  87.  
  88. private boolean receiveResponse() throws Exception
  89. {
  90. DatagramPacket response = new DatagramPacket(incomingData, incomingData.length);
  91. clientSocket.receive(response);
  92.  
  93. String rsp = new String(response.getData(),0,response.getLength());
  94. if(rsp.contains("ERROR"))
  95. {
  96. System.out.println(rsp);
  97. return false;
  98. }
  99. return true;
  100. }
  101.  
  102.  
  103. private void receiveData() throws Exception
  104. {
  105. Calendar cal = Calendar.getInstance();
  106. SimpleDateFormat prefix = new SimpleDateFormat(date_format);
  107. String date = prefix.format(cal.getTime()).toString();
  108.  
  109. DatagramPacket receiveData = new DatagramPacket(incomingData, incomingData.length);
  110. OutputStream fos = new FileOutputStream(new File(date+data));
  111. System.out.println("I get to here 1");
  112.  
  113. DatagramPacket sendAck = new DatagramPacket(outgoingData, outgoingData.length, serverAddress, serverPort);
  114. System.out.println("I get to here 2");
  115.  
  116. while(true)
  117. {
  118. clientSocket.receive(receiveData);
  119. clientSocket.send(receiveData);
  120. System.out.println("I get to here 3");
  121.  
  122. if(receiveData.getLength() == 512)
  123. {
  124. fos.write(receiveData.getData());
  125. } else {
  126. fos.write(receiveData.getData(), receiveData.getOffset(), receiveData.getLength());
  127. break;
  128. }
  129. }
  130. fos.close();
  131. clientSocket.close();
  132. }
  133.  
  134. public static void main(String[] args)
  135. {
  136.  
  137. try {
  138.  
  139. new Client(9998, "img.jpg").request("RRQ");
  140. } catch (Exception e) {}
  141. }
  142. }
  143.  

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS