我已经为多线程服务器编写了一个客户端。 2 个客户端的 2 次运行失败
我为多线程服务器设计了一个客户端。问题是,当我运行客户端时..它会在某个时间后挂起..并且它不会运行超过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论