Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. /*
  2. * client.c
  3. *
  4. * Copyright 2017 Unknown <s01116@lab4-pc14>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. *
  21. *
  22. */
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <unistd.h>
  32. #include <arpa/inet.h>
  33. #include <errno.h>
  34.  
  35. #define SOCKET_ERROR ((int)-1)
  36. #define SERVER_PORT 1324
  37. #define MAXSIZE 10
  38.  
  39. int main(int argc, char **argv)
  40. {
  41. char indirizzoServer[]="127.0.0.1";
  42. char messaggio[]="ciao";
  43. char buffer[MAXSIZE];
  44. char ch,chMaiu;
  45. int ris,nwrite,len;
  46. int socketfd;
  47. struct sockaddr_in locale,remoto;
  48.  
  49. locale.sin_family=AF_INET;
  50. locale.sin_addr.s_addr=htonl(INADDR_ANY);
  51. locale.sin_port=htons(0);
  52. remoto.sin_family=AF_INET;
  53. remoto.sin_addr.s_addr=inet_addr(indirizzoServer);
  54. remoto.sin_port=htons(SERVER_PORT);
  55.  
  56. printf("Creazione del socket()\n");
  57. socketfd=socket(AF_INET,SOCK_STREAM,0);
  58. if(socketfd==SOCKET_ERROR)
  59. {
  60. printf("fallito il bind(),errore: %d \"%s\"\n",errno,strerror(errno));
  61. return(1);
  62. }
  63.  
  64. printf("bind()\n");
  65. ris=bind(socketfd,(struct sockaddr*)&locale,sizeof(locale));
  66. if(ris==SOCKET_ERROR)
  67. {
  68. printf("fallito il bind(),errore: %d \"%s\"\n",errno,strerror(errno));
  69. return(3);
  70. }
  71.  
  72. printf("connect()\n");
  73. ris=connect(socketfd,(struct sockaddr*)&remoto,sizeof(remoto));
  74. if(ris==SOCKET_ERROR)
  75. {
  76. printf("fallita la connect(),errore: %d \"%s\"\n",errno,strerror(errno));
  77. fflush(stdout);
  78. return(4);
  79. }
  80.  
  81. len=strlen(messaggio);
  82. printf("\nstringa lunga: %d \n",len);
  83. for(nwrite=0;nwrite<len;nwrite++)
  84. {
  85. write(socketfd,&(messaggio[nwrite]),1);
  86. if(read(socketfd,&chMaiu,1)>0)
  87. {
  88. printf("carattere ricevuto: %c\n",chMaiu);
  89. buffer[nwrite]=chMaiu;
  90. }
  91. }
  92. close(socketfd);
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement