Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Send text file over socket

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Send text file over socket

    Hi!

    I'm trying to send a text file over a socket, from client to server, but I doesn't work. I will demonstrate a part of the code that isn't working.

    The critical part of the server look like this:

    while(true) {
                try {
                    Socket socket = fileSocket.accept();
                    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     
                    String s = in.readLine();

    And the client like this:

    Socket = new Socket(address, number);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Socket.getOutputStream()));
                BufferedReader in = new BufferedReader(new InputStreamReader(Socket.getInputStream()));
     
                BufferedReader fileIn = new BufferedReader(new FileReader(filename));
     
                String s = fileIn.readLine();
                out.write(s);


    Now I want the s in the server to hold the same string as s in the client, but the server can't read the stream - nothing happes.
    I know that readLine() reads until a new line character, but it doesn't word to write and read a single character either.

    Hank


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Send text file over socket

    know that readLine() reads until a new line character
    Does the client send the new line character that the readLine() method needs?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Send text file over socket

    Quote Originally Posted by iHank View Post
                String s = fileIn.readLine();
                out.write(s);
    Here you are writing (on the socket) a string that is not EOL (end-of-line) terminated. But the server expects a "line" (with EOL termination).
    Please, strive to establish a precise well known "protocol".
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Send text file over socket

    No, but i does not work to send anything at all. If i want to send an integer:

    while(true) {
                try {
                    Socket socket = fileSocket.accept();
                    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     
                    int i = in.read();

    And client:

    Socket = new Socket(address, number);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Socket.getOutputStream()));
                BufferedReader in = new BufferedReader(new InputStreamReader(Socket.getInputStream()));
     
                BufferedReader fileIn = new BufferedReader(new FileReader(filename));
     
                int i = 26
                out.write(i);

    I get no error messages...

    --- Update ---

    Seems like I forgot to flush....

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Send text file over socket

    Quote Originally Posted by iHank View Post
    int i = in.read();
    int i = 26
    out.write(i);
    Note that conceptually here you are writing/reading a "character".

    Quote Originally Posted by iHank View Post
    Seems like I forgot to flush....
    Yes, in general this is another important issue (apart the EOL vs non-EOL I have told before).
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Chat using TCP/IP, chat text,Voice,Icon,send file ....
    By hien281191 in forum Totally Off Topic
    Replies: 2
    Last Post: September 13th, 2013, 02:56 PM
  2. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  3. Would like to send hyperlink text value to another jsp
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: August 19th, 2011, 12:14 PM
  4. Send and Receive data from socket simultaneously
    By bigmac025 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 25th, 2011, 05:18 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM