200分求LAN聊天室程序1 同上 谢谢帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //这里是服务器端 客户端放在另一个帖了 这是以前学socket时候写的 你看行不import java.net.*;import java.io.*;import java.util.*;public class MyChatRoomServer { public static void main(String[] args)throws Exception { ServerSocket ss = new ServerSocket(8000); System.out.println("Starting..."); List s = new ArrayList(); while(true){ Socket socket=ss.accept(); System.out.println("connect"); s.add(socket); new ChatRoomServer(s,socket).start(); } }}class ChatRoomServer extends Thread{ private BufferedReader in; private static PrintWriter out; private Socket socket; private List s; private boolean isStart=true; public ChatRoomServer( List s,Socket socket) { this.socket = socket; this.s=s; try { in=new BufferedReader(new InputStreamReader(this.socket.getInputStream())); } catch (IOException e) { e.printStackTrace(); } } public void run(){ while(isStart){ try { String str; if((str=in.readLine())!=null){ if(str.equals("78987")){ isStart=false; socket.close(); s.remove(socket); System.out.println("exiting"); }else print(s,str); } }catch (Exception e) { e.printStackTrace(); } } } static void print(Collection c,String str){ Iterator it = c.iterator(); while(it.hasNext()){ Object o = it.next(); Socket soc =(Socket)o; try { out=new PrintWriter(soc.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } out.println(str); out.flush(); } }} 感谢CodeFactory_yy() 面试题目,字符串的操作,希望能写出代码! java主类方法中的 static 关键字 jpanel设置了背景图片,并且可以更换,如何保存更换后的背景图片的设置,使下次开启的jpanel的背景图片是上次设置的图片? 类的初始化顺序,总结一下。 类型转换 关于imageIcon的问题 是不是只能用gif??? 7年mm,有结局,撒分,java版每个都有,希望斑竹别删 关于applet的窗口大小问题 为什么说java是跨平台的? 求助,这个应该怎么写 200分求LAN聊天室程序2 eclipse的junit测试如何实现?
import java.net.*;
import java.io.*;
import java.util.*;
public class MyChatRoomServer {
public static void main(String[] args)throws Exception {
ServerSocket ss = new ServerSocket(8000);
System.out.println("Starting...");
List s = new ArrayList();
while(true){
Socket socket=ss.accept();
System.out.println("connect");
s.add(socket);
new ChatRoomServer(s,socket).start();
}
}
}class ChatRoomServer extends Thread{
private BufferedReader in;
private static PrintWriter out;
private Socket socket;
private List s;
private boolean isStart=true;
public ChatRoomServer( List s,Socket socket) {
this.socket = socket;
this.s=s;
try {
in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(isStart){
try {
String str;
if((str=in.readLine())!=null){
if(str.equals("78987")){
isStart=false;
socket.close();
s.remove(socket);
System.out.println("exiting");
}else
print(s,str);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
static void print(Collection c,String str){
Iterator it = c.iterator();
while(it.hasNext()){
Object o = it.next();
Socket soc =(Socket)o;
try {
out=new PrintWriter(soc.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
out.println(str);
out.flush();
}
}
}