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 12 of 12

Thread: Creating an EOF and Acknowledgement on client side

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating an EOF and Acknowledgement on client side

    I have this client and multi threaded server and need to do an end of file and an acknowledgement but it wont seem to work because of the errors can anyone help me plsss ?

     
    import java.io.*;
    import java.net.*;
     
    class NetworkFileClient1
    {
      public static void main(String args[]) throws IOException
      {
        DatagramSocket socket = new DatagramSocket(2019); //setting up port number to communicate with server
       socket.setSoTimeout(1000);
       boolean received = false;
     
     
          String message = new String("HELO\r\n");
          DatagramPacket packet = new DatagramPacket(
            message.getBytes(), message.length(),
            InetAddress.getByName("127.0.0.1"), 87); //Show how server was found by using getByName method.
            										//Int address Returns the IP address of the machine from which the datagram was received or sent.(of the server)
     
          socket.send(packet);
          System.out.println("Request sent!"); //when client get the server destination details by the getname method it will send a request to server
     
          byte buffer[] = new byte[1090000];
          boolean receiving = true;
     
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("CopyofTextfile.txt"))); // check
     
          while (receiving) { //when request is sent to server the client will display a . while receiving the datagram
          	try {
     
     
          	DatagramPacket getbitoffile =  new DatagramPacket(buffer, buffer.length);
     
            socket.receive(getbitoffile);
     
            buffer = getbitoffile.getData(); //returns the data buffer
     
    		if (!(lineFromServer.equals("***EOF***"))) {
     
            pw.println(getbitoffile); // Writing lines being received to text file
     
     
            System.out.print(".");
     
    			lineFromServer = new String(receivePacket.getData(), 0, receivePacket.getLength());
    		}
    					if (lineFromServer.equals("***EOF***"))) {
     
    						receiving = false;
    						System.out.println("File Received...");
    						pw.close;
    					}
     
    			catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
              receiving = false;
              System.out.println("Timeout. Got fed up waiting...");
     
     
    	  }
        }
      }
    }

     
    //Server
     
    import java.net.*;
    import java.io.*;
     
    //client
    class Worker implements Runnable {
     
        DatagramSocket socket = null;
        DatagramPacket packet = null;
     
        public Worker (DatagramSocket socket, DatagramPacket packet) {
            this.socket = socket;
            this.packet = packet;
        }
        public void run() {
     
           try {
           	BufferedReader fileCopyBuffer = new BufferedReader(new InputStreamReader(new FileInputStream ("NormanConquest.txt")));
          String strFromClient;
          String line;
     
     
          while ((line = fileCopyBuffer.readLine()) != null)
          {
     
     
            DatagramPacket response = new DatagramPacket(line.getBytes(), line.length(),packet.getAddress() , packet.getPort());
           System.out.print(".");
      	  socket.send(response);
     
      	  strLine = "***EOF***";
       			buf = strLine.getBytes();
       			odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
       			ds.send(odp);
    			System.out.println("File transfer is complete!");
     
     
        	}
     
        }
     
     
        catch (Exception e){;}; // might consider saving in text file
     
        }
     
    }
    class Server  {
     
     public  static void main(String Args[]){
        	try {DatagramSocket socket = new DatagramSocket(87);
     
            while (true) {
     
            		byte buffer[] = new byte[66508];
                  	DatagramPacket getstartpacket =
                                new DatagramPacket(buffer, buffer.length);
                    socket.receive(getstartpacket);
                    new Thread( new Worker(socket, getstartpacket)).start();
     
                       }
                } catch (Exception e) {;};
       }
    }

    These are errors given to me by the server when compiled

    Server.java:30: error: cannot find symbol
    strLine = "***EOF***";
    ^
    symbol: variable strLine
    location: class Worker
    Server.java:31: error: cannot find symbol
    buf = strLine.getBytes();
    ^
    symbol: variable buf
    location: class Worker
    Server.java:31: error: cannot find symbol
    buf = strLine.getBytes();
    ^
    symbol: variable strLine
    location: class Worker
    Server.java:32: error: cannot find symbol
    odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
    ^
    symbol: variable odp
    location: class Worker
    Server.java:32: error: cannot find symbol
    odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
    ^
    symbol: variable buf
    location: class Worker
    Server.java:32: error: cannot find symbol
    odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
    ^
    symbol: variable buf
    location: class Worker
    Server.java:32: error: cannot find symbol
    odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
    ^
    symbol: variable idp
    location: class Worker
    Server.java:32: error: cannot find symbol
    odp = new DatagramPacket(buf, buf.length, idp.getAddress(), idp.getPort());
    ^
    symbol: variable idp
    location: class Worker
    Server.java:33: error: cannot find symbol
    ds.send(odp);
    ^
    symbol: variable odp
    location: class Worker
    Server.java:33: error: cannot find symbol
    ds.send(odp);
    ^
    symbol: variable ds
    location: class Worker
    10 errors

    Tool completed with exit code 1

    And these are those of the client

    NetworkFileClient1.java:46: error: illegal start of expression
    if (lineFromServer.equals("***EOF***"))) {
    ^
    NetworkFileClient1.java:50: error: not a statement
    pw.close;
    ^
    NetworkFileClient1.java:53: error: 'catch' without 'try'
    catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
    ^
    NetworkFileClient1.java:53: error: ')' expected
    catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
    ^
    NetworkFileClient1.java:53: error: not a statement
    catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
    ^
    NetworkFileClient1.java:53: error: ';' expected
    catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
    ^
    NetworkFileClient1.java:28: error: 'try' without 'catch', 'finally' or resource declarations
    try {
    ^
    NetworkFileClient1.java:61: error: reached end of file while parsing
    }
    ^
    8 errors

    Tool completed with exit code 1


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    because of the errors
    If there are error messages, please copy the full text and paste it here.
    Otherwise, please explain what the error is.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    I fixed the formatting. Can you help me pls?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    You should compile more frequently when typing in code so that there are not so many errors all at once.

    For the error: cannot find symbol
    that means the compiler can not find a definition for the variable that is mentioned in the rest of that error message.
    You either need to check the spelling or add a definition for the variable.

    if (lineFromServer.equals("***EOF***"))) {
    Count the )s

    reached end of file while parsing
    Check that there is a pairing } for every {

    pw.close;
    Close is a method and needs ()
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Jess17 (February 5th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    The initial file where like this

    import java.net.*;
    import java.io.*;
     
    //client
    class Worker implements Runnable {
     
        DatagramSocket socket = null;
        DatagramPacket packet = null;
     
        public Worker (DatagramSocket socket, DatagramPacket packet) {
            this.socket = socket;
            this.packet = packet;
        }
        public void run() {
     
           try {
           	BufferedReader fileCopyBuffer = new BufferedReader(new InputStreamReader(new FileInputStream ("NormanConquest.txt")));
           String line;
          while ((line = fileCopyBuffer.readLine()) != null)
          {
            DatagramPacket response = new DatagramPacket(line.getBytes(), line.length(),packet.getAddress() , packet.getPort());
           System.out.print(".");
        socket.send(response); } } catch (Exception e){;}; // might consider saving in text file
        }
     
    }
    class Server  {
     
     public  static void main(String Args[]){
        	try {DatagramSocket socket = new DatagramSocket(87);
     
            while (true) {
     
            		byte buffer[] = new byte[66508];
                  	DatagramPacket getstartpacket =
                                new DatagramPacket(buffer, buffer.length);
                    socket.receive(getstartpacket);
                    new Thread( new Worker(socket, getstartpacket)).start();
     
                       }
                } catch (Exception e) {;};
       }
    }
     
    import java.io.*;
    import java.net.*;
     
    class NetworkFileClient1
    {
      public static void main(String args[]) throws IOException
      {
        DatagramSocket socket = new DatagramSocket(2019); //setting up port number to communicate with server
       socket.setSoTimeout(1000);
       boolean received = false;
     
     
          String message = new String("HELO\r\n");
          DatagramPacket packet = new DatagramPacket(
            message.getBytes(), message.length(),
            InetAddress.getByName("127.0.0.1"), 87); //Show how server was found by using getByName method.
            										//Int address Returns the IP address of the machine from which the datagram was received or sent.(of the server)
     
          socket.send(packet);
          System.out.println("Request sent!"); //when client get the server destination details by the getname method it will send a request to server
     
          byte buffer[] = new byte[1090000];
          boolean receiving = true;
     
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("CopyofTextfile.txt"))); // check
     
          while (receiving) { //when request is sent to server the client will display a . while receiving the datagram
          	try {
     
     
          	DatagramPacket getbitoffile =  new DatagramPacket(buffer, buffer.length);
     
            socket.receive(getbitoffile);
     
            buffer = getbitoffile.getData(); //returns the data buffer
     
     
            pw.println(getbitoffile); // Writing lines being received to text file
     
     
            System.out.print(".");
     
          } catch (SocketTimeoutException e) { //if data is not received until timeout exception program will display("")
              receiving = false;
              System.out.println("Timeout. Got fed up waiting...");
          }
          finally
          {
    		  pw.close();
    	  }
        }
      }
    }

    What I need to do is instead of the client waiting for a socket timeout after the last part of the file has been received, the server sends a packet containing an end-of-file marker. If marker is not received then he will resend it again. Can you help me do the code for that pls? Ps thanks a lot for your replies appreciate it

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    Are there any printouts from the program that shows what it is doing now? If not, there should be some there for debug tracing the logic and how data is passed from one to the other.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    Right now all it does the system is:

    Server waits for request from client, while request is received, server starts sending a text file by displaying the ..... and saving the lines being received in a text file until timeout occurs. Now the question is to remove the timeout and replace it by an EOF marker

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    How are you going to do that?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    that s what i dont know :/

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    When and where will the code know it's at the EOF condition?
    What should it do then?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    when last line of text file is send, server will send to client that it's end of file. If not end of file it will keep sending packets

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an EOF and Acknowledgement on client side

    Have you found where to put the code to send the eof message and tried adding it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. File transfer from Client to Server side
    By highlander141 in forum Java Networking
    Replies: 1
    Last Post: August 29th, 2012, 08:53 AM
  2. EOF Exception using InputStream/ReadObject
    By ryumishima44 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 20th, 2012, 06:06 AM
  3. RandomAccessFile.readByte() find EOF
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:14 AM
  4. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM
  5. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM