Я написал клиент для многопоточного сервера.не работает 2 запуска для 2 клиентов - PullRequest
1 голос
/ 11 февраля 2012

Я разработал клиент для многопоточного сервера. проблема в том, что когда я запускаю клиент .. он зависает после sometym .. и не работает для более чем 1 клиента Я не могу понять, где может быть проблема. пожалуйста, помогите ...

код выглядит следующим образом:

 public class C1editted extends Activity {

public static final String SERVER_HOSTNAME = "localhost";
public static final int SERVER_PORT = 56555;
private TextView tv=null;
private EditText et=null;
private Button bt=null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Thread t = new Thread(){
        @Override
        public void run() {
            client();
        }
    };
    t.start();


}
void client()
{
     tv=(TextView)findViewById(R.id.clienttext);
     et=(EditText)findViewById(R.id.clientedit);
     bt=(Button)findViewById(R.id.cbtn);
     BufferedReader in = null;
     PrintWriter out=null;
     String msg=null;
    try {
        // Connect to Nakov Chat Server
         Socket socket = new Socket(SERVER_HOSTNAME, SERVER_PORT);
        in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
        out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
                true);
     //   System.out.println("Connected to server " +
       //    SERVER_HOSTNAME + ":" + SERVER_PORT);
        tv.append("Connected to server " +
                SERVER_HOSTNAME + ":" + SERVER_PORT);
        Thread.sleep(1000);
     } catch (IOException ioe) {
        System.err.println("Can not establish connection to " +
          SERVER_HOSTNAME + ":" + SERVER_PORT);
        tv.append("Can not establish connection to " +SERVER_HOSTNAME + ":" + SERVER_PORT);

        ioe.printStackTrace();
        System.exit(-1);
     }  catch (Exception ioe) {
         System.err.println("Can not establish connection to " +
                 SERVER_HOSTNAME + ":" + SERVER_PORT);
               tv.append("Can not establish connection to " +SERVER_HOSTNAME + ":" + SERVER_PORT);

               ioe.printStackTrace();
               System.exit(-1);
     }

     bt.setOnClickListener(new OnClickListener() {

             public void onClick(View v) {


                  String msg=et.getText().toString();





             }
          }); 
  // Create and start Sender thread
     Sender sender = new Sender(out,msg);
     sender.setDaemon(true);
     sender.start();
     et.setText("");


     try {
        // Read messages from the server and print them
         String message;
        while ((message=in.readLine()) != null) {
           // System.out.println(message);
           tv.append(message);
        }
     } catch (IOException ioe) {
        System.err.println("Connection to server broken.");
        tv.append("Connection to server broken.");
        ioe.printStackTrace();
     }

}
}


 class Sender extends Thread
{
  private PrintWriter mOut;
  private String msg;
  public Sender(PrintWriter aOut, String amsg)
  {
      mOut = aOut;
      msg=amsg;
  }

/**
 * Until interrupted reads messages from the standard input (keyboard)
 * and sends them to the chat server through the socket.
 */
 public void run()
 {
 try {
 //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
mOut.println(msg);
mOut.flush();
} catch (Exception ioe) {
        // Communication is broken
System.out.println("Communication is broken");
}
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...