Advertisement
yaramohamed1

Server-new

Sep 16th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.io.FileNotFoundException;
  7. import java.io.PrintWriter;
  8. import java.io.UnsupportedEncodingException;
  9. import java.util.Calendar;
  10. import javax.swing.JOptionPane;
  11.  
  12.  
  13. public class Camera extends Thread{
  14.  
  15. PrintWriter writer;
  16. long TimeoutStart;
  17. int TIMEOUT=6000;
  18. int PositionfromSocket=5;
  19. String line;
  20. long timeDiff;
  21. long receivedTime;
  22. String indexOfReceived;
  23. String rec;
  24. BufferedReader in;
  25. Socket clientSocket;
  26. ServerSocket serverSocket;
  27.    
  28.     public Camera() throws FileNotFoundException, UnsupportedEncodingException{
  29.         writer = new PrintWriter("time.txt", "UTF-8");//open new file
  30.     try {
  31.     serverSocket = new ServerSocket(1248);
  32.         } catch (IOException e1) {
  33. e1.printStackTrace();
  34. }
  35. }
  36.  
  37. public void run() {
  38. try {
  39. clientSocket = serverSocket.accept();
  40. in = new BufferedReader(
  41.        new InputStreamReader(clientSocket.getInputStream()));
  42. } catch (IOException e1) {
  43. // TODO Auto-generated catch block
  44. e1.printStackTrace();
  45. }
  46. int prev=999;
  47. long timeCounter = System.currentTimeMillis();
  48. while(true)
  49. {
  50. try
  51. {
  52. line=in.readLine();//read input line
  53. receivedTime=System.currentTimeMillis(); //get time now in miliseconds
  54. PositionfromSocket=Integer.parseInt(line.substring(0,1)); //get the position of the player
  55. indexOfReceived=line.substring(14);//get the index
  56. rec=line.substring(0,1);//add the position to new line
  57. rec+=receivedTime+"";//add the received time
  58. rec+=indexOfReceived;//add the index
  59. writer.println(rec);//add this line to file
  60. System.out.println("Message time: "+(System.currentTimeMillis()-timeCounter));
  61. timeCounter=System.currentTimeMillis();
  62. System.out.println("Socket Position "+PositionfromSocket);
  63. if(PositionfromSocket<1 || PositionfromSocket>9)
  64. {   PositionfromSocket = prev; 
  65. }
  66.  
  67. } catch (IOException e)
  68. {
  69. System.err.println("Can not read from camera module.");
  70. }
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement