我正在尝试创建一个服务器,它将接收到的任何输入发送回所有连接的客户端。我正在使用一个在创建它时启动一个线程的服务。
public void onCreate()
{
Thread fst = new Thread(new ServerThread());
fst.start();
}
这是线程
public void run()
{
try
{
serverSocket = new ServerSocket(SERVERPORT);
while (true)
{
// listen for incoming clients
Socket client = serverSocket.accept();
mclients = new Vector<Socket>();
mclients.add(client);
boolean finished = false;
try
{
for(int i=0; i<mclients.size(); i++) {
DataInputStream in = new DataInputStream(mclients.get(i).getInputStream());
PrintStream out = new PrintStream(mclients.get(i).getOutputStream());
// Print a message:
System.out.println("Client from : " + mclients.get(i).getInetAddress() + " port " + mclients.get(i).getPort());
// now get the input from the socket...
while(!finished)
{
String st = in.readLine();
// Send the same back to client
out.println(st);
// Write it to the screen as well
System.out.println(st);
// If the input was "quit" then exit...
if (st.equals("quit")) { finished = true; System.out.println("Thread exiting..."); }
}
}
handler.post(new Runnable() {@Override public void run() {}});
}
catch (final Exception e) {handler.post(new Runnable() {@Override public void run() {}});e.printStackTrace();}
}
它仅适用于发送输入的客户端。输入没有回显给所有客户端。任何人都可以帮我解决这个问题。我认为我的客户端代码是错误的,但我无法弄清楚。这是我的客户端代码:
public class Client extends Activity implements OnClickListener{
private EditText serverIp;
private static EditText data;
private TextView receivedata, answer1 ,answer2, answer3, answer4;
private Button connectPhones, send;
private String serverIpAddress = "";
private boolean connected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
receivedata = (TextView) findViewById(R.id.textView1);
answer1 = (TextView) findViewById(R.id.txtans1);
answer2 = (TextView) findViewById(R.id.txtans2);
answer3 = (TextView) findViewById(R.id.txtans3);
answer4 = (TextView) findViewById(R.id.txtans4);
data = (EditText) findViewById(R.id.data);
connectPhones = (Button) findViewById(R.id.connect_phones);
send = (Button) findViewById(R.id.send);
connectPhones.setOnClickListener(this);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == send)
{
if (!connected)
{
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(""))
{
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
}
public class ClientThread implements Runnable
{
String line;
String[] answers = new String[5];
final Handler handler = new Handler();
final Runnable updateUI = new Runnable() {
public void run() {
receivedata.setText( line );
//answer1.setText( answers[1] );
//answer2.setText( answers[2] );
//answer3.setText( answers[3] );
//answer4.setText( answers[4] );
}
};
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: SENDING...");
Socket socket = new Socket(serverAddr, 5000);
connected = true;
while(connected)
{
try
{
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());
out.println(data.getText());
line = in.readLine(); // read server
System.out.println("Echo: " + line);
handler.post(updateUI);
in.close(); // Close stream
out.close();
}
catch (Exception e)
{
Log.e("ClientActivity", "S: Error", e);
}
connected = false;
}
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e)
{
Log.e("ClientActivity", "C: Error", e);
}
}
}}
-----编辑-----
我应该使用不同的线程来处理客户端还是会出现问题?我该怎么做?