Results 1 to 4 of 4
Thread: read from socket
- 03-18-2015, 11:33 AM #1
Member
- Join Date
- Jul 2014
- Posts
- 7
- Rep Power
- 0
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(); } } }
- 03-18-2015, 11:59 AM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 03-18-2015, 12:51 PM #3
Member
- Join Date
- Jul 2014
- Posts
- 7
- Rep Power
- 0
Re: read from socket
Thanks!!I couldn't see it.
- 03-18-2015, 01:01 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: read from socket
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(); } } }
Last edited by gimbal2; 03-18-2015 at 01:10 PM.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
java.sql.SQLException: Socket read timed out
By tapan6415 in forum Advanced JavaReplies: 0Last Post: 10-05-2012, 02:02 PM -
Extra Q needed between buffered socket read and buffered file write? (multithreaded)
By lark1313 in forum Threads and SynchronizationReplies: 0Last Post: 05-24-2012, 08:37 PM -
Socket and how to launch thread for receiving socket messages
By newbiejava in forum New To JavaReplies: 1Last Post: 07-02-2010, 01:18 PM -
Problem reading from socket using read(bytes[])
By sm123 in forum New To JavaReplies: 1Last Post: 04-21-2010, 06:49 PM -
append response to the request from Socket and write to another socket
By vaibhav_singh_vs@yahoo.co in forum NetworkingReplies: 3Last Post: 04-17-2009, 07:02 PM
Bookmarks