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面对对象基础学完,多态接口抽象类学完,可是做项目确实一头雾水。 SSL通讯加解密问题 数据库里查询的显示格式 java连接mysql 的问题,请过来看一下? 客户端接接问题 提问:System.exit(0);和System.exit(1);有什么区别? 这三句话定义了3个什么对象?请教! 请进来帮帮我 , 帮忙看一下小代码 “当某个实例不再被任何对象引用时就会被垃圾回收机制回收”,那么怎样才算是不被任何对象引用呢? 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();
}
}
}