我已经为多线程服务器编写了一个客户端。 2 个客户端的 2 次运行失败

发布于 2025-01-05 00:39:53 字数 3401 浏览 0 评论 0原文

我为多线程服务器设计了一个客户端。问题是,当我运行客户端时..它会在某个时间后挂起..并且它不会运行超过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");
}
}
}

I have designed a client for a multithreaded server. the problem is that when i run the client.. it hangs after sometym.. and it doesn't run for more than 1 client. I cannot understand where can be the problem.
please help...

the code is as follows:

 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");
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文