Programy Sieciowy- przystępna teoria C++ // Tworzenie procesów potomnych dla Klienta (podłączanie wielu klientów do jednego serwera) // Telepraca // Strona białego domu zrobiona na drupalu // Opis bibliotek w C++ // Biblioteki programistyczne do pobrania



Przebieg połączenia TCP serwer-klient

Kod serwera i klienta:
#include <iostream.h>
#include <winsock2.h>

const unsigned int maxchars = 200; //maximum number of chars for the buffer (i/o)

//The client
void Client()
{
   char dest_ip[15]; //destination IP
   cout << "Destination IP: ";
   cin >> dest_ip;
   cout << endl;

   SOCKET go; //create the sending socket structure


   if(go = socket(AF_INET,SOCK_STREAM,0))
      cout << "--- Sending socket created successfully!\n";
   else
   {
      cout << "Can't create sending socket! Aborting!\n";
      WSACleanup();
      exit(0);
   }

   sockaddr_in target;

   target.sin_family = AF_INET;
   target.sin_port = htons(7979); //sending port
   target.sin_addr.s_addr = inet_addr(dest_ip);


   if(connect(go,(LPSOCKADDR)&target,sizeof(target)) == SOCKET_ERROR)
   {
      cout << "Can't connect! Aborting!\n";
      WSACleanup();
      exit(0);
   }
   else
   {
      cout << "--- Connected successfully!\n\n";
      cout << "-| Enter 000 when you want to end the session!\n\n";
         

      char buff[maxchars];

      while(buff[0] != '0' || buff[1] != '0' || buff[2] != '0')
      {
         cin.getline(buff,maxchars);      
         cout << "Enter data: ";
         
         

         if(buff[0] != '0' || buff[1] != '0' || buff[2] != '0')
            send(go,buff,sizeof(buff),0);
         else
         {
            send(go,buff,sizeof(buff),0);
            closesocket(go);
            cout << "-> Session closed!\n";
            WSACleanup();
            exit(0);
         }
      }


   }
}

void Server()
{
   SOCKET come;

   if(come = socket(AF_INET,SOCK_STREAM,0))
      cout << "--- Receiving socket created successfully!\n";
   else
   {
      cout << "Can't create receiving socket! Aborting!\n";
      WSACleanup();
      exit(0);
   }

   sockaddr_in addr; //the address structure for a TCP socket

   addr.sin_family = AF_INET; //Address family internet
   addr.sin_port = htons(7979); //Receiving port
   addr.sin_addr.s_addr = htonl(INADDR_ANY);

   if(bind(come,(LPSOCKADDR)&addr,sizeof(addr)) == SOCKET_ERROR)
   { //error
      cout << "Error binding! Aborting!\n";
      WSACleanup(); //unload WinSock
      exit(0); //quit
   }

   if(listen(come,1) == SOCKET_ERROR)
   { //error. unable to listen
      cout << "Error listening! Aborting!\n";
      closesocket(come);
      WSACleanup();
      exit(0);
   }
   else
   {
      SOCKET client; //socket handles to clients

      client = accept(come,NULL,NULL);
      
      if(client == INVALID_SOCKET)
      { //error accepting the connection
         cout << "Can't accept connection! Aborting!\n";
         closesocket(come);
         WSACleanup();
         exit(0);
      }
      else
      {
         cout << endl;
         char buffer[maxchars]; //buffer for incomming messages

         while(buffer[0] != '0' || buffer[1] != '0' || buffer[2] != '0')
         {
            buffer[0] = 0;
            buffer[1] = 0;
            buffer[2] = 0;

            recv(client,buffer,sizeof(buffer),0);

            if(buffer[0] != '0' || buffer[1] != '0' || buffer[2] != '0')
            {
               if(strlen(buffer) > 0)
                  cout << "Received: " << buffer << endl;
            }
            else
            {
               closesocket(come);
               cout << "-> Session closed!\n" << endl;
               WSACleanup();
               exit(0);
            }
            

         }


      }
   }

}

void main()
{
   
   WSADATA w;

   if(WSAStartup(0x0202,&w))
   {
      cout << "Can't initialize WinSock. Aborting!\n";
      exit(0);
   }
   else
      cout << "--- WinSock initialized successfully!\n";

   if(w.wVersion != 0x0202)
   { //wrong version
      cout << "Wrong version.Aborting!\n";
      WSACleanup(); //Unload ws2_32.dll
      exit(0);
   }
   else
      cout << "--- Version 2.2 accepted!\n";


   char o;
   cout << endl;
   while(o != '1' && o!= '2' && o != '3')
   {
      cout << "1 - Run Server\n";
      cout << "2 - Run Client\n";
      cout << "3 - Exit\n";
      cout << "Enter 1,2 or 3: ";
      cin >> o;
   }

   switch(o)
   {
   case '1':
      Server();
      break;
   case '2':
      Client();
      break;
   case '3':
      exit(0);
      break;
   };
}
//

Komentarze

Popularne posty