请教一下,这个为什么要写这个send方法

wangxuelid 2010-12-21 10:53:30

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}

public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

try {

while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;

public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
//e.printStackTrace();
}
}

public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);//-------=========--------这里不可以直接写吗,为什么把这部分写成个方法呢。往高手指点。
}
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}

} catch (IOException e1) {
e1.printStackTrace();
}


}
}

}
}

...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangxuelid 2010-12-21
  • 打赏
  • 举报
回复
再次感谢。sunyiz ,taoyongming
sunyiz 2010-12-21
  • 打赏
  • 举报
回复
和dos肯定是有关系的,send方法中用到了dos这个引用

其实楼主你可以这样理解
按你说的不写send方法,直接写在run的那一段,
针对这个例子来说,完全可以没有问题

但是,这样做的话
1、破坏了设计者的原意
2、代码重用度差
仅此而已,针对这个例子来说,影响不大
taoyongming 2010-12-21
  • 打赏
  • 举报
回复
我觉得写send()和直接写在run里面应该都可以把
wangxuelid 2010-12-21
  • 打赏
  • 举报
回复
而且,既然这个send是个public方法
就说明设计者是希望这个方法可以被直接通过对象调用

如果把这个功能写死在run里面,
那这个send的功能就不能通过对象来调用了
-------------------------首先,谢谢。这个可以这样理解。
Client c = clients.get(i);这个东西已经过去连接的个个客户端。
那么使用send方法,是否跟
private Socket s;
private DataOutputStream dos 有关系呢。
往指点。
sunyiz 2010-12-21
  • 打赏
  • 举报
回复
而且,既然这个send是个public方法
就说明设计者是希望这个方法可以被直接通过对象调用

如果把这个功能写死在run里面,
那这个send的功能就不能通过对象来调用了
wangxuelid 2010-12-21
  • 打赏
  • 举报
回复
往高手指点,,不胜感谢
sunyiz 2010-12-21
  • 打赏
  • 举报
回复
当一个方法中的代码过长的时候
把有独立功能的部分重新封装成一个方法
便于重用

这个send这样一封装
你在别的地方也可以用的上了

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧