67,513
社区成员
发帖
与我相关
我的任务
分享
public class Server{
List<Myclient> linkedlist=new LinkedList<Myclient>();
ServerSocket ss;
Socket s;
public void connection() throws IOException{
ss=new ServerSocket(11165);
while(true) {
s=ss.accept();
Myclient my=new Myclient(s);
linkedlist.add(my);
new Thread(my).start();
}
}
private class Myclient implements Runnable{
BufferedReader br;
BufferedWriter bw;
public Myclient(Socket s){
try {
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter((s.getOutputStream())));
} catch (IOException e) {
Close.closeAll(br,bw);
}
}
@Override
public void run() {
while(true) {
send();
}
}
public String receive(){
String msg=null;
try {
msg=br.readLine();
} catch (IOException e) {
Close.closeAll(br);
}
return msg;
}
public void send(){
String data=null;
data=this.receive();
for(Myclient other:linkedlist){
if(this!=other) {
if(null!=data&& !data.equals("")) {
try {
other.bw.write("服务器-》"+data);
other.bw.newLine();
other.bw.flush();
} catch (IOException e) {
Close.closeAll(bw);
}
}
}
}
}
}
public static void main(String[] args) throws IOException{
Server sv=new Server();
sv.connection();
}
}