Thread: Implement TCP server in custom event

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    77

    Implement TCP server in custom event

    I need to add a TCP server to an existing code. The TCP server is started as a thread and it should raise an event to the parent as soon as a message is received in the socket from a client. The parent should stop the TCP thread, treat the message and restart the TCP server with a back message to be sent via the socket to the client.
    There are several issues here; I know how to create the TCP server and to create a thread. I would like to have some guidance on how to put all together so that to implement a custom event when the TCP input buffer is full.
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think the first issue is your massively over complicated design.

    How is your 'main' thread waiting for messages to begin with?

    Why for example can't the actual message handling bit of the code be run in the TCP thread context?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    You are right regarding the architecture complexity. But there are several reasons for taking out message processing from the TCP server (one is multi-user).
    Now, to structure my need:
    - I have a main that creates and starts a thread and an event
    - The thread runs a TCP-server-function in a listen loop. It doesn't block the main thread.
    - The event is fired when the TCP-server-function gets an input message in the socket buffer.
    - The event executes a "callback" function in the main thread.
    I ahev worked in C++, Java, C# with such built-in patterns but I haven't tried in C so far.
    I am in a Windows environment.
    If you have some link to a simple working example I'd appreciate.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're worried about the sockets blocking your main thread, then perhaps
    select function (Windows)

    You setup your select fd_sets, you can call select() with a timeout in case none of your sockets are active within a time frame, and you don't have any messy IPC issues with having sockets on separate threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    In fact this is my top description case:
    I have a main program in C that does machine learning in an infinite loop, just printing out from time to time, at the console, about different processing phases ans steps.
    This program processes data from a huge volume of files and gets, when needed, on-going parameters entered from console. I interrupt the infinite loop the get these parameters by using the Windows _kbhit() function. Hitting the keyboard stops the infinite loop and I can implement whatever I want through a function that eventually relaunch the infinite loop. It works as an event(hitting the key) + callback (the function).
    Now, the main program must evolve to have parameters provided also from an external application, that can communicate via TCP sockets. Therefore, I want to replicate the "_kbhit() + function" model, as described, into a TCP server + call back. I can put the TCP server into a thread, but as soon as it gets data from the external application it has to raise an event that stops the main program infinite loop. So this is my problem. In fact all is summarized to implement concurrent asynchrounous processes in C. More precisely, to create an event triggered in the TCP server thread, then to execute the function that will stop the infinite loop etc. as described.
    My concern now is how to create the event and fire it when the TCP server gets data from socket. I don't think WaitForSingleObject is appropriate (puts the parent process on wait, so the infinite loop is stopped), or I don't know how to use it.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Imagine the select () call as being equivalent to kbhit ().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    Thanks Salem for your assistance.
    I have solved the problem, which, in fact, was about event + callback. I just used signal(SIGINT, callback) in main, and raise(SIGINT) in the thread.
    Now about select(), it's true that is also handy but as I had to: listen-to-socket/get-data/process-data/send-back-data-to-socket I've kept the TCP server for full communication protocol with the external application (a Node JS one).
    Veeeery easy in fact, just had to know :-)!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to implement ftp server C
    By polslinux in forum C Programming
    Replies: 2
    Last Post: 06-29-2012, 09:21 AM
  2. Replies: 2
    Last Post: 06-06-2011, 08:23 AM
  3. How to implement custom window message
    By naruto in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2004, 08:19 PM
  4. Custom mail server-like application?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 11-11-2003, 10:30 AM
  5. Custom Server
    By Thantos in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-28-2003, 09:58 PM

Tags for this Thread