qq_28076861
qq_28076861
2015-07-29 06:58

socket编程发送消息出现socket closed求大神,急

  • socket

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {

ServerSocket ss;
Socket  s ; 
Clients clients;
DataInputStream dis;

boolean startServer = false;

public void startServer(){


    try {
        ss   = new ServerSocket(8888);//8888是TCP端口号
        startServer = true;
        while(startServer){

             s = ss.accept();
            System.out.println(" connected!");

             clients = new Clients(s);
            new Thread(clients).start();    
        }
    }catch(IOException e){
        e.printStackTrace();
}finally{
    try {
        ss.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
public static void main(String[] args) {

    new Server().startServer();
}

}

class Clients implements Runnable{//包装类,把外部连接的client包装

private Socket s;
private boolean isConnected = false ;
private DataInputStream dis;


public Clients(Socket s) {

    this.s = s;

    try {
        isConnected = true;
        dis = new DataInputStream(s.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void run() {

    while(isConnected){

        try {
            String str = dis.readUTF();//阻塞:它会一直等待

System.out.println(str);
} catch (EOFException e){
System.out.println("Client is closed!");
}catch (IOException e1) {
e1.printStackTrace();
}finally{
try {
if(dis!=null)
{
dis.close();
}else{
;
}
if(s!=null)
{
s.close();
}else{
;
}
} catch (IOException e) {
e.printStackTrace();
}

}

    }   
}

}

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

Socket s;
PrintWriter pw;
DataOutputStream dos;
String line = "";
Scanner reader = new Scanner(System.in);

public void startClient(){

    try {
        s  = new Socket("127.0.0.1", 8888);

        dos = new DataOutputStream(s.getOutputStream());
        System.out.println("connected");
        do{
            System.out.print("please input:");

            line = reader.next();
            dos.writeUTF(line);
            dos.flush();

        }while(!line.equals("bye"));

            dos.close();
            s.close();

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {

    new Client().startClient();
}

}

java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at Clients.run(Server.java:75)
at java.lang.Thread.run(Unknown Source)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答