Advertisement
hugol

Untitled

May 4th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 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.  
  19. #define MYPORT 10049    // 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. //HANDLE ghMutex;
  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.         //WaitForSingleObject(
  64.         //  ghMutex,    // handle to mutex
  65.         //  INFINITE);  // no time-out interval
  66.  
  67.         gotoxy(posx, posy + 13);
  68.         if (recvbuf != '\n')
  69.             printf("%c", recvbuf);
  70.  
  71.         //ReleaseMutex(ghMutex);
  72.  
  73.         posx++;
  74.         if (posx == 79)
  75.         {
  76.             posx = 0;
  77.             posy++;
  78.         }
  79.        
  80.     }
  81.  
  82. }
  83.  
  84. void print_separator()
  85. {
  86.     gotoxy(0, 12);
  87.  
  88.     printf("--------------------------------------------------------------------------------");
  89. }
  90.  
  91.  
  92.  
  93. int main(void)
  94. {
  95.     //ghMutex = CreateMutex(
  96.     //  NULL,              // default security attributes
  97.     //  FALSE,             // initially not owned
  98.     //  NULL);             // unnamed mutex
  99.  
  100.  
  101.  
  102.     mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  103.     if (mainSocket == -1)
  104.     {
  105.         printf("Error creating socket");
  106.         return 1;
  107.     }
  108.  
  109.     sockaddr_in service;
  110.     memset(&service, 0, sizeof(service));
  111.     service.sin_family = AF_INET;
  112.     service.sin_addr.s_addr = INADDR_ANY;
  113.     service.sin_port = htons(MYPORT);
  114.  
  115.     printf("Server: 1, Klient: 2\n");
  116.     int mode;
  117.     scanf("%d", &mode);
  118.  
  119.     if (mode == 1)
  120.     {
  121.  
  122.         if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
  123.         {
  124.             printf("bind() failed.\n");
  125.             closesocket(mainSocket);
  126.             return 1;
  127.         }
  128.  
  129.         if (listen(mainSocket, 1) == -1)
  130.             printf("Error listening on socket.\n");
  131.  
  132.         SOCKET acceptSocket = SOCKET_ERROR;
  133.         printf("Waiting for a client to connect...\n");
  134.  
  135.         while (acceptSocket == SOCKET_ERROR)
  136.         {
  137.             acceptSocket = accept(mainSocket, NULL, NULL);
  138.         }
  139.  
  140.  
  141.         system("clear");
  142.         print_separator();
  143.  
  144.         mainSocket = acceptSocket;
  145.        
  146.         pthread_t thread1;
  147.         pthread_create( &thread1, NULL, receiver, NULL);
  148.  
  149.     }
  150.     if (mode == 2)
  151.     {
  152.         if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == -1)
  153.         {
  154.             printf("Failed to connect.\n");
  155.             return 1;
  156.         }
  157.  
  158.         system("clear");
  159.         print_separator();
  160.  
  161.         pthread_t thread1;
  162.         pthread_create( &thread1, NULL, receiver, NULL);
  163.     }
  164.  
  165.     int posx = 0, posy = 0;
  166.  
  167.     while (1)
  168.     {
  169.         char key = mygetch();
  170.         return 0;
  171.         send(mainSocket, &key, 1, 0);
  172.         if (key == '\r')
  173.         {
  174.             posy++;
  175.             posx = 0;
  176.             continue;
  177.         }
  178.         // bckspace
  179.         else  if (key == 8 && posx>0)
  180.         {
  181.             posx--;
  182.             key = ' ';
  183.             send(mainSocket, &key, 1, 0);
  184.             gotoxy(posx, posy);
  185.             printf("%c", key);
  186.             //ReleaseMutex(ghMutex);
  187.  
  188.             key = 8;
  189.             send(mainSocket, &key, 1, 0);
  190.             continue;
  191.         }
  192.  
  193.         if (posy == 12)
  194.             posy = 0;
  195.  
  196.         //WaitForSingleObject(
  197.         //  ghMutex,    // handle to mutex
  198.         //  INFINITE);  // no time-out interval
  199.  
  200.         gotoxy(posx, posy);
  201.         if (key != '\r')
  202.             printf("%c", key);
  203.         //ReleaseMutex(ghMutex);
  204.  
  205.  
  206.  
  207.         posx++;
  208.         if (posx == 79)
  209.         {
  210.             posx = 0;
  211.             posy++;
  212.         }
  213.     }
  214.  
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement