[英]java socket problems connection reset by peer
I am working in a project in uni and i need to create a server client application with sockets , right now i just need to send a object throw but i am having some problems.It would be very helpful for me if someone pointed me out how to solve this error , i tried a lot of things and looked online a lot but coudent find anything helpfull , sorry for bad english and thanks in advance this is the exception i am getting. 我正在uni的一个项目中工作,我需要创建一个带有套接字的服务器客户端应用程序,现在我只需要发送一个对象抛出即可,但是我遇到了一些问题。如果有人指出我的做法,这对我非常有帮助为了解决此错误,我尝试了很多事情,并在网上进行了很多查找,但是coudent找到了一些有用的信息,对不起英语不好,在此先感谢您获得的例外。
Server output 服务器输出
Server started--1
Server started
new connection , all conections are :1
Starting thread for client 1 at Thu Mar 29 14:32:48 CEST 2018
Client 1's host name is 127.0.0.1
Client 1's IP Address is 127.0.0.1
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
at Server$HandleClient.run(Server.java:61)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Server Code 服务器代码
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.concurrent.*;
public class Server {
private ExecutorService executor = Executors.newCachedThreadPool();
private int connectedDevices=0;
private int portNr =8090;
private boolean running=true;
private int clientNo=0;
public Server(){
new Thread(()->{
try{
System.out.println("Server started--1");
ServerSocket serverSocket = new ServerSocket(portNr);
System.out.println("Server started");
while(running){
Socket socket = serverSocket.accept();
clientNo++;
connectedDevices++;
System.out.println("new connection , all conections are :"+connectedDevices);
System.out.println("Starting thread for client " + clientNo +" at " + new Date() );
InetAddress inetAddress = socket.getInetAddress();
System.out.println("Client " + clientNo + "'s host name is "+ inetAddress.getHostName() );
System.out.println("Client " + clientNo + "'s IP Address is "+ inetAddress.getHostAddress() );
executor.execute(new HandleClient(socket));
}
}catch(Exception e){
e.printStackTrace();
}
}).start();
}
public synchronized void decrement(){
connectedDevices--;
System.out.println("lost connection , all conections are :"+connectedDevices);
}
public static void main(String [] args){
Server s=new Server();
}
class HandleClient implements Runnable{
private Socket socket;
public HandleClient(Socket socket){
this.socket=socket;
}
public void run() {
try{
ObjectOutputStream dos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream din = new ObjectInputStream(socket.getInputStream());
String msg="";
System.out.println(msg);
Date date = (Date) din.readObject();
System.out.println("(Client) Current time is: " + new Date() );
System.out.println("(Client) Object read from server : " + date );
decrement();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
And finally the client code 最后是客户端代码
import java.io.*;
import java.net.*;
import java.util.Date;
public class Client {
public static void main(String[] args) {
try {
System.out.println("before socket");
Socket s = new Socket("localhost",8090);
System.out.println("after socket");
ObjectOutputStream dos = new ObjectOutputStream(s.getOutputStream());
System.out.println("before sending");
dos.writeObject(new Date());
System.out.println("after sending");
}catch(IOException e) {
e.printStackTrace();
}
}
}
Why do you think there is anything wrong with your program. 您为什么认为程序有什么问题。 It is working as expected. 它按预期工作。
However, worth noticing is your code, I believe you want the client and server to run indefinitely. 但是,值得注意的是您的代码,我相信您希望客户端和服务器无限期运行。
But where is the infinite loop? 但是无限循环在哪里? You don't have one. 你没有一个。 So, the client will write a Date object, and the Server will read it. 因此,客户端将编写一个Date对象,而Server将读取它。 Just ONCE. 就一次。
For continuity, I took your code, added two infinite loops and it works. 为了连续性,我采用了您的代码,添加了两个无限循环,并且它可以正常工作。
import java.io.ObjectInputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Server { private ExecutorService executor = Executors.newCachedThreadPool(); private int connectedDevices = 0; private int portNr = 8090; private boolean running = true; private int clientNo = 0; private ServerSocket serverSocket; public Server() { new Thread(() -> { try { System.out.println("Server started--1"); serverSocket = new ServerSocket(portNr); System.out.println("Server started"); while (running) { Socket socket = serverSocket.accept(); clientNo++; connectedDevices++; System.out.println("new connection , all conections are :" + connectedDevices); System.out.println("Starting thread for client " + clientNo + " at " + new Date()); InetAddress inetAddress = socket.getInetAddress(); System.out.println("Client " + clientNo + "'s host name is " + inetAddress.getHostName()); System.out.println("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress()); executor.execute(new HandleClient(socket)); } } catch (Exception e) { e.printStackTrace(); } }).start(); } public synchronized void decrement() { connectedDevices--; System.out.println("lost connection , all conections are :" + connectedDevices); } public static void main(String[] args) { new Server(); } class HandleClient implements Runnable { private Socket socket; private ObjectInputStream din; public HandleClient(Socket socket) { this.socket = socket; } public void run() { try { din = new ObjectInputStream(socket.getInputStream()); String msg = ""; while (true) { System.out.println(msg); Date date = (Date) din.readObject(); System.out.println("(Client) Current time is: " + new Date()); System.out.println("(Client) Object read from server : " + date); decrement(); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } } } }
Client: 客户:
import java.io.IOException; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Date; public class Client { private static Socket s; public static void main(String[] args) throws InterruptedException { try { System.out.println("before socket"); s = new Socket("localhost", 8090); System.out.println("after socket"); ObjectOutputStream dos = new ObjectOutputStream(s.getOutputStream()); while (true) { System.out.println("before sending"); dos.writeObject(new Date()); System.out.println("after sending"); Thread.sleep(1000); } } catch (IOException e) { e.printStackTrace(); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.