Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

multithreaded socket programming in vc++

Posted on 2004-04-29
1
Medium Priority
?
9,743 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 135 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

660 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