我写了一个小小的聊天系统 可是当客户端向服务端说话的时候抛出很多异常 问题应该出在服务端,可是我怎么看都看不出来 希望大哥哥大姐姐帮我看看啊
服务端
import java.io.*;
import java.net.*;
public class ChatServer {
ServerSocket ss = null;
boolean start = false;
public static void main(String[] args) {
ChatServer server = new ChatServer();
server.start();
}
public void start() {
try {
ss = new ServerSocket(6666);
start = true;
while (start) {
Socket s = null;
s = ss.accept();
System.out.println("一个客户端链接成功");
ClientThread client = new ClientThread(s);
Thread tt = new Thread(client);
tt.start();
}
} catch (IOException e) {
System.out.println("申请绑定端口失败");
}
}
class ClientThread implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private boolean sts = false;
public ClientThread(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
sts = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (sts) {
try {
System.out.println("zhi xing dao sssssssssle ma ");
System.out.println(dis.readUTF());
System.out.println("zhi xing dao le ma ");
} catch (EOFException w) {
System.out.println("client close");
} catch (IOException e) {
System.out.println("a client slose");
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}
客户端
import java.io.*;
import java.net.*;
public class ChatServer {
ServerSocket ss = null;
boolean start = false;
public static void main(String[] args) {
ChatServer server = new ChatServer();
server.start();
}
public void start() {
try {
ss = new ServerSocket(6666);
start = true;
while (start) {
Socket s = null;
s = ss.accept();
System.out.println("一个客户端链接成功");
ClientThread client = new ClientThread(s);
Thread tt = new Thread(client);
tt.start();
}
} catch (IOException e) {
System.out.println("申请绑定端口失败");
}
}
class ClientThread implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private boolean sts = false;
public ClientThread(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
sts = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (sts) {
try {
System.out.println("zhi xing dao sssssssssle ma ");
System.out.println(dis.readUTF());
System.out.println("zhi xing dao le ma ");
} catch (EOFException w) {
System.out.println("client close");
} catch (IOException e) {
System.out.println("a client slose");
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}