title: Android Socket to Server, Server reports "Connection Reset" error
body: <p>am developing an Android Application for Email System, and the application needs to connect server (written by my partner).
  My cellphone use wifi generated by my PC, when I try to login,the server just reports the "Connection reset"error for a few times in 10 seconds.</p>
  
  <pre><code>Client Login IP:59.71.68.237 Server host:Iris   Time:2015.06.13 at 09:04:36
  java.net.SocketException:Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:209)
      at java.net.SocketInputStream.read(SocketInputStream.java:141)
      at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
      at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
      at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
      at java.io.InputSteamReader.read(InputStreamReader.java:184)
      at java.io.BufferedReader.fill(BufferedReader.java:161)
      at java.io.BufferedReader.readLine(BufferedReader.java:324)
      at java.io.BufferedReader.readLine(BufferedReader.java:389)
      at com.mailService.emsp.tempEMSP.run(tempEMSP.java:42)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>I debug in Android,and I find out that the code stops after executing </p>
  
  <pre><code>client = new Socket(ServerInfo.sockethost, ServerInfo.socketport);
  </code></pre>
  
  <p>and the server reports error just after that.</p>
  
  <p>Here is the client code.</p>
  
  <pre><code>public class Login extends Activity {
  
  private Intent intent = null;
  private EditText account = null; //Account
  private EditText passwpord = null; //password
  private Button login = null; // login
  private TextView register = null; // register
  
  private String useraccount = null;
  private String userpassword = null;
  
  private Dialog m_Dialog = null; // dialog
  
  private Socket client; //client socket
  private BufferedReader reader;
  private PrintWriter writer;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
  
      intent = this.getIntent();
  
      account = (EditText) this.findViewById(R.id.login_account);
      passwpord = (EditText) this.findViewById(R.id.login_password);
      login = (Button) this.findViewById(R.id.login_button);
      register = (TextView) this.findViewById(R.id.login_register);
  
      register.setClickable(true);
      //login button event
      login.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              useraccount = account.getText().toString();
              userpassword = account.getText().toString();
  
              //judge whether the account is null
              if (Regex.isEmpty(useraccount)) {
                  Toast.makeText(getApplicationContext(), "Please fill in account！",
                          Toast.LENGTH_SHORT).show();
              }
              // judge whether the account meets email form
              else if (Regex.isEmail(useraccount)) {
                  Toast.makeText(getApplicationContext(), "account form is wrong！",
                          Toast.LENGTH_SHORT).show();
              }
              // judge whether password is null
              else if (Regex.isEmpty(userpassword)) {
                  Toast.makeText(getApplicationContext(), "Please fill in the password！",
                          Toast.LENGTH_SHORT).show();
              } else {
                  System.out.println("Client：Connecting");
                  m_Dialog = ProgressDialog.show(Login.this, "Hint", "Login...",true);
  
                  // create a new thread
                  new Thread(new Runnable() {
                      int flag = 0;           
                      int mark = 0;
  
                      // based on the custom protocol
                      @Override
                      public void run() {
                          try {
                              // create a new socket
                              System.out.println("new Socket('', 6666);");
                              //This is where the code stops
                              client = new Socket(ServerInfo.sockethost,  ServerInfo.socketport);
                              System.out.println("Socket 'client' created.");
  
                              String command = null;
                              System.out.println("new reader.");
                              // IO reader and writer
                              reader = new BufferedReader(
                                      new InputStreamReader(client.getInputStream()));
                              System.out.println("new writer.");
                              writer = new PrintWriter(client.getOutputStream(), true);
                              // Client:login
                              writer.println("login");
  
                              while ((command = reader.readLine()) != null) {
                                  StringTokenizer stk = new StringTokenizer(command);
                                  String cmd = stk.nextToken();
                                  System.out.println("cmd:" + cmd);
                                  if (cmd.equals("user")) {
                                      writer.println(useraccount);
                                      System.out
                                              .println("writer.println('useraccount');");
                                  } else if (cmd.equals("pass")) {
                                      writer.println(userpassword);
                                      System.out
                                              .println("writer.println('userpassword');");
                                  } else if (cmd.equals("inexist")) {
                                      writer.println(useraccount);
                                      System.out
                                              .println("inexist,  writer.println('username');");
                                  } else if (cmd.equals("error")) {
                                      writer.println(useraccount);
                                      System.out
                                              .println("error,  writer.println('username');");
                                  } else if (cmd.equals("loginok")) {
                                      System.out.println("case 'loginok'");
                                      mark = 1;
                                      System.out.println("========mark = "
                                              + mark);
                                      break;
                                  }
                              }
                              System.out.println("Login succeed!，flag=0");
                          } catch (Exception e) {
                              flag = 1; // Login fail
                              e.printStackTrace();
                          } finally {
                              try {
                                  if (client != null)
                                      client.close();
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                          }
  
                          m_Handler.post(new Runnable() {
                              @Override
                              public void run() {
                                  if (flag == 0) {
                                      ServerInfo.username = useraccount;
                                      ServerInfo.password = userpassword;
                                      intent.setClass(Login.this,
                                              MainActivity.class);
                                      Bundle bundle = new Bundle();
                                      bundle.putString("username",
                                              useraccount);
                                      bundle.putString("password",
                                              userpassword);
                                      intent.putExtras(bundle);
                                      startActivity(intent);
                                      Login.this.finish();
                                      System.out
                                              .println("Client: Login succeed!");
                                      Toast.makeText(getApplicationContext(),
                                              "Login succeed!!", Toast.LENGTH_SHORT)
                                              .show();
                                  } else {
                                      Toast.makeText(getApplicationContext(),
                                              "Login fail!", Toast.LENGTH_SHORT)
                                              .show();
                                  }
                              }
                          });
                          m_Dialog.dismiss();
                      }
                  }).start();
              }
          }
      });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When I execute client code separately from the application, it just works fine!</p>
  
  <p>What is the problem ? Do you have any suggestions ?</p>