计算机网络课程设计(6)

2019-03-03 16:08

{

return clients; }

/** *

* 服务器端在此启动 */

public static void main(String[] arg) {

int port = DEFAULT_PORT; try {

serverSocket = new ServerSocket(port);

System.out.println(\服务器已经启动,正在监听...\ } catch (IOException e) {

System.out.println(\异常\ System.err.println(e); System.exit(1);

}

while (true) { // 死循环 try {

Socket cs = serverSocket.accept();

ClientProc cp = new ClientProc(cs); // 启动一个用户线程 Thread ct = new Thread(cp); //Thread ar = new Thread(ct.) ct.start();

addConnection(cs, cp); } catch (IOException e) {

System.err.println(e); } } } }

★★★★★★★★★★★★★★★★★★★★★★ ////////////////// ClientProc.java package Server;

26

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

class ClientProc implements Runnable {

/**

* 为某个用户服务的一个用户线程 */

Socket s;

BufferedReader in; PrintWriter out;

private String name = null; private String sex = null;

public ClientProc(Socket s) throws IOException {

this.s = s;

in = new BufferedReader(new InputStreamReader(s.getInputStream()));// 得到输入的端口流

out = new PrintWriter(s.getOutputStream());// 从端口得到一个输出流 this.updateList(); }

public String getName() {

return name; }

public String getSex() {

return sex; }

public Socket getSocket() {

return s; }

private void updateList() {

// 更新用户列表(即发送当前在聊天室的用户到新来的用户列表中) Vector cs = ChatServer.getClients();

27

if (cs != null) {

for (Enumeration e = cs.elements(); e.hasMoreElements();) {

ClientProc cp = (ClientProc) e.nextElement(); String exist_name = cp.getName(); String exit_sex = cp.getSex();

System.out.println(\ out.println(\ out.flush(); } } }

public void run() {

while (name == null) {

try {

String inmsg;

inmsg = in.readLine();

ChatServer.sendAll(\+ inmsg); new&zhangsan

String[] userInfo;

userInfo = inmsg.split(\ name = userInfo[0]; sex = userInfo[1]; } catch (IOException ee) {

ee.printStackTrace(); } }

while (true) {

try {

String line = in.readLine(); System.out.println(line); // 处理退出事件(读取信息) if (line.equals(\ {

28

// 发送信息更新用户列表 天室\ 息

消息 文件消息

ChatServer.sendAll(\【系统消息】 \退出了聊

ChatServer.deleteConnection(s, this); return;

// 处理刷新用户列表请求

} else if (line.equals(\{

this.updateList();

// 一般消息,又可以分为几种,对大家说, 与某个人交谈,或者私聊 } else {

String[] inmsg = line.split(\

if (inmsg[0].compareTo(\ {

ChatServer.sendOne(inmsg[1], \ + this.name);

} else if (inmsg[0].compareTo(\发送文件消 {

// String[] sendfile = line.split(\

ChatServer.sendOne(inmsg[1], \

+ this.name + \ + \

} else if (inmsg[0].compareTo(\接收文件 {

// String[] acceptfile = line.split(\ ChatServer

.sendOne(inmsg[1], inmsg[0] + \ } else if (inmsg[0].compareTo(\拒绝接收 {

// String[] refusefile = line.split(\ ChatServer

.sendOne(inmsg[1], inmsg[0] + \ } else if (!line.startsWith(\ { // 对所有人说

ChatServer.sendAll(this.name + \ }

// String[] inmsg = line.split(\ else if (inmsg[1].equals(\ {

if (ChatServer.sendOne(inmsg[2], \

29

+ name + \ { // success

out.println(\ + inmsg[2] + \ // 再发给自己一份

out.flush(); // 一定要有 } else {

out.println(inmsg[2] + \已经离开聊天室\ out.flush(); // 一定要记住要 } } else {

ChatServer.sendAll(\ + inmsg[2] + \ } // 发给所有的人

}

} catch (IOException e) {

System.out.println(e.toString()); try {

s.close();

} catch (IOException e2) { }

return; } }

}//end run }

30


计算机网络课程设计(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:我国房产税制度设计与探讨

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: