Advertisement
hugol

Untitled

Apr 27th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. /*
  2. ** server.c -- serwer używający gniazd strumieniowych
  3. */
  4.  
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <conio.h>
  8.  
  9. #include <winsock2.h>
  10.  
  11. #define MYPORT 10050    // port, z którym będą się łączyli użytkownicy
  12.  
  13.  
  14.  
  15. HANDLE ghMutex;
  16.  
  17. void gotoxy(int x, int y)
  18. {
  19.     COORD coord;
  20.     coord.X = x;
  21.     coord.Y = y;
  22.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  23. }
  24.  
  25. DWORD WINAPI receiver( LPVOID lpParam )
  26. {
  27.     int mainSocket = (int)lpParam;
  28.     int bytesRecv = SOCKET_ERROR;
  29.     char recvbuf;
  30.  
  31.     int posx = 0;
  32.     int posy = 0;
  33.  
  34.     while (1)
  35.     {
  36.         bytesRecv = recv(mainSocket, &recvbuf, 1, 0);
  37.  
  38.         if (recvbuf == '\n' || recvbuf == '\r')
  39.         {
  40.             posy++;
  41.             posx = 0;
  42.             continue;
  43.         }
  44.         // bckspace
  45.         else  if (recvbuf == 8 && posx>0)
  46.         {
  47.             posx--;
  48.             continue;
  49.         }
  50.         if (posy + 13 == 25)
  51.             posy = 0;
  52.  
  53.         WaitForSingleObject(
  54.             ghMutex,    // handle to mutex
  55.             INFINITE);  // no time-out interval
  56.  
  57.         gotoxy(posx, posy + 13);
  58.         if (recvbuf != '\n')
  59.             printf("%c", recvbuf);
  60.  
  61.         ReleaseMutex(ghMutex);
  62.  
  63.         posx++;
  64.         if (posx == 79)
  65.         {
  66.             posx = 0;
  67.             posy++;
  68.         }
  69.        
  70.     }
  71.  
  72. }
  73.  
  74. void print_separator()
  75. {
  76.     gotoxy(0, 12);
  77.  
  78.     printf("--------------------------------------------------------------------------------");
  79. }
  80.  
  81.  
  82.  
  83. int main(void)
  84. {
  85.     ghMutex = CreateMutex(
  86.         NULL,              // default security attributes
  87.         FALSE,             // initially not owned
  88.         NULL);             // unnamed mutex
  89.  
  90.  
  91.     WSADATA wsaData;
  92.  
  93.     int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  94.     if (result != NO_ERROR)
  95.         printf("Initialization error.\n");
  96.  
  97.     SOCKET mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  98.     if (mainSocket == INVALID_SOCKET)
  99.     {
  100.         printf("Error creating socket: %ld\n", WSAGetLastError());
  101.         WSACleanup();
  102.         return 1;
  103.     }
  104.  
  105.     sockaddr_in service;
  106.     memset(&service, 0, sizeof(service));
  107.     service.sin_family = AF_INET;
  108.     service.sin_addr.s_addr = inet_addr("127.0.0.1");
  109.     service.sin_port = htons(10050);
  110.  
  111.     printf("Server: 1, Klient: 2\n");
  112.     int mode;
  113.     scanf_s("%d", &mode);
  114.  
  115.     if (mode == 1)
  116.     {
  117.  
  118.         if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
  119.         {
  120.             printf("bind() failed.\n");
  121.             closesocket(mainSocket);
  122.             return 1;
  123.         }
  124.  
  125.         if (listen(mainSocket, 1) == SOCKET_ERROR)
  126.             printf("Error listening on socket.\n");
  127.  
  128.         SOCKET acceptSocket = SOCKET_ERROR;
  129.         printf("Waiting for a client to connect...\n");
  130.  
  131.         while (acceptSocket == SOCKET_ERROR)
  132.         {
  133.             acceptSocket = accept(mainSocket, NULL, NULL);
  134.         }
  135.  
  136.  
  137.         system("cls");
  138.         print_separator();
  139.  
  140.         mainSocket = acceptSocket;
  141.        
  142.         DWORD threadId;
  143.         CreateThread(
  144.             NULL,                   // default security attributes
  145.             0,                      // use default stack size  
  146.             receiver,       // thread function name
  147.             (LPVOID)mainSocket,          // argument to thread function
  148.             0,                      // use default creation flags
  149.             &threadId);   // returns the thread identifier
  150.  
  151.     }
  152.     if (mode == 2)
  153.     {
  154.         if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
  155.         {
  156.             printf("Failed to connect.\n");
  157.             WSACleanup();
  158.             return 1;
  159.         }
  160.  
  161.         system("cls");
  162.         print_separator();
  163.  
  164.         DWORD threadId;
  165.         CreateThread(
  166.             NULL,                   // default security attributes
  167.             0,                      // use default stack size  
  168.             receiver,       // thread function name
  169.             (LPVOID)mainSocket,          // argument to thread function
  170.             0,                      // use default creation flags
  171.             &threadId);   // returns the thread identifier
  172.     }
  173.  
  174.     int posx = 0, posy = 0;
  175.  
  176.     while (1)
  177.     {
  178.         char key = _getch();
  179.         send(mainSocket, &key, 1, 0);
  180.         if (key == '\r')
  181.         {
  182.             posy++;
  183.             posx = 0;
  184.             continue;
  185.         }
  186.         // bckspace
  187.         else  if (key == 8 && posx>0)
  188.         {
  189.             posx--;
  190.             key = ' ';
  191.             send(mainSocket, &key, 1, 0);
  192.             gotoxy(posx, posy);
  193.             printf("%c", key);
  194.             ReleaseMutex(ghMutex);
  195.  
  196.             key = 8;
  197.             send(mainSocket, &key, 1, 0);
  198.             continue;
  199.         }
  200.  
  201.         if (posy == 12)
  202.             posy = 0;
  203.  
  204.         WaitForSingleObject(
  205.             ghMutex,    // handle to mutex
  206.             INFINITE);  // no time-out interval
  207.  
  208.         gotoxy(posx, posy);
  209.         if (key != '\r')
  210.             printf("%c", key);
  211.         ReleaseMutex(ghMutex);
  212.  
  213.  
  214.  
  215.         posx++;
  216.         if (posx == 79)
  217.         {
  218.             posx = 0;
  219.             posy++;
  220.         }
  221.     }
  222.  
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement