Aakash
Aakash

Reputation: 17

Java Socket Programming and Streams

I have written the following code for sending a text file from Client to Server using Sockets. I am new to both JAVA and Socket Programming. I feel I have got the socket concepts but Java Streams are quite confusing. Any help with the following code will be really helpful. PLEASE mention the mistake and Solution with reasoning.

The data seems to be sent just fine. But it is not received on the other end.

Server Code:

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

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(3000);
        while(true)
        {
            Socket convSocket = serverSocket.accept();
            System.out.println("connection accepted");

            BufferedReader in = new BufferedReader(new InputStreamReader(convSocket.getInputStream()));
            FileWriter fileOut = new FileWriter("/Users/aakashmalhotra/a.txt");

            int c;
            while( (c = in.read()) != -1){
                fileOut.write(c);
            }
            System.out.println("Transfer Done");
        }


    }
}

Client Code:

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

public class Client {
    public static void main(String[] argv) throws Exception {
        Socket clientSocket = new Socket("localhost", 3000); // create a socket

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedReader fileIn = new BufferedReader(new FileReader("/Users/aakashmalhotra/s.txt"));

        int c;
        while ((c = fileIn.read()) != -1) {
            out.write(c);
        }

    }
}

Upvotes: 0

Views: 194

Answers (2)

Jazzwave06
Jazzwave06

Reputation: 1851

IO objects, and especially streams, always need to be closed. This is because the OS is optimized for these type of operations, and it decides when the data should be sent on the pipe. Closing the resource make sure that the remainder of the data is sent. Example for the server side:

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

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(3000);
        while (true) {
            Socket convSocket = serverSocket.accept();
            System.out.println("connection accepted");

            try (BufferedReader in = new BufferedReader(new InputStreamReader(convSocket.getInputStream()))) {
                try (FileWriter fileOut = new FileWriter("/Users/aakashmalhotra/a.txt")) {
                    int c;
                    while ((c = in.read()) != -1) {
                        fileOut.write(c);
                    }
                    System.out.println("Transfer Done");
                }
            }
        }
    }
}

It is important to note the usage of a try-with-resource statement, which ensures that the streams will be closed regardless of the outcome of your application. This guarantees that your stream will always be closed and that no resource will leak.

Upvotes: 0

MeBigFatGuy
MeBigFatGuy

Reputation: 28568

You need to .flush() or .close() the out writer on the client side.

Upvotes: 2

Related Questions