public class soo {
public static void main(String[] args) {
new soo().launch();
}
public void launch() {
ServerSocket ss;
try {
ss = new ServerSocket(7790);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(new BufferedInputStream(
s.getInputStream()));
String str = dis.readUTF();
System.out.println(str);
new Thread(new Client(dis)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private class Client implements Runnable {
private DataInputStream dis;
public Client(DataInputStream dis) {
this.dis = dis;
}
@Override
public void run() {
while (true) {
try {
int type = dis.readInt();
System.out.println(type);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class So {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",7790);
DataOutputStream dos = new DataOutputStream(
s.getOutputStream());
dos.writeUTF("aaa");
dos.writeInt(55);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样为什么一直抛出connect reset的错误?
服务端那个必须一直不停的接受客服端的数据,所以我用了死循环,但是为什么会有错啊?

解决方案 »

  1.   

    因为客户端发送完55就结束了,程序退出了。与服务器端的socket连接断开了。
    服务器那端的socket,dis都不存在了,就抛出异常了。
    可以在Client类里的捕获异常处加一return 就让服务器端结束了。
    或者是让客户端不停地发送数字,服务器端不停接收。
      

  2.   

      客户端发送完数据后就关闭了,而服务器端却还在从流中读取数据。
       我猜的话,应该是 String str = dis.readUTF(); 这里抛出socketException异常,楼主可以debug一下
      

  3.   

    哎,无语了。。
    Socket s = new Socket(props.getProperty("server"), Integer
    .parseInt(props.getProperty("port")));
    s.setReceiveBufferSize(1024 * 500);
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
    s.getOutputStream()));
    DataInputStream dis = new DataInputStream(
    new BufferedInputStream(s.getInputStream()));
    dos.writeUTF(nickname);
    dos.writeInt(1);
    dos.writeInt(Integer.parseInt(username.getText().trim()));
    if(dis.readBoolean()==true){
    head=tk.getImage("src/images/defaulthead.gif");
    }else{
    int size = dis.readInt();
    byte[] b = new byte[size];
    dis.read(b);
    head=tk.createImage(b);
    }
    dis.close();
    dos.close();
    s.close();package org.miracle.chat;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.List;public class Server { public static final int PORT = 7790;
    public static final int SEND_HEAD = 1;
    public static final int GET_HEAD = 2;
    public static final int SEND_MESSAGE = 3;
    private ServerSocket ss;
    private List<Client> clients = new ArrayList<Client>(); public Server() {
    try {
    ss = new ServerSocket(PORT);
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    try {
    new Server().launch();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } private void launch() throws IOException {
    while (true) {
    Socket s = ss.accept();
    s.setReceiveBufferSize(1024 * 500);
    s.setSendBufferSize(1024 * 500);
    Client c = new Client(s);
    clients.add(c);
    new Thread(c).start();
    }
    } private void sendHead(Socket s, DataInputStream dis, DataOutputStream dos)
    throws IOException {
    int filename = dis.readInt();
    File file = new File("d:/icons/" + filename + ".gif");
    if (!file.exists()) {
    dos.writeBoolean(true);
    return;
    }
    FileInputStream fis = new FileInputStream(file);
    int size = (int) file.length();
    byte b[] = new byte[size];
    fis.read(b);
    dos.writeBoolean(false);
    dos.writeInt(size);
    dos.write(b);
    dos.flush();
    } public void getHead(Socket s, DataInputStream dis) throws IOException {
    int filename = dis.readInt();
    File file = new File("d:/icons/" + filename + ".gif");
    if (file.exists())
    file.delete();
    byte b[] = new byte[dis.readInt()];
    dis.read(b);
    BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(file));
    bos.write(b);
    bos.flush();
    bos.close();
    } private class Client implements Runnable { private Socket s;
    private DataInputStream dis;
    private DataOutputStream dos;
    private String nickname; public Client(Socket s) {
    this.s = s;
    try {
    dis = new DataInputStream(new BufferedInputStream(
    s.getInputStream()));
    dos = new DataOutputStream(new BufferedOutputStream(
    s.getOutputStream()));
    } catch (IOException e) {
    e.printStackTrace();
    }
    } @Override
    public void run() {
    try {
    nickname = dis.readUTF();
    int type = dis.readInt();
    switch (type) {
    case SEND_HEAD:
    Server.this.sendHead(s, dis, dos);
    break;
    case GET_HEAD:
    Server.this.getHead(s, dis);
    break;
    case SEND_MESSAGE:
    Server.this.interact(s, dis, nickname);
    break;
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } public void interact(Socket s, DataInputStream dis, String myname)
    throws IOException {
    String nickanme = dis.readUTF();
    String message = dis.readUTF();
    for (int i = 0; i < clients.size(); i++) {
    if (clients.get(i).nickname.equals(nickanme)) {
    clients.get(i).dos.writeUTF(myname);
    clients.get(i).dos.writeUTF(message);
    break;
    }
    }
    }
    }
      

  4.   

    这是我程序的部分代码,还是那个什么connection reset...郁闷
      

  5.   

    楼主的目的是什么呢?while (true) {
    try {
    int type = dis.readInt();  //第一次执行的时候可以读出内容,此时dis已经到达末尾,没数据了,第二次读的时候也就抛异常了
    System.out.println(type);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  6.   

    如果楼主希望不会报错的话,可以这么改。不知道是否达到楼主需求就是了。
    public void run() {
    try {
    while (dis.available() != 0) {
    int type = dis.readInt();
    System.out.println(type);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }