English 中文(简体)
爪哇聊天室... 为什么我得到一个套接字例外?
原标题:Java Chat Room- Why am I getting a SocketException?

I am making a chat room using simple socket-socket connections. I have a server and client program. The server runs on port 225, and then when I run the client on port 225 so that they can read/write to the sockets, the client instantly stops with the error message

java.net.SocketException: Connection resetJava Result: 1

为何要提出这个例外? 它连接, 线被打印到控制台, 显示 :

try {
    client = new Socket(serverAdress, portNumber);
    System.out.println("Successful connection to server on port " + portNumber + ".");

那么为什么它不能连接起来呢?

这是例外追踪...

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at schoolroom10.SchoolRoomClient.SchoolRoomClientWindow.main(SchoolRoomClientWindow.java:85)

这是我的服务器代码...

public class SchoolRoomServer {

    //  server  will create the server over the local port:
    static ServerSocket serverSocket = null;

    //Socket for listening:
    static Socket clientcommunicate = null;

    //portnum is the number of the local port. Change to required port before running.
    static int portnum = 225;

    public static void main(String[] args) throws IOException{
        try {
            serverSocket = new ServerSocket(portnum);
        clientcommunicate = serverSocket.accept();
            Communicate c = new Communicate(Communicate.server);
            Thread t = new Thread(c);
            t.start();
        } catch (UnknownHostException uhe) {
            System.err.println(uhe);
            System.exit(1);
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }


    } 
}

class Communicate implements Runnable {
    public static Socket server;
    Communicate(Socket server) {
        Communicate.server = server;
    }
    public void run() {
        String in = "";
        try {
            //i/o for clients:
            PrintStream output = new PrintStream(server.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream()));
            String message;

            //Message handling:
            while(((message = input.readLine()) != null) && (!in.equals("."))) {
                output.println(message);
            }
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }
}
最佳回答

连接重置通常意味着您已经写入了已经关闭到另一端的连接 。 一般说来, 这是 pplicaston 协议错误 。 但是在某些情况下, 比如, 当端点是一个浏览器, 或者在您的情况中, 它只是当端点选择随时离开的必然结果 。 在这种情况下, 您所能做的就是关闭套接字, 忘记连接 。

问题回答

如以往的海报指出,您应避免使用1024以下的港口,因为这些港口是保留港口。

看不到所有的代码, 剩下的只是猜测, 但是在客户端, 您应该进入一个循环, 从输入流中不断准备, 并处理收到的信息, 然后在重复前稍作暂停, 例如睡眠( 100) 。 相同的服务器结束, 用于监控连接请求和收到的信息 。

如果您不继续读取输入流, 它会填充缓冲, 并丢弃连接, 但有一个例外 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签