furiousNoob
furiousNoob

Reputation: 53

Java server connection reset

I've been trying to fix "connection reset" problem in a simple java server-client program for a while.

My scenario is like this

client program will take filename as input, then send it to server program. Server will check if that file exists in the directory. If exist then server will print "ok", otherwise "file not found"

I'm getting this execption java.net.SocketException: Connection reset

Server program

package tcpserver;
import java.net.*;
import java.io.*;

public class TCPServer {

    ServerSocket serversocket;
    Socket socket;
    BufferedReader buffread, buffout;

    String filename;
    String strDir = "D:\";

    private void findFile(String name) {

        File fileObj = new File(strDir);
        File[] fileList = fileObj.listFiles();
        if (fileList != null) {
            for (File indexFile : fileList) {
                if (name.equalsIgnoreCase(indexFile.getName())) {
                    System.out.println("200 ok ");
                } else {
                    System.out.println("File Not found");
                }
            }
        }
    }

    public TCPServer() {
        try {
            //creating server object
            serversocket = new ServerSocket(6666);
            socket = serversocket.accept();
            //get input stream through the socket object from buffer
            buffread = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            filename = buffread.readLine();
            findFile(filename);
        } catch (IOException ex) {
            //System.err.println(ex);
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TCPServer serverObject = new TCPServer();
    }
 }

Client program

package tcpclient;

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

public class TCPClient {

    BufferedReader bffread, bffinput;
    String fileInput;

    public TCPClient() {
        try {
            //Creating socket
            Socket socket = new Socket("localhost", 6666);
            System.out.println("Enter filename");
            bffinput = new BufferedReader(new InputStreamReader(System.in));
            OutputStream outputObject = socket.getOutputStream();

        } catch (Exception ex) {
            System.out.println("Unhandled exception caught");
        }
    }

    public static void main(String[] args) {
        TCPClient clientObject = new TCPClient();
    }   
}

Exception stack

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
    at tcpserver.TCPServer.<init>(TCPServer.java:38)
    at tcpserver.TCPServer.main(TCPServer.java:47)*

Any help/suggestion is appreciated. Thanks in advance

Upvotes: 0

Views: 1595

Answers (2)

Adi
Adi

Reputation: 2394

Client

Your client programme is not reading anything from console and sending it over to socket. Change it to something like this..

public TCPClient() {
    try {
        //Creating socket
        Socket socket = new Socket("localhost", 6666);
        System.out.println("Enter filename");
        bffinput = new BufferedReader(new InputStreamReader(System.in));
        String filename = bffinput.readLine();
        OutputStream outputObject = socket.getOutputStream();
        // send filename over socket output stream
        outputObject.write(value.getBytes());

    } catch (Exception ex) {
        System.out.println("Unhandled exception caught");
    }
}

Upvotes: 1

Thomas Stets
Thomas Stets

Reputation: 3043

Your server accepts the connection, but never sends anything back. The "200 OK" message gets written to stdout, not to the socket. Then the server terminates, closing the connection. At that time the client, still waiting for data, gets the exception.

I guess you want to send "200 OK" the client. So you have to pass the socket, or at least the OutputStream of the socket to findFile(), and write the response into that.

Alternatively, and a bit cleaner: return the response string from findFile(), and send it in the calling method, so findFile() doesn't even need to know about sending the response.

You should also close the socket in the block where you open it, so that data that might still be in a buffer in memory will be sent.

Upvotes: 2

Related Questions