2012-05-31 27 views
1
package project.robot.network; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 

import project.robot.BluetoothConnection; 
import android.os.AsyncTask; 

public class TCPServer extends AsyncTask<Void, Integer, Void> { 
    int port;    //Port on which server is running 
    String clientIP;  //IP address of remote client 
    ServerSocket serverSocket; //Server Socket 
    Socket clientSocket; //Socket connected to client 
    DataOutputStream out; //Output stream object to send data 
    DataInputStream in;   //Input Stream object to receive data 

    boolean flag; 
    boolean videoFlag; //Used to toggle video 
    private VideoThread vthread; //Video Thread Object 

    public static BluetoothConnection conn; 

    /* 
    * Starts a TCP Server which listens to incoming connections 
    */ 
    public TCPServer(int port) { 
     this.port = port; 
     videoFlag = false; 
     vthread = null; 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     int msg;  

     //Initiating server socket 
     try { 
      serverSocket = new ServerSocket(port); 
      flag = true; 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      System.out.println("Tcpserver: unable to bind socket"); 
      e1.printStackTrace(); 
      flag = false; 
     } 

     System.out.println("TCPServer: Server started"); 

     while(flag) { 
      try { 
       //Accepting incoming connection 
       clientSocket = serverSocket.accept(); 
       clientIP = clientSocket.getInetAddress().getHostAddress(); 
       System.out.println("TCPServer: Connected to client at " + clientIP); 

       //Getting input and output streams 
       in = new DataInputStream(clientSocket.getInputStream()); 
       out = new DataOutputStream(clientSocket.getOutputStream()); 
       msg = 1; 

       //Start VideoThread 
       try { 
        vthread = new VideoThread(clientIP); 
        vthread.startVideo(); 
        System.out.println("TCPServer: Video started"); 
        videoFlag = true; 
       } catch (Exception e) { 
        e.printStackTrace(); 
        vthread = null; 
        System.out.println("TCPServer: Error while starting video"); 
       } 

       //Reading input commands and signaling processing function 
       while(flag && msg != 0) { 
        msg = in.readInt(); 
        publishProgress(msg); 
       } 

       //Stopping video streaming if running 
       if(vthread != null) { 
        vthread.stopVideo(); 
        vthread = null; 
       } 
       System.out.println("TCPServer: Video stopped"); 

       //Closing Connection to client 
       clientSocket.close(); 
       clientSocket = null; 
       System.out.println("TCPServer: Closed connection to host at " + clientIP); 
       clientIP = "null"; 

      } catch (IOException e) { 
       if(clientSocket != null) { 
        clientSocket = null; 
       } 
       flag = false; 
       System.out.println("TCPServer: Error while accepting or closing client connection"); 
      } 
     } 

     //Closing serverSocket 
     if(serverSocket != null) { 
      try { 
       serverSocket.close(); 
       System.out.println("TCPServer: Server Socket Closed"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       System.out.println("TCPServer: Error while closing socket"); 
      } 
      serverSocket = null; 
     } 

     System.out.println("TCPServer: Server stopped"); 
     return null; 
    } 

    private void sendSignal(int signal) { 
     if(conn != null) 
      conn.send(signal); 
     else 
      System.out.println("TCPServer: null conn, can't send value"); 
    } 


    @Override 
    protected void onProgressUpdate(Integer... integers) { 
     //Method is called every time a publishProgress is called from doInBackground 
     for(Integer integer : integers) { 
      System.out.println("TCPServer: Message received - " + integer); 
      switch(integer) { 
      case 1: 
       //Enable SMS service 
       if(vthread != null) 
        vthread.msgFlag = true; 
       break; 
      case 2: 
       //Move backward 
       sendSignal(2); 
       break; 
      case 4: 
       //Move left 
       sendSignal(4); 
       break; 
      case 5: 
       //Stop 
       sendSignal(5); 
       break; 
      case 6: 
       //Move right 
       sendSignal(6); 
       break; 
      case 7: 
       //Buzzer toggle 
       sendSignal(7); 
      case 8: 
       //Move forward 
       sendSignal(8); 
       break; 
      case 9: 
       //Toggle Video 
       toggleVideo(); 
       break; 
      default: 
       System.out.println("TCPServer: Unrecognized instruction : " + integers[0]); 
       break; 
      } 
     } 
    } 

    /* 
    * Toggles video streaming state 
    * if it was on then it will stop it 
    * else it will start video streaming 
    */ 
    private void toggleVideo() { 
     //If videoFlag is true then stop Video else start video 
     if(vthread==null) 
      return; 

     if(vthread.videoStream) { 
      vthread.videoStream = false; 
      System.out.println("TCPServer: Video streamming stopped"); 
     } else { 
      System.out.println("TCPServer: Video streamming started"); 
      vthread.videoStream = true; 
     } 
    } 


    /* 
    * `s the server process 
    */ 
    public void stop() { 
     System.out.println("TCPServer: Stopping server"); 
     flag = false; 

     //Stopping video if it is running 
     if(vthread != null) { 
      vthread.stopVideo(); 
      vthread = null; 
     } 
     System.out.println("TCPServer: Video stopped"); 

     //Closing server socket 
     if(serverSocket != null) { 
      try { 
       serverSocket.close(); 
       System.out.println("TCPServer: Server Socket Closed"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       System.out.println("TCPServer: Error while closing socket"); 
      } 
      serverSocket = null; 
     } 
    } 
} 

請有人告訴我telnet是如何工作的(或告訴我在哪裏我可以找到一個教程)。我已經得到了上面的代碼。我必須將上面的代碼(適用於blutooth)更改爲telnet代碼(適用於wifi)。telnet教程

+2

我不敢看你的代碼,但遠程登錄感覺很老給我;它以明文iirc傳遞密碼。不妨從ssh開始,我想.. –

回答

3

你的問題是有點不可思議,在這個意義上,它會像問「教程瀏覽器」,顯示出一段代碼,下載一個網頁,並要求瀏覽器是如何工作的。

Telnet只是一個應用程序(如瀏覽器),可以連接到服務器併發送接收文本。這段代碼還是連接到一臺服務器,併發送接收的東西(快速瀏覽,主要是整數後)的應用程序。

很多代碼的內容主要涉及建立連接和傳遞參數的。將你感興趣的東西是數據連接:

  //Getting input and output streams 
      in = new DataInputStream(clientSocket.getInputStream()); 
      out = new DataOutputStream(clientSocket.getOutputStream()); 

後來

  //Reading input commands and signaling processing function 
      while(flag && msg != 0) { 
       msg = in.readInt(); 
       publishProgress(msg); 
      } 

後來

private void sendSignal(int signal) { 
    if(conn != null) 
     conn.send(signal); 
    else 
     System.out.println("TCPServer: null conn, can't send value"); 
} 

你應該瞭解在Java Socket編程(tutorial here)第一 。明白了之後,你會明白爲什麼你的問題有點奇怪。您也可以開始弄清楚如何編寫代碼以使用其他協議進行連接。是的,您可以在編寫代碼之前先使用telnet手動嘗試。

如果你真的需要一個「遠程登錄教程」,你可以下載一個使用純文本連接任何RFC,只是模擬的遠程登錄協議。 HTTP,FTP,SMTP和IRC是最簡單的開始。嘗試使用telnet發送電子郵件。