【问题标题】:simple client/server socket application SocketException: Connection reset [closed]简单的客户端/服务器套接字应用程序SocketException:连接重置[关闭]
【发布时间】:2012-11-26 12:55:27
【问题描述】:

所以我正在使用套接字做一个简单的客户端服务器程序。 主要是我从KnockKnockServer 获取代码,但我使用线程实现了客户端(这对我来说是必要的,因为我想稍后在 android 应用程序中使用它)

import java.net.*;
import java.io.*;

public class HelpMeServer {
public static void main(String[] args) throws IOException {
    Database db=new Database();
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(50000);
        System.out.println("Listening on port 911");
    } catch (IOException e) {
        System.err.println("Could not listen on port: 911.");
        System.exit(1);
    }

    Socket clientSocket = null;
    try {
        clientSocket = serverSocket.accept();
        System.out.println("incomig");
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }
    System.out.println("getting printwriter and bufferedreader");
    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = 
        new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    String inputLine, outputLine;


    Protocol kkp = new Protocol(db);
    // initiate conversation with client
    System.out.println("listening..");
//Here the exception is thrown.
    while ((inputLine = in.readLine()) != null) {
         kkp.processInput(inputLine);
         //if (outputLine.equals("Bye."))
            //break;
    }        out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();
}
}
import java.net.*;
import java.io.*;

public class Protocol {
private static final int WAITING = 0;
private static final int REGISTERING = 1;
private static final int LOGIN = 2;

private int state = WAITING;
private Database db;
private String email;
private String password;
public Protocol(Database db)
{
    this.db=db;
    email=null;
    password=null;
}
public void processInput(String theInput) {
    System.out.println("Cliet: "+theInput);


    if (state == WAITING) {
        if(theInput.equals("register"))
            {
            state=REGISTERING;
            System.out.println("set state to registering");
            }
        else if(theInput=="login"){
            state=LOGIN;
            System.out.println("set state to registering");
        }
        }


    else if(state == REGISTERING)
    {
        if(email==null) {
            email=theInput;
            System.out.println("set email to "+email);
        }
        else {
            password=theInput;
            System.out.println("saving "+email);
            db.addUser(new String(email), new String(password));
            password=null;
            email=null;
            state=WAITING;
        }
    }
}
}
 import java.io.*;
 import java.net.*;

public class NetworkingThread extends Thread{

Socket kkSocket;
PrintWriter out;
BufferedReader in;
public void run()
    {
    kkSocket = null;
    out = null;
    in = null;

    try {
        kkSocket = new Socket("localhost", 50000);
        out = new PrintWriter(kkSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: taranis.");
        System.exit(1);
    }   

}
public void close()
{
    out.close();
    try {
        in.close();
        kkSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void write(String s)
{
    out.println(s);  
    out.flush();
}
}

public class go {

/**
 * @param args
 */
public static void main(String[] args) {
        NetworkingThread n=new NetworkingThread();
    n.start();
    n.write("register");
    n.write("a@gmail.com");
    n.write("password");
    n.close();

}

 }

我的错误:

Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at HelpMeServer.main(HelpMeServer.java:34)

如果我使用此代码并放入 NetworkThread 的 run 方法,我的错误就会消失。但我不能在我的应用程序中使用它,我必须从外部调用这些方法

n.write("register");
n.write("a@gmail.com");
n.write("password");
n.close();

所以我根据您的建议进行了更改(在同一个地方仍然是同样的错误)

import java.net.*;
import java.io.*;

public class HelpMeServer {
 public static void main(String[] args) throws IOException {
    Database db=new Database();
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(50000);
        System.out.println("Listening on port 911");
    } catch (IOException e) {
        System.err.println("Could not listen on port: 911.");
        System.exit(1);
    }

   while(true)
   {
    Socket clientSocket = null;
    try {
        clientSocket = serverSocket.accept();
        System.out.println("incomig");
        ClientThread t=new ClientThread(clientSocket,db);
        t.start();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }
   }
  }
          }

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class ClientThread extends Thread {

Socket clientSocket;
Database db;
public ClientThread(Socket s,Database db)
{
    clientSocket=s;
    this.db=db;
}
public void run()
{
     System.out.println("getting printwriter and bufferedreader");
        PrintWriter out;
        try {
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = 
        new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                String inputLine, outputLine;


                Protocol kkp = new Protocol(db);
                System.out.println("listening..");
                while ((inputLine = in.readLine()) != null) {//error!
                     kkp.processInput(inputLine);
                     //if (outputLine.equals("Bye."))
                        //break;
                } 
                out.close();
               in.close();
               clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

  }

【问题讨论】:

    标签: java multithreading sockets client


    【解决方案1】:

    要完成这项工作,您必须将serverSocket.accept() 放入一个循环中。此时您收到一条消息然后关闭服务器套接字,因此任何后续连接到您的服务器的尝试都将被拒绝,因为没有服务器套接字侦听。

    【讨论】:

    • 循环应该看起来如何?
    • 你说你已经阅读了关于socket编程的java教程。页面末尾有一段标题为“支持多个客户端”,解释了它是如何工作的。
    • 嗯,我根据段落改变了它,但我仍然得到同样的错误。
    • 我编辑了上面的代码,所以你可以看到我的代码现在的样子
    • 您是否从程序中获得更多输出?
    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多