Advertisement
Guest User

ddd

a guest
Dec 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/errno.h>
  8. #include "stdafx.h"
  9.  
  10. const unsigned short int PORT = 1234;
  11. char buffer[255];
  12.  
  13. int sendall(int fd, const char *data, int *len)
  14. {
  15. int transfered = 0; // Bytes already sent
  16. int left = *len; // Bytes still to send
  17. int i;
  18.  
  19. while (transfered < *len)
  20. {
  21. i = send(fd, data + transfered, left, 0);
  22. if (i == -1)
  23. {
  24. break;
  25. }; // Error!
  26. transfered += i;
  27. left -= i;
  28. };
  29.  
  30. *len = transfered;
  31. return i == -1 ? -1 : 0;
  32. }
  33. int main(void)
  34. {
  35. int fd, connectionfd;
  36. struct sockaddr_in ownAddress;
  37. struct sockaddr_in remAddress;
  38. int addrlen;
  39. char str[INET_ADDRSTRLEN];
  40.  
  41. //create a socket:
  42. if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
  43. {
  44. perror("socket");
  45. exit(1);
  46. }
  47.  
  48. //IP address and port number of the server:
  49. ownAddress.sin_family = AF_INET; //AF_INET= IPv4
  50. ownAddress.sin_port = htons(PORT); //htons=change Host byte order TO Network byte order, Short data
  51. ownAddress.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY=listen on each
  52. //available network interface;
  53. //can also listen on a specific address
  54. memset(&(ownAddress.sin_zero), '\0', 8);
  55.  
  56. //bind to the address and port:
  57. if (bind(fd, (struct sockaddr *) &ownAddress, sizeof(struct sockaddr)) == -1)
  58. {
  59. perror("bind");
  60. exit(2);
  61. }
  62.  
  63. //listen (wait) for incoming connections from clients:
  64. if (listen(fd, 5) == -1) //5=backlog (waiting queue length), usually between 5 and 10
  65. {
  66. perror("listen");
  67. exit(3);
  68. }
  69.  
  70. puts("Waiting for incoming connections...");
  71.  
  72. //accept the incoming connection, ie. establish communication with a client:
  73. addrlen = sizeof(struct sockaddr_in);
  74. if ((connectionfd = accept(fd, (struct sockaddr *)&remAddress, &addrlen)) == -1)
  75. {
  76. perror("accept");
  77. exit(4);
  78. }
  79.  
  80. //now we have a working connection between this server and some client;
  81. //we can receive and send data
  82.  
  83. //we can also check the address of the connected client:
  84. inet_ntop(AF_INET, &(remAddress.sin_addr), str, INET_ADDRSTRLEN);
  85. //(ntop=Network byte order TO (textual) Presentation);
  86. //a similar function, inet_ntoa(remAddress.sin_addr), is now deprecated
  87. printf("Connection from: %s\n", str);
  88.  
  89.  
  90. char *message = "Hello";
  91. int messlen;
  92. messlen = strlen(message);
  93. if (sendall(connectionfd, message, &messlen) == -1)
  94. {
  95. fprintf(stderr, "Only %d bytes was transferred because of an error!\n", messlen);
  96. }
  97. else
  98. {
  99. printf("Transfer completed successfully!");
  100. }
  101.  
  102.  
  103. while (recv(connectionfd, buffer, sizeof(buffer), 0) != -1)
  104. {
  105. size_t size = recv(connectionfd, buffer, sizeof(buffer), 0);
  106. printf(buffer);
  107.  
  108. messlen = strlen(buffer);
  109. if (sendall(connectionfd, buffer, &messlen) == -1)
  110. {
  111. fprintf(stderr, "Only %d bytes was transferred because of an error!\n", messlen);
  112. }
  113. else
  114. {
  115. printf("Transfer completed successfully!");
  116. }
  117.  
  118. *buffer = NULL;
  119. close(connectionfd); //close connection when finished with this client
  120. }
  121.  
  122. close(fd); //close server's socket when all server work is finished
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement