Advertisement
hugol

Untitled

May 10th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. /*
  2. ** server.c -- serwer używający gniazd strumieniowych
  3. */
  4.  
  5. #include <cstdio>
  6. #include <cstdlib>
  7.  
  8. #include <sys/socket.h>
  9.  
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <pthread.h>
  13. #include <arpa/inet.h>
  14. #include <string.h>
  15. #include <sys/types.h>  
  16. #include <unistd.h>
  17. #include <termios.h>
  18. #include <iostream>
  19. #define MYPORT 10053   // port, z którym będą się łączyli użytkownicy
  20.  
  21. #define SOCKADDR sockaddr
  22. #define SOCKET_ERROR -1
  23. #define SOCKET int
  24. #define closesocket close
  25.  
  26. pthread_mutex_t lock;
  27.  
  28. void gotoxy(int x,int y)
  29. {
  30.     printf("%c[%d;%df",0x1B,y,x);
  31. }
  32.  
  33.  
  34. int mainSocket;
  35.  
  36. void *receiver( void *ptr )
  37. {
  38.     int bytesRecv = -1;
  39.     char recvbuf;
  40.  
  41.     int posx = 0;
  42.     int posy = 0;
  43.  
  44.     while (1)
  45.     {
  46.         bytesRecv = recv(mainSocket, &recvbuf, 1, 0);
  47.  
  48.         if (recvbuf == '\n' || recvbuf == '\r')
  49.         {
  50.             posy++;
  51.             posx = 0;
  52.             continue;
  53.         }
  54.         // bckspace
  55.         else  if (recvbuf == 8 && posx>0)
  56.         {
  57.             posx--;
  58.             continue;
  59.         }
  60.         if (posy + 13 == 25)
  61.             posy = 0;
  62.  
  63.          pthread_mutex_lock(&lock);
  64.  
  65.         gotoxy(posx, posy + 13);
  66.         if (recvbuf != '\n')
  67.         {
  68.             printf("%c", recvbuf);
  69.             fflush(stdout);
  70.         }
  71.  
  72.         pthread_mutex_unlock(&lock);
  73.  
  74.         posx++;
  75.         if (posx == 79)
  76.         {
  77.             posx = 0;
  78.             posy++;
  79.         }
  80. pthread_mutex_lock(&lock);
  81.         gotoxy(79,24);
  82. pthread_mutex_unlock(&lock);
  83.     }
  84.  
  85. }
  86.  
  87. void print_separator()
  88. {
  89.     gotoxy(0, 12);
  90.  
  91.     printf("--------------------------------------------------------------------------------");
  92. }
  93.  
  94.  
  95.  
  96. int main(void)
  97. {
  98.     //ghMutex = CreateMutex(
  99.     //  NULL,              // default security attributes
  100.     //  FALSE,             // initially not owned
  101.     //  NULL);             // unnamed mutex
  102.  
  103.     system("/bin/stty raw");
  104.  
  105.     mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  106.     if (mainSocket == -1)
  107.     {
  108.         printf("Error creating socket");
  109.         return 1;
  110.     }
  111.  
  112.     sockaddr_in service;
  113.     memset(&service, 0, sizeof(service));
  114.     service.sin_family = AF_INET;
  115.     service.sin_addr.s_addr = INADDR_ANY;
  116.     service.sin_port = htons(MYPORT);
  117.  
  118.     printf("Server: 1, Klient: 2\n");
  119.     int mode;
  120.     scanf("%d", &mode);
  121.  
  122.     if (mode == 1)
  123.     {
  124.  
  125.         if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
  126.         {
  127.             printf("bind() failed.\n");
  128.             closesocket(mainSocket);
  129.             return 1;
  130.         }
  131.  
  132.         if (listen(mainSocket, 1) == -1)
  133.             printf("Error listening on socket.\n");
  134.  
  135.         SOCKET acceptSocket = SOCKET_ERROR;
  136.         printf("Waiting for a client to connect...\n");
  137.  
  138.         while (acceptSocket == SOCKET_ERROR)
  139.         {
  140.             acceptSocket = accept(mainSocket, NULL, NULL);
  141.         }
  142.  
  143.  
  144.         system("clear");
  145.         print_separator();
  146.  
  147.         mainSocket = acceptSocket;
  148.        
  149.         pthread_t thread1;
  150.         pthread_create( &thread1, NULL, receiver, NULL);
  151.  
  152.     }
  153.     if (mode == 2)
  154.     {
  155.         if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
  156.         {
  157.             printf("Failed to connect.\n");
  158.             return 1;
  159.         }
  160.  
  161.         system("clear");
  162.         print_separator();
  163.  
  164.         pthread_t thread1;
  165.         pthread_create( &thread1, NULL, receiver, NULL);
  166.     }
  167.  
  168.     int posx = 0, posy = 0;
  169.  
  170.     while (1)
  171.     {
  172. pthread_mutex_lock(&lock);
  173.         gotoxy(79,24);
  174. pthread_mutex_unlock(&lock);
  175.         char key;
  176.         std::cin.get(key);
  177.         send(mainSocket, &key, 1, 0);
  178.         if (key == '\r')
  179.         {
  180.             posy++;
  181.             posx = 0;
  182.             continue;
  183.         }
  184.         // bckspace
  185.         else  if (key == 8 && posx>0)
  186.         {
  187.             posx--;
  188.             key = ' ';
  189.             send(mainSocket, &key, 1, 0);
  190.             pthread_mutex_lock(&lock);
  191.             gotoxy(posx, posy);
  192.             printf("%c", key);
  193.            
  194.             pthread_mutex_unlock(&lock);
  195.  
  196.             key = 8;
  197.             send(mainSocket, &key, 1, 0);
  198.             continue;
  199.         }
  200.  
  201.         if (posy == 12)
  202.             posy = 0;
  203.  
  204.          pthread_mutex_lock(&lock);
  205.  
  206.         gotoxy(posx, posy);
  207.         if (key != '\n')
  208.         {
  209.             printf("%c", key);
  210.         }
  211.         fflush(stdout);
  212.         pthread_mutex_unlock(&lock);
  213.  
  214.  
  215.  
  216.         posx++;
  217.         if (posx == 79)
  218.         {
  219.             posx = 0;
  220.             posy++;
  221.         }
  222.        
  223.     }
  224.  
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement