public void run() {

    mRun = true;

    try {
      // here you must put your computer's IP address.
      InetAddress serverAddr = InetAddress.getByName(SERVERIP);

      Log.e("TCP Client", "C: Connecting...");

      // create a socket to make the connection with the server
      Socket socket = new Socket(serverAddr, SERVERPORT);

      try {

        // send the message to the server
        out =
            new PrintWriter(
                new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

        Log.d("TCP Client", "C: Sent.");

        Log.d("TCP Client", "C: Done.");
        textView.setText("connected");
        // receive the message which the server sends back
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        try {
          // in this while the client listens for the messages sent by the server

          while (mRun) {
            serverMessage = in.readLine();

            if (serverMessage != null && mMessageListener != null) {
              // call the method messageReceived from MyActivity class
              mMessageListener.messageReceived(serverMessage);

              Log.d("TCP", "S: Received Message: '" + serverMessage + "'");
            }
            serverMessage = null;
          }
        } catch (SocketException e) {
          textView.setText("connection lost");
        }
        Log.d("TCP", "S: Received Message: '" + serverMessage + "'");
        Log.d("TCP Client", serverMessage);
      } catch (Exception e) {
        textView.setText("unable to connect");
        Log.d("TCP", "S: Error", e);

      } finally {
        // the socket must be closed. It is not possible to reconnect to this socket
        // after it is closed, which means a new socket instance has to be created.
        socket.close();
      }

    } catch (Exception e) {

      Log.d("TCP", "C: Error", e);
    }
  }
示例#2
0
  public void run() {

    // Vector picVec = new Vector();

    File pictureFileDir = getDir();
    if (!pictureFileDir.exists()) {

      Log.e("Picture Directory", "Cannot open");
      return;
    }

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";

    String filename = pictureFileDir.getPath() + File.separator + photoFile;

    File pictureFile = new File(filename);

    try {

    } catch (Exception error) {
      Log.e("Picture Directory", "File could not read");
    }

    mRun = true;

    try {
      // here you must put your computer's IP address.
      InetAddress serverAddr = InetAddress.getByName(SERVERIP);
      // SERVERIP = getIPAddress();
      // Log.d("DEBUG", "Susmita's SERVERIP "+SERVERIP);
      Log.e("TCP Client", "C: Connecting...");

      // create a socket to make the connection with the server
      Socket socket = new Socket(serverAddr, SERVERPORT);

      try {

        // send the message to the server
        out =
            new PrintWriter(
                new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

        Log.e("TCP Client", "C: Sent.");

        Log.e("TCP Client", "C: Done.");

        // receive the message which the server sends back
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        // in this while the client listens for the messages sent by the server
        while (mRun) {
          serverMessage = in.readLine();

          if (serverMessage != null && mMessageListener != null) {
            // call the method messageReceived from MyActivity class
            mMessageListener.messageReceived(serverMessage);
          }
          serverMessage = null;
        }
        // Sending picture file
        fos = new FileInputStream(pictureFile);

        while ((b = fos.read()) != -1) {
          out.print(b);
        }
        Log.d("DEBUG", "Picture file sent");

        Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

      } catch (Exception e) {

        Log.e("TCP", "S: Error", e);

      } finally {
        // the socket must be closed. It is not possible to reconnect to this socket
        // after it is closed, which means a new socket instance has to be created.
        socket.close();
        fos.close();
      }

    } catch (Exception e) {

      Log.e("TCP", "C: Error", e);
    }
  }