Results 1 to 4 of 4
  1. #1
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default read from socket

    I have created a simple program that reads data from a file and sends them as characters using a socket.I have the following problem:
    The client sends the data to the server and when he has finished he closes the connection.
    I use read() to read the data but it keeps reading even after the connection has been closed.
    If the connection is closed will read() return -1?
    Another strange thing is that i need to flush my output stream otherwise server reads garbage.
    I think calling close() on socket flushes the stream.Is that correct?
    Java Code:
    package filetranfer;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class Client {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Give server's ip address");
    		String ipAddress = sc.next();
    		System.out.println("Give server's port");
    		int port = sc.nextInt();
    		System.out.println("Give file's name");
    		String fileName = sc.next();
    		sc.close();
    		try {
    			Socket socket = new Socket(InetAddress.getByName(ipAddress), port);
    
    			// output stream
    			BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(
    					socket.getOutputStream()));
    
    			// file stream
    			BufferedReader bufIn = new BufferedReader(new FileReader(fileName));
    
    			int character;
    			int i = 0;
    			while ((character = bufIn.read()) != -1) {
    				i++;
    				System.out.println(i + "bytes have been sent");
    				bufOut.write(character);
    			}
    
    			// close streams and socket
    
    			bufOut.flush();
    			socket.close();
    			bufIn.close();
    
    		} catch (UnknownHostException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			System.out.println("Wrong ip address");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println("Client exitting");
    	}
    }
    Java Code:
    package filetranfer;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Server {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Specify the server port");
    		int port = sc.nextInt();
    		sc.close();
    
    		try {
    			ServerSocket serverSocket = new ServerSocket(port);
    			Socket clientSocket = serverSocket.accept();
    
    			BufferedReader in = new BufferedReader(new InputStreamReader(
    					clientSocket.getInputStream()));
    
    			BufferedWriter out = new BufferedWriter(new FileWriter("foo.out"));
    
    			int character;
    
    			while ((character = in.read()) != 1) {
    				out.write(character);
    			}
    
    			serverSocket.close();
    			out.close();
    
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: read from socket

    Look at line 31 of your server. There is a very nasty typo in there. If you don't see it, change the code to this:

    Java Code:
                while ((character = in.read()) != 1) {
                    out.write(character);
    				System.out.println("WRITTEN: " + character);
                }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: read from socket

    Thanks!!I couldn't see it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: read from socket

    Quote Originally Posted by vas View Post
    Thanks!!I couldn't see it.
    I hope you've now learned how easy it is to make it so you do see it ;)

    EDIT:

    also a secondary tip, I don't know if this is the exact code that you were using to test but the fact that the scanner code is in there is just hell for testing. I changed your code to this to make the process a lot more speedy:

    Java Code:
    public class Client {
        public static void main(String[] args) {
            //Scanner sc = new Scanner(System.in);
            //System.out.println("Give server's ip address");
            //String ipAddress = sc.next();
            //System.out.println("Give server's port");
            //int port = sc.nextInt();
            //System.out.println("Give file's name");
            
    		String fileName = "test.txt";
            //sc.close();
            
    		try {
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9876);
     
                // output stream
                BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream()));
     
                // file stream
                BufferedReader bufIn = new BufferedReader(new FileReader(fileName));
     
                int character;
                int i = 0;
                while ((character = bufIn.read()) != -1) {
                    i++;
                    System.out.println(i + "bytes have been sent");
                    bufOut.write(character);
                }
     
                // close streams and socket
     
                bufOut.flush();
                socket.close();
                bufIn.close();
     
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Wrong ip address");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Client exitting");
        }
    }
    Java Code:
     
    public class Server {
     
        public static void main(String[] args) {
            //Scanner sc = new Scanner(System.in);
            //System.out.println("Specify the server port");
            int port = 9876;
            //sc.close();
     
            try {
                ServerSocket serverSocket = new ServerSocket(port);
                Socket clientSocket = serverSocket.accept();
     
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        clientSocket.getInputStream()));
     
                BufferedWriter out = new BufferedWriter(new FileWriter("foo.out"));
     
                int character;
     
                while ((character = in.read()) != 1) {
                    out.write(character);
    				System.out.println("WRITTEN: " + character);
                }
     
                serverSocket.close();
                out.close();
     
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    Invest a little effort to save yourself a lot of effort.
    Last edited by gimbal2; 03-18-2015 at 01:10 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. java.sql.SQLException: Socket read timed out
    By tapan6415 in forum Advanced Java
    Replies: 0
    Last Post: 10-05-2012, 02:02 PM
  2. Replies: 0
    Last Post: 05-24-2012, 08:37 PM
  3. Replies: 1
    Last Post: 07-02-2010, 01:18 PM
  4. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 06:49 PM
  5. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •