Advertisement
captainIBM

Socket Chat server

Dec 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5.  
  6. #define PORT 2000
  7.  
  8. int init_connection();
  9. int receive(int csock);
  10. int send_message(int csock);
  11.  
  12. int main()
  13. {
  14.     int socket = init_connection();
  15.     int rest = 1;
  16.  
  17.     while(rest){
  18.         rest = receive(socket);
  19.         rest = send_message(socket);
  20.     }
  21.  
  22.     return EXIT_SUCCESS;
  23. }
  24.  
  25. int send_message(int csock){
  26.     char bufferSend[50];
  27.     int error;
  28.  
  29.     printf("<<<< SERVER DIT: ");
  30.     fgets(bufferSend, sizeof(bufferSend), stdin);
  31.     error = send(csock, bufferSend, strlen(bufferSend), 0);
  32.     printf("\n...................Attente de la reponce du client (passiontez svp)....\n\n");
  33.  
  34.     if(error == SOCKET_ERROR){
  35.             printf(":-/ Socket error, send !\n");
  36.             return 0;
  37.     }
  38.  
  39.     return 1;
  40.  
  41. }
  42.  
  43.  
  44. int receive(int csock){
  45.     char bufferRecv[50];
  46.     int error;
  47.     error = recv(csock, bufferRecv, sizeof(bufferRecv)-1, 0);
  48.         if(error != SOCKET_ERROR){
  49.             bufferRecv[error] = '\0';
  50.             printf(">>>> CLIEN DIT: %s\n",bufferRecv);
  51.             return 1;
  52.         }else{
  53.             printf(":-/ Socket error, recv !\n");
  54.             return 0;
  55.         }
  56.  
  57. }
  58.  
  59. int init_connection(){
  60.     int error;
  61.  
  62.     WSADATA WSAData;
  63.     WSAStartup(MAKEWORD(2,0), &WSAData);
  64.  
  65.     SOCKADDR_IN sin = {0};
  66.     SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
  67.  
  68.     if( sock != INVALID_SOCKET ){
  69.         printf(":-) Socket %d is now opened in TCP/IP mode.\n",sock);
  70.  
  71.         sin.sin_addr.s_addr = htonl(INADDR_ANY);
  72.         sin.sin_family = AF_INET;
  73.         sin.sin_port = htons(PORT);
  74.  
  75.         error = bind(sock, (SOCKADDR *)&sin, sizeof(sin));
  76.         if( error != SOCKET_ERROR ){
  77.             error = listen(sock, 2);
  78.             if( error != SOCKET_ERROR ){
  79.                 printf(":-) listening on port %d\n"
  80.                 "Waiting for a client connection on this port...\n",PORT);
  81.  
  82.                 SOCKADDR_IN csin = {0};
  83.                 int sizeofcsin = sizeof(csin);
  84.                 SOCKET csock = accept(sock, (SOCKADDR *)&csin, &sizeofcsin);
  85.                 if( csock != INVALID_SOCKET ){
  86.                     printf("::::: Client connected\n\n");
  87.                     return csock;
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement