0

I've been trying to fix this error for days now. Really. I just don't get it. The code is simple enough.. Why is it not working?

    public class server
    {
public static void main(String[] args)
{
    String fileName = null;

    try
    {
        ServerSocket ss = new ServerSocket(9999, 3);

        Socket dataSocket = ss.accept();
        System.out.println("Server: Connection Established");

        InputStream is = dataSocket.getInputStream();
        //BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Scanner sc = new Scanner(dataSocket.getInputStream());

        while(sc.hasNextByte())
        {
            System.out.println("True");
            fileName = fileName+sc.next();
        }

        FileWriter fw = new FileWriter("server"+fileName);
        while(dataSocket  != null)
        {
            fw.write(is.read());
        }

        fw.flush();
        dataSocket.close();
        ss.close();
            /*String[] nameParts;
            nameParts = fileName.split(".");
            File f = new File(nameParts[0]+"."+nameParts[1]);
            FileOutputStream fos = new FileOutputStream(f);
            System.out.println(nameParts[0]);*/
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
}

    }

Above is the server code. The client code is below:

    public class client 
    {
public static void main(String[] args) 
{
    // Read a file
    try 
    {
        Socket dataSocket = new Socket();

        dataSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9999));
        System.out.println("Client: Connection Established");

        OutputStream os = dataSocket.getOutputStream();
        PrintWriter pw = new PrintWriter(os);

        String fileName = "text.txt";

        pw.write(fileName);


        File f = new File(fileName);
        Scanner sc = new Scanner(f);

        /*while(sc.hasNextInt())
        {
            pw.write(sc.nextInt());
        }*/

        //pw.flush();
        //pw.close();

        //dataSocket.close();

        /*String[] nameParts = new String[2];
        nameParts = args[0].split(".");

        while(sc.hasNextByte())
        {
            os.write(sc.nextByte());5
        }*/
    }

    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

    }

At first, both client and server just kept waiting for each other to speak, like two shy teenagers. After a few changes, this started to happen. I get the Connection Reset error. Please help. Deadlines!

Asim
  • 6,962
  • 8
  • 38
  • 61

2 Answers2

2

sc.hasNextByte() remains true until the peer has closed the connection. So you will never get out of this loop. You need to send the filename length ahead of the filename, so you know how much of the incoming data is the filename. Then read the filename, open the file, and read and copy the rest of the stream until EOS.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Is there no way to do this without sending the name size first? I'm sure there is some way :s This is JAVA not C :D – Asim Apr 25 '12 at 17:56
  • @Asim Of course there are other ways. You could use `DataOutputStream.writeUTF()` and `DataInputStream.readUTF()` for the filename, for example. – user207421 Apr 26 '12 at 01:20
0

Optionally, you can write your file name with a lien as below:

pw.write(fileName + "\n");

Or user some writer to writeLine(fileName ).

And in server side, use bufferedReader to read line by line:

while ((line = br.readLine()) != null) {

....

}

Then it can finish the loop if the client is closed.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • That doesn't solve my problem :s I want to keep the connection open so I can send the file contents after I send the fileName. – Asim Apr 25 '12 at 17:55