Thread: socket problem

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    1
    Rep Power
    0

    Unhappy socket problem


    I'm lost while I'm writing a socket network program. My program is working for a single client. But I have no idea how to change my program to works with more than one client. Can anyone give me solution? thanks a lot.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i am new to this myself, but perhaps i can help. first off though:

    is it tcp/ip or udp? if it is UDP, u can get away with a singlethreaded server and a bunch of clients since they dont actually establish a connection. UDP just sends packets back and forth. if it is tcp/ip, i believe u need a multi threaded server to handle more than one client, since a constant connection is being established.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,179
    Rep Power
    2222
    I'm assuming that you're writing a TCP server. Multithreading is one option and about your only option if you're in Windows. If you're doing in Linux/UNIX and are not comfortable with pthreads, you could consider forking a new process.

    Either way, the main process/thread is doing the listening. When it accepts a connect, then it can create a new thread or process that will handle the client connected to it and finally return to listening for the next connect to come in.

    There's an example of a multithreaded TCP server at the site for "The Pocket Guide to TCP/IP Sockets: C Version" by Michael J. Donahoo and Kenneth L. Calvert at http://cs.baylor.edu/~donahoo/PocketSocket/ . Of course, they're using pthreads, but the concepts are pretty much the same under Windows. Actually, they also have Winsock versions of their examples.

IMN logo majestic logo threadwatch logo seochat tools logo