[英]Reading from my Socket input stream using bufferedReader.readLine(), it throws an SocketException stating “Connection reset”
I am creating two sockets: a client socket and a server socket. 我正在创建两个套接字:客户端套接字和服务器套接字。
ServerSock class: ServerSock类:
public class ServerSock {
public static void main(String args[]) throws Exception{
//creating welcome socket @param: port number
System.out.println("Server is started...");
ServerSocket serverSocket = new ServerSocket(55555);
System.out.println("Server is wating for client request...");
//creating individual sockets for clients
Socket socket0 = serverSocket.accept();
System.out.println("Client connected...");
//reading the input data
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket0.getInputStream()));
//reading the data from bufferedReader
String str = bufferedReader.readLine();
//console the data
System.out.println(" client data : " + str);
}
}
ClientSocket class: ClientSocket类:
public class ClientSocket {
public static void main(String args[]) throws Exception{
//ip address of the server
String ip = "localhost";
//port number of the application
int port = 55555;
//creating a socket
Socket socket = new Socket(ip, port);
//data to send
String data0 = "lakshan waruna";
//converting data to ByteStream param: where to send the data
OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream());
//writing the data
os.write(data0);
os.flush();
}
}
ServerSock
runs until I start the client socket. ServerSock
一直运行到启动客户端套接字为止。 It also prints the "client connected" statement in the ServerSock
class. 它还在ServerSock
类中打印“ client connected”语句。 But when it tries to read data from bufferedReader using the statement String str = bufferedReader.readLine();
但是,当它尝试使用语句String str = bufferedReader.readLine();
从bufferedReader读取数据时String str = bufferedReader.readLine();
, it throws a SocketException
stating "connection reset". ,则抛出SocketException
指出“连接重置”。 How can I fix this issue? 如何解决此问题?
Your problem is you don't close the Socket
in the client, "Connection reset" is usually thrown when a remote Socket
has ungracefully closed a connection which is the case here since you don't call Socket.close()
. 您的问题是您没有在客户端中关闭Socket
,当远程Socket
不良地关闭了连接时,通常会抛出“连接重置”(在这种情况下,因为您不调用Socket.close()
。 If you do you'll notice it will work but you're probably thinking well why does that fix it? 如果您会注意到它会起作用,但您可能想的很好,为什么能解决此问题? Why not just give me the data then next read tell me the Socket
has closed, well it does but since you're using readLine()
which constantly reads until it has received \\n
or \\r
or if the given InputStream
returns less than zero in read
. 为什么不只给我数据,然后下次读取告诉我Socket
已关闭,它确实可以,但是由于您使用的是readLine()
,它会一直读取直到收到\\n
或\\r
或给定的InputStream
返回小于零的值。 read
。 Pretty much it is reading until it has received a full line (to declare a full line like System.out.println
does you just add \\n
at the end), and it has still yet to find a new line, if you add \\n
at the end of the message you'll notice it will also work. 它几乎一直在读取,直到收到完整的一行为止(要声明为System.out.println
类的完整行,您只是在末尾添加\\n
),如果您添加\\n
,它仍未找到新行\\n
在消息的最后,你会发现它也可以工作。
Also, remove PrintWriter
, you never use it. 另外,删除PrintWriter
,您将永远不会使用它。
ALWAYS CLOSE A SOCKET . 总是关闭袜子 。
Tested your code, worked for me. 测试了您的代码,为我工作。
Edited typo and included link to plausible information on question 编辑错字,并包含指向有关问题的合理信息的链接
Possible reasons for connection reset could be that the socket is closed before sending the data. 连接重置的可能原因可能是套接字在发送数据之前已关闭。
java.net.SocketException: Connection reset java.net.SocketException:连接重置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.