Solved

multithreaded socket programming in vc++

Posted on 2004-04-29
1
9,733 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to match SAS datasets by multiple key variables and then rename and create new variables? 2 89
MaxSpan challenge 9 97
canBalance challenge 34 87
Path of Workbook 3 79
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ā€¦
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.

832 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