Advertisement
themoebius

socket_server.cpp

Apr 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <string.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <netdb.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <strings.h>
  11. #include <stdlib.h>
  12. #include <string>
  13. #include <pthread.h>
  14. using namespace std;
  15.  
  16. void *task1(void *);
  17.  
  18. static int connFd;
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22.     int pId, portNo, listenFd;
  23.     // socklen_t len; //store size of the address
  24.     bool loop = false;
  25.     struct sockaddr_in svrAdd, clntAdd;
  26.  
  27.     pthread_t threadA[3];
  28.  
  29.     if (argc < 2)
  30.     {
  31.         cerr << "Syntax: ./server <port>" << endl;
  32.         return 0;
  33.     }
  34.  
  35.     portNo = atoi(argv[1]);
  36.  
  37.     if((portNo > 65535) || (portNo < 2000))
  38.     {
  39.         cerr << "Please enter a port number between 2000 - 65535" << endl;
  40.         return 0;
  41.     }
  42.  
  43.     //create socket
  44.     listenFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  45.  
  46.     if(listenFd < 0)
  47.     {
  48.         cerr << "Cannot open socket" << endl;
  49.         return 0;
  50.     }
  51.  
  52.     bzero((char*) &svrAdd, sizeof(svrAdd));
  53.  
  54.     svrAdd.sin_family = AF_INET;
  55.     svrAdd.sin_addr.s_addr = INADDR_ANY;
  56.     svrAdd.sin_port = htons(portNo);
  57.  
  58.     //bind socket
  59.     if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
  60.     {
  61.         cerr << "Cannot bind" << endl;
  62.         return 0;
  63.     }
  64.  
  65.     listen(listenFd, 5);
  66.  
  67.     int noThread = 0;
  68.  
  69.     while (noThread < 3)
  70.     {
  71.         socklen_t len = sizeof(clntAdd);
  72.         cout << "Listening" << endl;
  73.  
  74.         //this is where client connects. svr will hang in this mode until client conn
  75.         connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
  76.  
  77.         if (connFd < 0)
  78.         {
  79.             cerr << "Cannot accept connection" << endl;
  80.             return 0;
  81.         }
  82.         else
  83.         {
  84.             cout << "Connection successful" << endl;
  85.         }
  86.  
  87.         pthread_create(&threadA[noThread], NULL, task1, NULL);
  88.  
  89.         noThread++;
  90.     }
  91.  
  92.     for(int i = 0; i < 3; i++)
  93.     {
  94.         pthread_join(threadA[i], NULL);
  95.     }
  96. }
  97.  
  98. void *task1 (void *dummyPt)
  99. {
  100.     cout << "Thread No: " << pthread_self() << endl;
  101.     char test[301];
  102.     bzero(test, 301);
  103.     bool loop = true;
  104.     int len_data = 0;
  105.     while(loop)
  106.     {
  107.         bzero(test, 301);
  108.  
  109.  
  110.         len_data = read(connFd, test, 300);
  111.  
  112.         string tester (test);
  113.         cout << tester << endl;
  114.  
  115.  
  116.         if(len_data == 0)
  117.             break;
  118.     }
  119.     cout << "\nClosing thread " << pthread_self() << " and conn" << endl;
  120.     close(connFd);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement