Don't like ads? PRO users don't see any ads ;-)
Guest

kurwa komunikator

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 11.27 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     #define WIN32_LEAN_AND_MEAN
  2.  
  3.     #include <process.h>
  4.     #include <windows.h>
  5.     #include <winsock2.h>
  6.     #include <ws2tcpip.h>
  7.     #include <stdlib.h>
  8.     #include <stdio.h>
  9.         #include "memwatch.h"
  10.      
  11.     // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  12.     #pragma comment (lib, "Ws2_32.lib")
  13.     #pragma comment (lib, "Mswsock.lib")
  14.     #pragma comment (lib, "AdvApi32.lib")
  15.      
  16.      
  17.     #define DEFAULT_BUFLEN 512
  18.     #define DEFAULT_PORT "81"
  19.      
  20.         WSADATA wsaData;
  21.         SOCKET ConnectSocket = INVALID_SOCKET; // PISANIE
  22.                 SOCKET ListenSocket = INVALID_SOCKET; // SŁUCHANIE
  23.         SOCKET ClientSocket = INVALID_SOCKET; // SŁUCHANIE
  24.        
  25.         struct addrinfo *result = NULL;
  26.         struct addrinfo hints;
  27.  
  28.                 struct element
  29.                 {
  30.                         char * dane;
  31.                         struct element * next;
  32.                 };
  33.  
  34.                 typedef struct element * segment;
  35.                
  36.  
  37.         //      typedef struct element * segment;
  38.  
  39.         char *sendbuf = "gnieciona bulka\0";
  40.      
  41.         char s[512], tmp[512];
  42.         char recvbuf[DEFAULT_BUFLEN] = "\0";
  43.      
  44.                 int gadanie = 1;
  45.         int iResult;
  46.         int recvbuflen = DEFAULT_BUFLEN;
  47.        
  48.             int i=0;
  49.                         int blad = 0;
  50.             int wybor = 0;
  51.          
  52.             int pierwszy = 1;
  53.                         int port1, port2;
  54.                         segment glowa = NULL;
  55.                         HANDLE  hRunMutex;                     /* "Keep Running" mutex */
  56.                         int     ThreadNr;                      /* Number of threads started */
  57.  
  58.         void KbdFunc( void  );                 /* Keyboard input, thread dispatch */
  59.         void ShutDown( void );                 /* Program shutdown */
  60.         int pisanie ();
  61.         int sluchanie ( void *pMyID );
  62.         void watek_sluchania( void );
  63.         void read (segment ostatni);
  64.         segment create (segment ostatni, char * slowo);
  65.         segment delete_all (segment ostatni);
  66.         void update (segment ostatni,int pozycja, char * slowo);
  67.  
  68.     int __cdecl main(int argc, char **argv)
  69.     {
  70.  
  71.  
  72.      
  73.         // inicjuje Winsock'a
  74.         iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  75.         if (iResult != 0)
  76.                 {
  77.             printf("WSAStartup failed with error: %d\n", iResult);
  78.             return 1;
  79.         }
  80.      
  81.         ZeroMemory( &hints, sizeof(hints) );
  82.         hints.ai_family = AF_INET;
  83.         hints.ai_socktype = SOCK_STREAM;
  84.         hints.ai_protocol = IPPROTO_TCP;
  85.  
  86.                 printf("Twoj wybor:");
  87.                 scanf("%d", &wybor);
  88.  
  89.                 if (wybor==1)
  90.                 {
  91.                         port1 = "83";
  92.                         port2 = "81";
  93.                        
  94.                 }
  95.                 else if (wybor == 2)
  96.                 {
  97.                         port1 = "81";
  98.                         port2 = "83";
  99.                        
  100.                 }
  101.                 watek_sluchania();
  102.                 Sleep(550);
  103.                
  104.                 printf("Gotow?");
  105.                 getchar();
  106.        
  107.                    
  108.                 while(gadanie)
  109.         {
  110.                     if(pierwszy == 1)
  111.                                         {
  112.                         //    system("CLS");
  113.                             //printf("PISANIE");
  114.                            
  115.                     }
  116.                                        
  117.                     pierwszy = 0;
  118.                    
  119.                                         powtorka:
  120.  
  121.                     iResult = getaddrinfo( "localhost", port1, &hints, &result ); // bez parametrow
  122.                     if ( iResult != 0 )
  123.                                         {
  124.                       //      printf("getaddrinfo failed with error: %d\n", iResult);
  125.                                                 //      system("PAUSE");
  126.      //                       WSACleanup();
  127.                            // return 1;
  128.                                                 goto powtorka;
  129.                     }
  130.                     // Attempt to connect to an address until one succeeds
  131.            
  132.      
  133.             // Create a SOCKET for connecting to server
  134.             ConnectSocket = socket(result->ai_family, result->ai_socktype,
  135.                 result->ai_protocol);
  136.             if (ConnectSocket == INVALID_SOCKET) {
  137.         //        printf("socket failed with error: %ld\n", WSAGetLastError());
  138.                 //              system("PAUSE");
  139.                                 goto powtorka;
  140.          //       WSACleanup();
  141.               //  return 1;
  142.             }
  143.                    
  144.             // Connect to server.
  145.             do {
  146.             iResult = connect( ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
  147.             if (iResult == SOCKET_ERROR) {
  148.                             //WSACleanup();
  149.             //                printf("socket error");
  150.                                                         goto powtorka;
  151.                             //system("PAUSE");
  152.                             //return 1;
  153.                     }
  154.                     }
  155.                     while(iResult < 0);
  156.                    
  157.      
  158.                     freeaddrinfo(result);
  159.      
  160.                     if (ConnectSocket == INVALID_SOCKET) {
  161.              //               printf("Unable to connect to server!\n");
  162.                         //                              system("PAUSE");
  163.                                                         goto powtorka;
  164.            //                 WSACleanup();
  165.                       //      return 1;
  166.                     }
  167.                     do {                        // wysylanie informacji  
  168.                                                 if (blad == 0)
  169.                                                 {
  170.                             printf("\nJa: ");
  171.                             fgets( s, 512, stdin );
  172.                             s[strlen(s)-1] = '\0';
  173.                                                         if (strncmp(s,"koniec",4)==0) gadanie = 0;
  174.                                                         glowa = create(glowa,s);
  175.                                                        
  176.                             //scanf("%s", s);
  177.                             sendbuf = s; //wskaznik do zmiennej s do ktorej dopisuje informacje
  178.                                                 }
  179.                             iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
  180.                             if (iResult == SOCKET_ERROR) {
  181.              //                       printf("send failed with error: %d\n", WSAGetLastError());
  182.                                     closesocket(ConnectSocket);
  183.                                                                         iResult = shutdown(ConnectSocket, SD_SEND);
  184.                         //                                              system("PAUSE");
  185.                                                                         blad = 1;
  186.                                                                         goto powtorka;
  187.           //                          WSACleanup();
  188.                            //         return 1;
  189.                             }
  190.                                                         blad = 0;
  191.                             printf("\n");
  192.                             iResult = 0;
  193.                             } while( iResult > 0 );
  194.  
  195.  
  196.             iResult = shutdown(ConnectSocket, SD_SEND);
  197.             closesocket(ConnectSocket);
  198.             pierwszy = 0;
  199.             continue;
  200.             }
  201.      
  202.            printf("stop");    
  203.                    read(glowa);
  204.                    glowa = create(glowa,"mis");
  205.                    read(glowa);
  206.         system("PAUSE");
  207.                 ShutDown();
  208.         return 0;
  209.     }
  210.  
  211.  
  212. int sluchanie ( void *pMyID )
  213. {
  214.         while(1)
  215.         {
  216.                 if (pierwszy == 1)
  217.                 {
  218.                             system("CLS");
  219.                             //printf("SLUCHANIE\n\n");
  220.                             printf("Konwersacja z:  %s\n\n", "localhost" );
  221.                     }
  222.                     pierwszy = 0;
  223.                    
  224.             /* SŁUCHANIE ----------------------------------------------------------------*/
  225.                     // Resolve the server address and port
  226.         iResult = getaddrinfo("localhost", port2, &hints, &result);
  227.         if ( iResult != 0 ) {
  228.             printf("getaddrinfo failed with error: %d\n", iResult);
  229.                     system("PAUSE");
  230.     //        WSACleanup();
  231.          //   return 1;
  232.         }
  233.      
  234.         // Create a SOCKET for connecting to server
  235.         ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  236.         if (ListenSocket == INVALID_SOCKET) {
  237.             printf("socket failed with error: %ld\n", WSAGetLastError());
  238.                     system("PAUSE");
  239.             freeaddrinfo(result);
  240.       //      WSACleanup();
  241.          //   return 1;
  242.         }
  243.      
  244.         // Setup the TCP listening socket
  245.         iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  246.         if (iResult == SOCKET_ERROR) {
  247.             printf("bind failed with error: %d\n", WSAGetLastError());
  248.                     system("PAUSE");
  249.             freeaddrinfo(result);
  250.             closesocket(ListenSocket);
  251.        //     WSACleanup();
  252.           //  return 1;
  253.         }
  254.      
  255.         //freeaddrinfo(result);
  256.      
  257.         iResult = listen(ListenSocket, 1);
  258.         if (iResult == SOCKET_ERROR) {
  259.             printf("listen failed with error: %d\n", WSAGetLastError());
  260.                     system("PAUSE");
  261.             closesocket(ListenSocket);
  262.     //        WSACleanup();
  263.      //       return 1;
  264.         }
  265.            
  266.      
  267.                    
  268.                     // Accept a client socket
  269.                     ClientSocket = accept(ListenSocket, NULL, NULL);
  270.   //   printf("wysluchane");
  271.                     if (ClientSocket == INVALID_SOCKET) {
  272.                             printf("accept failed with error: %d\n", WSAGetLastError());
  273.                             system("PAUSE");
  274.                             closesocket(ListenSocket);
  275.             //                WSACleanup();
  276.            //                 return 1;
  277.                     }
  278.      
  279.                     // odbieranie póki klient nie zrezygnuje z wysylania
  280.                     do {
  281.                             for(i=0; i<recvbuflen; i++)
  282.                             {
  283.                                     recvbuf[i] ='\0';
  284.                             }
  285.                             iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  286.                             if (iResult > 0) {
  287.                                                                 printf("\n%s: ", "localhost");
  288.                                     printf(" %s\n", recvbuf );
  289.                                                                         update (glowa,1,recvbuf);
  290.                                                                         read(glowa);
  291.                                                                        
  292.                                                                        
  293.                                    
  294.                             }
  295.                             else if (iResult == 0)
  296.                                     iResult=iResult;
  297.                             else  {
  298.                                     printf("recv failed with error: %d\n", WSAGetLastError());
  299.                                     system("PAUSE");
  300.                             }
  301.                     } while (iResult > 0);
  302.  
  303.                     closesocket(ListenSocket);
  304.                     closesocket(ClientSocket);
  305.  
  306.             continue;
  307.             }
  308.            
  309.      
  310.    
  311. }
  312.  
  313. void ShutDown( void )                  /* Shut down threads */
  314. {
  315.     while ( ThreadNr > 0 )
  316.     {
  317.             /* Tell thread to die and record its death. */
  318.             ReleaseMutex( hRunMutex );
  319.             ThreadNr--;  
  320.     }
  321.     /* Clean up display when done */
  322.   //  WaitForSingleObject( hScreenMutex, INFINITE );
  323.   //  ClearScreen();
  324. }
  325.  
  326. void watek_sluchania( void )                   /* Dispatch and count threads. */
  327. {
  328.  
  329.             ThreadNr++;
  330.          _beginthread( sluchanie, 0, &ThreadNr );
  331.  
  332.     ShutDown();
  333. }
  334.  
  335. segment create (segment ostatni, char * slowo)
  336. {
  337.        
  338.     segment nowy = malloc( sizeof( segment) ); //zarezerwowanie pamięci
  339.     nowy->dane = slowo;                                                 // zapisanie danych
  340.     nowy->next = ostatni;                                               //podpięcie pod stara glowe
  341.         read(nowy);                                                                     //zwrocenie jako nowa glowe
  342.     return nowy;
  343.        
  344. }
  345.  
  346. void read (segment ostatni)
  347. {
  348.     int i = 0;
  349.     segment poprzedni = ostatni;                               
  350.     while (poprzedni != NULL)
  351.     {
  352.         i++;
  353.         printf("\n%d . %s",i,poprzedni->dane);
  354.         poprzedni = poprzedni->next;
  355.     }
  356. }
  357.  
  358. segment delete_all (segment ostatni)
  359. {
  360.     segment poprzedni;
  361.     while (ostatni != NULL)
  362.     {
  363.         poprzedni = ostatni->next;
  364.         free (ostatni);
  365.         ostatni = poprzedni;
  366.     }
  367.     return (ostatni);
  368. }
  369.  
  370. void update (segment ostatni,int pozycja, char * slowo)
  371. {
  372.     int i;
  373.     segment poprzedni = ostatni;
  374.     for (i=1;i<pozycja;i++)
  375.     {
  376.         poprzedni= poprzedni->next;
  377.     }
  378.     poprzedni->dane = slowo;
  379. }