Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 14th, 2018 46 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9.  
  10. int main()
  11. {
  12.     int sockfd;
  13.     struct sockaddr_in dest;
  14.     char buffer[] = "Hello World!";
  15.     char buffered[128];
  16.  
  17.     /* create socket , same as client */
  18.     sockfd = socket(PF_INET, SOCK_STREAM, 0);
  19.  
  20.     /* initialize structure dest */
  21.     bzero(&dest, sizeof(dest));
  22.     dest.sin_family = PF_INET;
  23.     dest.sin_port = htons(8889);
  24.     /* this line is different from client */
  25.     dest.sin_addr.s_addr = INADDR_ANY;
  26.  
  27.     /* Assign a port number to socket */
  28.     bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));
  29.  
  30.     /* make it listen to socket with max 20 connections */
  31.     listen(sockfd, 20);
  32.  
  33.     /* infinity loop -- accepting connection from client forever */
  34.     while(1)
  35.     {
  36.         int clientfd;
  37.         int sock;
  38.         struct sockaddr_in client_addr;
  39.         socklen_t addrlen = sizeof(client_addr);
  40.  
  41.         /* Wait and Accept connection */
  42.         clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
  43.         recv(clientfd, buffered, sizeof(buffer), 0);
  44.         printf("%s",buffered);
  45.  
  46.         /* Send message */
  47.         connect(sock, (struct sockaddr*)&dest, sizeof(dest));
  48.         send(sock, buffer, sizeof(buffer), 0);
  49.         //recv(sockfd, buffered, sizeof(buffer), 0);
  50.        
  51.                
  52.         /* close(client) */
  53.         close(clientfd);
  54.     }
  55.  
  56.     /* close(server) , but never get here because of the loop */
  57.     close(sockfd);
  58.     return 0;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top