Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <arpa/inet.h>
  9. #include <pthread.h>
  10.  
  11. int main() {
  12.  
  13.  
  14. int choice;
  15. printf("Welcome to slack! \n \n");
  16. //printf("Please write your nickname: "); scanf("%s",nick); system("CLS");
  17. printf("Type 1 for CHAT, 2 for FILE MANAGER: \n"); scanf("%d",&choice);
  18. while(choice != 1 && choice != 2)
  19. {
  20. printf("Somthing went wrong, please try again: "); scanf("%d",&choice);
  21. }
  22. if(choice == 1)
  23. {
  24. struct sockaddr_in server_addr;
  25.  
  26. client = socket(AF_INET, SOCK_STREAM, 0);
  27.  
  28. /* ---------- ESTABLISHING SOCKET CONNECTION ----------*/
  29. /* --------------- socket() function ------------------*/
  30.  
  31. if (client < 0)
  32. {
  33. cout << "\nError establishing socket..." << endl;
  34. exit(1);
  35. }
  36.  
  37. cout << "\n=> Socket client has been created..." << endl;
  38.  
  39. server_addr.sin_family = AF_INET;
  40. server_addr.sin_port = htons(portNum);
  41.  
  42. if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
  43. cout << "=> Connection to the server port number: " << portNum << endl;
  44.  
  45. cout << "=> Awaiting confirmation from the server..." << endl;
  46. recv(client, buffer, bufsize, 0);
  47. cout << "=> Connection confirmed, you are good to go...";
  48.  
  49. cout << "\n\n=> Enter # to end the connection\n" << endl;
  50.  
  51. do {
  52. cout << "tu powinien byc klient: " << client;
  53. do {
  54. cin >> buffer;
  55. send(client, buffer, bufsize, 0);
  56. if (*buffer == '#') {
  57. send(client, buffer, bufsize, 0);
  58. *buffer = '*';
  59. isExit = true;
  60. }
  61. } while (*buffer != 42);
  62.  
  63. cout << "Server: ";
  64. do {
  65. recv(client, buffer, bufsize, 0);
  66. cout << buffer << " ";
  67. if (*buffer == '#') {
  68. *buffer = '\n';
  69. isExit = true;
  70. }
  71.  
  72. } while (*buffer != 42);
  73. cout << endl;
  74.  
  75. } while (!isExit);
  76.  
  77. /* ---------------- CLOSE CALL ------------- */
  78. /* ----------------- close() --------------- */
  79.  
  80. /*
  81. Once the server presses # to end the connection,
  82. the loop will break and it will close the server
  83. socket connection and the client connection.
  84. */
  85.  
  86. cout << "\n=> Connection terminated.\nGoodbye...\n";
  87.  
  88. close(client);
  89.  
  90.  
  91. }
  92. else if(choice == 2)
  93. {
  94. printf("TODO");
  95. }
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement