Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

multithreaded socket programming in vc++

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

782 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