Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. #define MAXLINE 4096 /*max text line length*/
  10. #define SERV_PORT 3000 /*port*/
  11. #define LISTENQ 8 /*maximum number of client connections */
  12.  
  13. int main (int argc, char **argv)
  14. {
  15. int listenfd, connfd, n;
  16. socklen_t clilen;
  17. char buf[MAXLINE];
  18. struct sockaddr_in cliaddr, servaddr;
  19.  
  20. //creation of the socket
  21. listenfd = socket (AF_INET, SOCK_STREAM, 0);
  22.  
  23. //preparation of the socket address
  24. servaddr.sin_family = AF_INET;
  25. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  26. servaddr.sin_port = htons(SERV_PORT);
  27.  
  28. bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  29.  
  30. listen(listenfd, LISTENQ);
  31.  
  32. printf("%sn","Server running...waiting for connections.");
  33.  
  34. for ( ; ; ) {
  35.  
  36. clilen = sizeof(cliaddr);
  37. connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
  38. printf("%sn","Received request...");
  39.  
  40. while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
  41. printf("%s","String received from and resent to the client:");
  42. puts(buf);
  43. send(connfd, buf, n, 0);
  44. }
  45.  
  46. if (n < 0) {
  47. perror("Read error");
  48. exit(1);
  49. }
  50. close(connfd);
  51.  
  52. }
  53. //close listening socket
  54. close(listenfd);
  55.  
  56. }
  57.  
  58. #include <stdlib.h>
  59. #include <stdio.h>
  60. #include <sys/types.h>
  61. #include <sys/socket.h>
  62. #include <netinet/in.h>
  63. #include <string.h>
  64. #include <arpa/inet.h>
  65.  
  66.  
  67. #define MAXLINE 4096 /*max text line length*/
  68. #define SERV_PORT 3000 /*port*/
  69.  
  70. int
  71. main(int argc, char **argv)
  72. {
  73. int sockfd;
  74. struct sockaddr_in servaddr;
  75. char sendline[MAXLINE], recvline[MAXLINE];
  76.  
  77. //basic check of the arguments
  78. //additional checks can be inserted
  79. if (argc !=2) {
  80. perror("Usage: TCPClient <IP address of the server");
  81. exit(1);
  82. }
  83.  
  84. //Create a socket for the client
  85. //If sockfd<0 there was an error in the creation of the socket
  86. if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  87. perror("Problem in creating the socket");
  88. exit(2);
  89. }
  90.  
  91. //Creation of the socket
  92. memset(&servaddr, 0, sizeof(servaddr));
  93. servaddr.sin_family = AF_INET;
  94. servaddr.sin_addr.s_addr= inet_addr(argv[1]);
  95. servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order
  96.  
  97. //Connection of the client to the socket
  98. if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
  99. perror("Problem in connecting to the server");
  100. exit(3);
  101. }
  102.  
  103. while (fgets(sendline, MAXLINE, stdin) != NULL) {
  104.  
  105. send(sockfd, sendline, strlen(sendline), 0);
  106.  
  107. if (recv(sockfd, recvline, MAXLINE,0) == 0){
  108. //error: server terminated prematurely
  109. perror("The server terminated prematurely");
  110. exit(4);
  111. }
  112. printf("%s", "String received from the server: ");
  113. fputs(recvline, stdout);
  114. }
  115.  
  116. exit(0);
  117. }
  118.  
  119. #include <stdlib.h>
  120. #include <stdio.h>
  121. #include <sys/types.h>
  122. #include <sys/socket.h>
  123. #include <netinet/in.h>
  124. #include <string.h>
  125. #include <arpa/inet.h>
  126.  
  127.  
  128. #define MAXLINE 4096 /*max text line length*/
  129. #define SERV_PORT 3000 /*port*/
  130.  
  131. int
  132. main(int argc, char **argv)
  133. {
  134. int sockfd;
  135. struct sockaddr_in servaddr;
  136. char sendline[MAXLINE], recvline[MAXLINE], buffer [256];
  137. //Añadidas por mi//
  138. float a, b, c;
  139. a = 0;
  140. b = 0;
  141. c = 0;
  142. c = a + b;
  143.  
  144. printf("nPrimer numero: ");
  145. scanf("%f", &a);
  146. printf ("nSegundo numero: ");
  147. scanf ("%f", &b);
  148.  
  149. sprintf(buffer, "%f", sizeof c, c);
  150.  
  151.  
  152. unsigned char len = strlen(buffer);
  153.  
  154. //basic check of the arguments
  155. //additional checks can be inserted
  156. if (argc !=2) {
  157. perror("Usage: TCPClient <IP address of the server");
  158. exit(1);
  159. }
  160.  
  161. //Create a socket for the client
  162. //If sockfd<0 there was an error in the creation of the socket
  163. if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  164. perror("Problem in creating the socket");
  165. exit(2);
  166. }
  167.  
  168. //Creation of the socket
  169. memset(&servaddr, 0, sizeof(servaddr));
  170. servaddr.sin_family = AF_INET;
  171. servaddr.sin_addr.s_addr= inet_addr(argv[1]);
  172. servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order
  173.  
  174. //Connection of the client to the socket
  175. if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
  176. perror("Problem in connecting to the server");
  177. exit(3);
  178. }
  179.  
  180. while (fgets(sendline, MAXLINE, stdin) != NULL) {
  181.  
  182. send(sockfd, sendline, strlen(sendline), 0);
  183. send(sockfd, &len, sizeof len, 0);
  184. send (sockfd, buffer, sizeof buffer, 0);
  185.  
  186. if (recv(sockfd, recvline, MAXLINE,0) == 0){
  187. //error: server terminated prematurely
  188. perror("The server terminated prematurely");
  189. exit(4);
  190. }
  191. printf("%s", "String received from the server: ");
  192. fputs(recvline, stdout);
  193. }
  194.  
  195. exit(0);
  196. }
  197.  
  198. #include <stdlib.h>
  199. #include <stdio.h>
  200. #include <sys/types.h>
  201. #include <sys/socket.h>
  202. #include <netinet/in.h>
  203. #include <string.h>
  204. #include <unistd.h>
  205.  
  206. #define MAXLINE 4096 /*max text line length*/
  207. #define SERV_PORT 3000 /*port*/
  208. #define LISTENQ 8 /*maximum number of client connections */
  209.  
  210. int main (int argc, char **argv)
  211. {
  212. float c;
  213. int listenfd, connfd, n;
  214. socklen_t clilen;
  215. char buf[MAXLINE], buf256[256], buffer[256];
  216. unsigned char len = strlen(buffer);
  217. struct sockaddr_in cliaddr, servaddr;
  218.  
  219. //creation of the socket
  220. listenfd = socket (AF_INET, SOCK_STREAM, 0);
  221.  
  222. //preparation of the socket address
  223. servaddr.sin_family = AF_INET;
  224. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  225. servaddr.sin_port = htons(SERV_PORT);
  226.  
  227. bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  228.  
  229. listen(listenfd, LISTENQ);
  230.  
  231. printf("%sn","Server running...waiting for connections.");
  232.  
  233. for ( ; ; ) {
  234.  
  235. clilen = sizeof(cliaddr);
  236. connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
  237. printf("%sn","Received request...");
  238.  
  239. while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
  240. printf("%s","String received from and resent to the client:");
  241. puts(buf);
  242.  
  243.  
  244. recv(connfd, &len, sizeof len, 0);
  245. recv(connfd, buf256, len, 0);
  246. recv(connfd, buffer, 256, 0);
  247. buf256[len] = 0;
  248. sscanf(buffer, "%f", &c);
  249. puts(buffer);
  250. printf ("n%f", &c);
  251. send(connfd, buf, n, 0);
  252. }
  253.  
  254. if (n < 0) {
  255. perror("Read error");
  256. exit(1);
  257. }
  258. close(connfd);
  259.  
  260. }
  261. //close listening socket
  262. close(listenfd);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement