Solved

multithreaded socket programming in vc++

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 101
if loop in java 3 114
pre4 challenge 19 88
firstChar challenge 13 86
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now