Solved

multithreaded socket programming in vc++

Posted on 2004-04-29
1
9,739 Views
Last Modified: 2013-11-20
hi,
somebody please post me how to create a mutiple client single server application using threads in vc++.
the problem is iam not able to put accept() function in an infinite loop for the server to accept connections infinitely and create a seperate thread for each accepted client.
thanks in advance.
0
Comment
Question by:Shailaja Kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
kevinnguyen earned 45 total points
ID: 10953346
Assume that:

1) Server's IP address is 192.168.0.2 and waiting for connection on port 5000.
2) _beginthread is used to create threads. Two other alternatives thread routines are AfxBeginThread() and CreateThread().

/** Server.cpp */
#include <winsock2.h>
#include <process.h>
#include <stdio.h>

#define SERVER_IP   "192.168.0.2"
#define BUF_SIZE    1460
#define SERVER_PORT 5000

void receive_from(void * data)
{
      char buf[BUF_SIZE] = {0};
      int  len, sNew = *((int*)data);
      while(1)
      {
            len = recv(sNew, buf, BUF_SIZE - 1, 0);
            if(len > 0) printf("<SERVER> Receive : %s\n", buf);
            Sleep(0);
      }
}

void send_to(void * data)
{
      char buf[BUF_SIZE] = {"Hello from server\n"};
      int  len, sNew = *((int*)data);
      while(1)
      {
            len = send(sNew, buf, BUF_SIZE - 1, 0);
            if(len > 0) printf("\n<SERVER> Send : %s\n", buf);
            Sleep(0);
      }
}

int main(int argc, char *argv[])
{
      WORD    wVersionRequested;
      WSADATA wsaData;
      int     s, err, addrlen;      
      struct sockaddr_in server;      

      wVersionRequested = MAKEWORD(2, 0);
      WSAStartup(wVersionRequested, &wsaData);
      s = socket(AF_INET, SOCK_STREAM, 0);
      if(s == -1)
      {
            WSACleanup();
            return -1;
      }      
      memset(&server, 0, sizeof(server));
      server.sin_family           = AF_INET;
      server.sin_port             = htons(SERVER_PORT);
      server.sin_addr.S_un.S_addr = INADDR_ANY;
      err = bind(s, (struct sockaddr*)&server, sizeof(server));
      if(err == -1)
      {
            closesocket(s);
            WSACleanup();
            return -1;
      }
      err = listen(s, SOMAXCONN);
      if(err == -1)
      {
            closesocket(s);
            WSACleanup();
            return -1;
      }
      while(1)
      {
            int sNew;
            sNew = accept(s, (struct sockaddr*)&server, &addrlen);
            if(sNew < 0) break;
            _beginthread(send_to,      0, (void*)&sNew);
            _beginthread(receive_from, 0, (void*)&sNew);
            Sleep(0);
      };
      closesocket(s);
      WSACleanup();
      return 0;
}

/** Client.cpp */
#include <winsock2.h>
#include <process.h>
#include <stdio.h>

#define SERVER_IP   "192.168.0.2"
#define BUF_SIZE    1460
#define SERVER_PORT 5000

/** Client.cpp */
int main(int argc, char *argv[])
{
      WORD    wVersionRequested;
      WSADATA wsaData;
      int     s, err;
      char    buf[BUF_SIZE];
      struct sockaddr_in server;

      wVersionRequested = MAKEWORD(2, 0);
      WSAStartup(wVersionRequested, &wsaData);
      s = socket(AF_INET, SOCK_STREAM, 0);
      if(s == -1)
      {
            WSACleanup();
            return -1;
      }      
      memset(&server, 0, sizeof(server));
      server.sin_family           = AF_INET;
      server.sin_port             = htons(SERVER_PORT);
      server.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);
      err = connect(s, (struct sockaddr*)&server, sizeof(server));
      while(err > 0)
      {
            strcpy(buf, "<CLIENT> Send : Hello from client\n");
            err = send(s, buf, strlen(buf),  0);
            printf("\n<CLIENT> Send : %s\n", buf);
            memset(buf, 0, BUF_SIZE);
            err = recv(s, buf, BUF_SIZE - 1, 0);
            Sleep(0);
      }
      closesocket(s);
      WSACleanup();
      return 0;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 72
sumHeights  challenge 17 83
noX challenge 17 144
c++  placing data into a form and an editbox 5 48
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question