Thread: Multithreaded Winsock

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    Multithreaded Winsock

    Im working on a program that can send data between a client and a server, kind of like a peer-to-peer chat program.

    I was wondering if anyone can show me a basic layout for a Win32 Multithreaded Winsock program.

    Ive attached the code for my server on this post and my client on the next post, could someone please help point out the errors? The server is extremely laggy, consuming 99% of my CPU (3 GHZ), also the server cannot send data (or the client cannot receive it).

    The client sends fine but doesnt receive data from the server.

    The server code has:

    2 threads (1 for accepting a client, 1 for receiving data from the client)

    The code attached is compilable, so if anyone can try it out and tell me whats wrong? Or post an improved/fixed version of the code.

    Thanx

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    And heres the code for the Client:

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The traditional approach is to have your main thread running your servers UI, a worker thread running your accept() calls and passing the I/O socket off to another thread for processing of the connection.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    Im kinda new to the whole multithreading thing, Ive heard that tutorials just cant describe how powerful it is or something, so Is it possible if u could show me just an extremely basic layout to what you just said?

    Some like:

    Code:
    HANDLE hThread1, hThread2;
    
    int Winmain(...)
    {
    	int ThreadId;
    	hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Acceptthread, NULL, 0, (LPDWORD)&ThreadId);
    
    	int ThreadId2;
    	hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IOthread, NULL, 0, (LPDWORD)&ThreadId2);
    }
    
    void Acceptthread()
    {
    }
    
    void IOthread()
    {
    }
    Im not really sure if the code above is right, but I was also wondering, do you need to declare a MUTEX in order to use a thread?

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    For powerful windows programming solutions including multithreading, I highly recommend Programming Applications for Microsoft Windows by Jeffrey Richter.

    Kuphryn

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    Hmm i would look into that except im poor lol, being 14 and all, so im pretty sure i wont be able to get a hold of any books...yet

  7. #7
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    being 14 and all, so im pretty sure i wont be able to get a hold of any books...yet
    get a job, or better yet, look at your community library or a university library, they are sure to have computer programming books at them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM
  5. Determining Data Size For Network Send/Rec :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 6
    Last Post: 05-07-2002, 09:01 PM