package fuwuqi;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.sun.corba.se.spi.orbutil.fsm.Input;
import Z0929.MessageIO;
public class Fuwuqi {
// 接收信息
// 注册信息-1 登录信息-2 聊天信息-3
private static ServerSocket server;
// 套接字列表
List<Socket> ss = new ArrayList<Socket>();
static {
try {
server = new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
}
public Fuwuqi() {
while (true) {
try {
// 和客户端建立连接
Socket s = server.accept();
ss.add(s);// 将套接字保存在ss列表中
new Thread() {
public void run() {
while (true) {
// System.out.println("建立连接");
// 接收该客户端的信息
InputStream in;
try {
in = s.getInputStream();
byte[] b = new byte[1024];
int i = in.read(b);
String message = new String(b, 0, i);
// 判断信息:注册1 登录2 聊天3
String type = message.substring(0, message.indexOf(" "));
if (type.equals("1")) {
// System.out.println("注册");
// 注册操作处理
// (1)判断用户名是否存在
// 截取客户端输入的用户名,跟已保存的用户信息中的用户名比较
String user = message.substring(message.indexOf(" ") + 1);
String[] mess = user.split(" ");
String userName = mess[0];
String users = "";
String filePath = "e:\\xxx.txt";
FileReader fr = new FileReader(filePath);
char[] c = new char[1024];
int i2 = fr.read(c);
while (i2 != -1) {
users += new String(c, 0, i2);
i2 = fr.read(c);
}
fr.close();
boolean flag = false;
String[] userss = users.split("\n");
for (int j = 0; j < userss.length; j++) {
String un = userss[j].split(" ")[0];
if (un.equals(userName)) {
flag = true;
break;
}
}
OutputStream out = s.getOutputStream();
if (flag) {
// 返回n 代表用户名已存在
out.write('n');
return;
}
// (2)将信息保存在服务器文件中
FileWriter fw = new FileWriter("e:\\xxx.txt", true);
if (!users.trim().equals("")) {
fw.write("\n");
}
fw.write(user);
fw.close();
// 发y表示 用户名没有重复,注册成功
out.write('y');
// 登录、聊天信息操作
} else if (type.equals("2")) {
// 登录操作
// System.out.println("d");
String un = "";
// 把客户端输入的消息截取出从用户名到密码的部分
String user = message.substring(message.indexOf(" ") + 1);
FileReader fr = new FileReader("e:\\xxx.txt");
char[] c = new char[1024];
int i2 = fr.read(c);// 读取所有用户的信息存到字符串中
while (i2 != -1) {
un += new String(c, 0, i2);
i2 = fr.read();
}
fr.close();
OutputStream out = s.getOutputStream();
String[] s = un.split("\n");// 以"换行"对读取到的信息字符串进行分割
for (int j = 0; j < s.length; j++) {// 依此跟输入的登录信息比较
if (user.equals(s[j])) {
// 用户名密码不符合,登录失败
out.write('y');
}
}
// 用户名密码都符合,登录成功
out.write('n');
} else if (type.equals("3")) {
// 聊天信息操作
new Thread() {
public void run() {
OutputStream soOut = null;
String user = message.substring(message.indexOf(" ") + 1);
try {
soOut = s.getOutputStream();
soOut.write(user.getBytes());
soOut.flush();
} catch (IOException e) {
ss.remove(soOut);
}
}
// }
}.start();
}
} catch (IOException e) {
break;
}
}
}
}.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Fuwuqi();
}
}
即时内部聊天程序——服务器
最新推荐文章于 2020-02-16 17:01:44 发布