• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads and Sockets

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This new problem spawned from my previous topic (https://coderanch.com/t/409993/java/java/Serialization-IOException-writeObject) about a Serialization problem with my networking project.

To summarize: My networking project is composed of two programs, a Server and a Client. Client requests a file to Server. If the file is available, Server sends packets (represented by an instance of a class I made called Frame) containing fragments of the file to Client and Client responds with an acknowledgment (ACK) for each packet. If the Server does not receive an ACK for a packet, that packet is retransmitted. The project should also be flexible enough to implement a sliding-window protocol (hence the use of threads).

Here is my implementation: I created a class called SendFrame which extends Thread. This class is responsible for transmitting and/or retransmitting (if ACK is not received) a Frame object (packet). The code inside the run() method is quite simple. The main idea is to keep retransmitting the packet until ACK is received. Pseudocode inside run() looks like this:




The server pseudocode looks like this:



simple pseudocode for Client:



My question: When Client receives a packet, is there a way for Client to awaken the sleeping Server so that it can receive the ACK? Also, I think my implementation feels awkward. Any tips or suggestions on making it better?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John: When Client receives a packet, is there a way for Client to awaken the sleeping Server so that it can receive the ACK?


Yes, instead of sleeping, do a timed wait on an object. When an ack is received (I assume there will be a different thread that receives ack by listening on a socket), just notify the object that the server is waiting on.

John: Any tips or suggestions on making it better?


  • Firstly, use a threadpool instead of creating a new thread for sending each file fragment.
  • Dont extend thread if you are not overriding anything but run()

  • What if the client dies after receiving a file fragment? Your server will run till infinity, rite?
  • I dont think its a good idea for the server to keep on trying till an ack is received. Server should respond with the next file fragment if it receives an ack. There must be a timeout within which the client should ack. If it failed to do so, the connection must be closed.


  • [ April 11, 2008: Message edited by: Nitesh Kant ]
     
    John Cebedo
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Yes, instead of sleeping, do a timed wait on an object. When an ack is received (I assume there will be a different thread that receives ack by listening on a socket), just notify the object that the server is waiting on.



    I googled some tutorials concerning wait(), notify(), and notifyall() but I am still not sure how to use these with my programs. The tutorials seem to be more interested in synchronizing methods or blocks of code so that only one thread can manipulate that method or block of code. What do you mean by "timed wait on an object"?

    I created a thread whose sole responsibility is to wait for ACK and wake up the sleeping server when that ACK arrives. But I am still ignorant on how to awaken the sleeping server. I went over the Thread API but there aren't any methods that I can use like threadName.awaken() or Thread.awaken(threadName) something similar.
     
    Nitesh Kant
    Bartender
    Posts: 1638
    IntelliJ IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    John:
    What do you mean by "timed wait on an object"?


    I meant to call wait(long) method.
    This method will force the current thread to wait till there is a notify on the monitor or the timeout value has passed. So, your server will wait for a maximum of n milliseconds or an ack from the client.
    In contrast to a sleep, a wait can be terminated by a notify.
    A sleeping thread can only wake up when the sleeping period is over or it is interrupted.(ignoring the spurious wakeups.)
    On the other hand a waiting thread can be woken by a notify (in your case, the thread listening for the ACK will notify the server)

    John:
    I created a thread whose sole responsibility is to wait for ACK and wake up the sleeping server when that ACK arrives. But I am still ignorant on how to awaken the sleeping server. I went over the Thread API but there aren't any methods that I can use like threadName.awaken() or Thread.awaken(threadName) something similar.


    No there is no such method as awaken(). As explained above, you should use a wait/notify mechanism.

    This article wil give you some insight into wait/notify.

    P.S.: If you are on jdk 5 or above, you may be interested in using Conditions instead of wait/notify.
     
    What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic