freeman167
freeman167

Reputation: 11

java.net.SocketException: Connection reset , anyone help me

I was learning tcp/ip protocol and i wrote a program yesterday. It's a simple code , client send a string, server receive and print it to console . but when i start , this become error . Anyone check it for me , please . Here's my code . Client side :

public class Client {
Socket client ;
DataInputStream is;
DataOutputStream os;
public Client(){
    try {
        client=new Socket("localhost", 7777);
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void send(String txt){
    try {
        os=new DataOutputStream(client.getOutputStream());
        if(os!=null && client!=null)
            os.writeBytes(txt);         
            System.out.println("Send OK");
            close();
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void close(){
    if(client!= null&& os!= null&& is!= null) {
    try{
    os.close();
    is.close();
    client.close(); 
    } 
    catch(UnknownHostException e) {
        System.err.println(e);
    } catch(IOException e) {
        System.err.println(e);
    }}}}

Client main :

public static void main(String[] args) {
    // TODO code application logic here
    Client client=new Client();
    client.send("hehea");

}

and the server :

public class ServerTCP {
PrintStream os;
DataInputStream is;
Socket client;
ServerSocket myserver;


public void open(){
    try {
        myserver=new ServerSocket(7777);
        System.out.println("Open Server ");
        client=myserver.accept();
        listen();
    } catch (IOException ex) {
        Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void listen(){
    try {
        System.out.println("\nListenning....");            
        is=new DataInputStream(client.getInputStream());
        os=new PrintStream(client.getOutputStream());
        String txt="";
        ReverseString result = null;
        while(true)
        {      
                    result=new ReverseString(is.readLine());                
                    System.out.println(is.readLine());
        }
    } catch (IOException ex) {
        Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE, null, ex);
    }       
}  
}

Server main

 public static void main(String[] args) {
    // TODO code application logic here
    ServerTCP server=new ServerTCP();
    server.open();

Finally , error in console

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.io.DataInputStream.readLine(DataInputStream.java:513)
at daochuoiservertcp.ServerTCP.listen(ServerTCP.java:50)
at daochuoiservertcp.ServerTCP.open(ServerTCP.java:34)
at daochuoiservertcp.Server.main(Server.java:21)

Upvotes: 1

Views: 2305

Answers (2)

user207421
user207421

Reputation: 310909

  1. You're reading lines but you aren't sending lines. You need to add a \n at least to the end of the sent message.
  2. You're reading infinitely and not checking for end of stream. The readLine() method returns null when the peer has disconnected. At the point you must exit the read loop and close the socket.
  3. You're reading twice per iteration. That will attempt to read two separate lines from the input. It won't give you the same line twice. It seems pointless.

Upvotes: 1

BurningDocker
BurningDocker

Reputation: 133

It's caused by the server or client close the connection when the other side haven't finish the IO operation. It'd better if you can paste the both main methods here. Then we can check it further

Upvotes: 0

Related Questions