(Java多客户端服务器)当客户端关闭套接字并且服务器写入客户端时没有异常
服务器代码:(Java多客户端服务器)当客户端关闭套接字并且服务器写入客户端时没有异常,java,sockets,client-server,Java,Sockets,Client Server,服务器代码: public class Main { public static void main(String args[]){ Socket s=null; ServerSocket ss2=null; System.out.println("Server Listening......"); try{ ss2 = new ServerSocket(8080); }
public class Main {
public static void main(String args[]){
Socket s=null;
ServerSocket ss2=null;
System.out.println("Server Listening......");
try{
ss2 = new ServerSocket(8080);
}
catch(IOException e){
e.printStackTrace();
System.out.println("Server error");
}
while(true){
try{
s = ss2.accept();
ss2.setReuseAddress(true);
System.out.println("connection Established");
ServerThread st=new ServerThread(s);
st.start();
}
catch(Exception e){
e.printStackTrace();
System.out.println("Connection Error");
}
}
}
}
class ServerThread extends Thread{
String line; = "testing";
PrintWriter os=null;
Socket s=null;
public ServerThread(Socket s){
this.s=s;
}
public void run() {
try{
os=new PrintWriter(s.getOutputStream());
}catch(IOException e){
System.out.println("IO error in server thread");
}
try {
for (int i = 0 ; i < 100 ; i++){
os.println(line);
os.flush();
Thread.sleep(1000);
}
}
catch(NullPointerException | IOException | InterruptedException e){
System.out.println("Client "+line+" Closed");
}
finally{
try{
System.out.println("Connection Closing..");
if(os!=null){
os.close();
System.out.println("Socket Out Closed");
}
if (s!=null){
s.close();
System.out.println("Socket Closed");
}
}
catch(IOException ie){
System.out.println("Socket Close Error");
}
}//end finally
}
公共类主{
公共静态void main(字符串参数[]){
套接字s=null;
ServerSocket ss2=null;
System.out.println(“服务器侦听…”);
试一试{
ss2=新服务器插座(8080);
}
捕获(IOE异常){
e、 printStackTrace();
System.out.println(“服务器错误”);
}
while(true){
试一试{
s=ss2.accept();
ss2.setReuseAddress(正确);
System.out.println(“已建立连接”);
ServerThread st=新的ServerThread;
st.start();
}
捕获(例外e){
e、 printStackTrace();
System.out.println(“连接错误”);
}
}
}
}
类ServerThread扩展线程{
字符串行;=“测试”;
PrintWriter os=null;
套接字s=null;
公共服务器线程(套接字){
这个.s=s;
}
公开募捐{
试一试{
os=新的PrintWriter(s.getOutputStream());
}捕获(IOE异常){
System.out.println(“服务器线程中的IO错误”);
}
试一试{
对于(int i=0;i<100;i++){
os.println(行);
os.flush();
睡眠(1000);
}
}
捕获(NullPointerException | IOException | InterruptedException e){
系统输出打印项次(“客户端”+行+“关闭”);
}
最后{
试一试{
System.out.println(“连接关闭”);
如果(os!=null){
os.close();
System.out.println(“插座关闭”);
}
如果(s!=null){
s、 close();
System.out.println(“插座关闭”);
}
}
捕获(IOIE){
System.out.println(“套接字关闭错误”);
}
}//最后结束
}
当客户端连接到服务器时,服务器开始println。但是当客户端关闭套接字时,服务器仍然保持println。假设服务器将引发异常
任何帮助都将不胜感激。我认为您应该在完成while后添加
ss2.close()
和s.close()
while(true){
try{
s = ss2.accept();
ss2.setReuseAddress(true);
System.out.println("connection Established");
ServerThread st=new ServerThread(s);
st.start();
}
catch(Exception e){
e.printStackTrace();
System.out.println("Connection Error");
}
}
ss2.close();
s.close();
PrintWriter
接受异常。请参阅JavadocServerSocket
之后调用setReuseAddress()
,就像在循环中调用它一样,完全没有意义s.close()
将只关闭最近接受的套接字,这应该发生在服务器线程中,而不是在这里,以便将它们全部关闭。所有这些都不能回答这个问题。谢谢。我发现,当我在java server中使用ObjectOutputStream时,当客户端断开连接时,它会引发异常,这是我想要的。但是,使用StreamReader In C#无法接收。您建议我使用什么?另外,我正在编写一个客户机(C#)-服务器(java)程序。只有PrintXXX
类会出现异常。您应该使用什么取决于您尝试发送的内容。请查看DataOutputStream。