服务器代码:
public class server {
//private static ArrayList<user> t = new ArrayList<>();
public static Socket s = null;
public static ServerSocket ss = null;
public static void main(String[] args) {
try {
ss = new ServerSocket(1777);
while (true) {
System.out.println("waiting for connexion");
s = ss.accept();
System.out.println("connected with "+s.getRemoteSocketAddress());
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(os,true);
String username = br.readLine();
int number = 0;
user us = new user(s,number,username);
//t.add(us);
pw.print(us);
//new service(s,username,number,t).start();
new service(s,us).start();
number++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
服务类代码:
public class service extends Thread {
private static Socket s;
private static String user;
private static int number ;
private ArrayList<user> locallist = new ArrayList<user>();
private static user us;
public service(Socket s,user u){
this.s = s;
this.us = u;
locallist.add(u);
}
@Override
public void run() {
int listsize = locallist.size();
user lastuser = locallist.get(listsize-1);
try {
while (true) {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(os,true);
System.out.println("last user : " + lastuser);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端类代码:
public class client {
public static void main(String[] args) {
try {
while (true) {
Socket s = new Socket("localhost",1777);
System.out.println("connected with localhost");
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(os,true);
Scanner e = new Scanner(System.in);
System.out.println("enter you username to continue");
String username = e.next();
pw.println(username);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
服务器输出:
waiting for connexion
connected with /127.0.0.1:51899
waiting for connexion
connected with /127.0.0.1:51902
last user : username teste usernumber 0
last user : username teste usernumber 0
last user : username teste usernumber 0
基本上是无限循环
客户端输出:
connected with localhost
enter you username to continue
teste
connected with localhost
enter you username to continue
我的问题是如何在每次客户端连接后停止无限循环并显示最后连接/添加的成员
答案 0 :(得分:0)
在服务器类中:
classes
在服务类中:
public static void main(String[] args) {
try {
ss = new ServerSocket(1777);
int number = 0; // <--- place the counter here not in the while
while (true) {
System.out.println("waiting for connexion");
s = ss.accept();
System.out.println("connected with "+s.getRemoteSocketAddress());
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(os,true);
String username = br.readLine();
//int number = 0; // Not here ... you just set the counter always to 0
user us = new user(s,number,username);
pw.print(us);
new service(s,us).start();
number++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
这就是完成的代码......你只需要在服务类中删除while,移动计数器变量&#34; number&#34;在服务器类中不合时宜。
如果您希望稍后在服务级中使用while进行服务器和客户端之间的通信,则只需在打印用户之后放置一段时间。