Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Fastolfe en 5 Enero 2012, 20:52 pm



Título: Problema con sockets
Publicado por: Fastolfe en 5 Enero 2012, 20:52 pm
Buenas!! acabo de empezar con los sockets y mi  programa solo envia y recibe una cadena (lo estoy probando con el netcat), lo que quiero es que sea una especie de "chat", aunque no sabiendo por que falla no sigo programando para no acumular errores.

El código es este:

Código
  1. #include <stdio.h>          
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5.  
  6. #define MAXR 100
  7.  
  8. int main(){
  9.  
  10. int fd, fd2;
  11.  
  12. struct sockaddr_in server;
  13.  
  14. struct sockaddr_in client;
  15.  
  16. int sin_size;
  17.  
  18. if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {  
  19. printf("error en socket()\n");
  20. exit(-1);
  21. }
  22.  
  23. printf("Introduce el puerto en el que quieres escuchar: ");
  24. int puerto = 0;
  25. scanf("%d", &puerto);
  26.  
  27. char cadena[100];
  28. char *pcadena = &cadena;
  29. int tcadena = 0;
  30.  
  31. server.sin_family = AF_INET;
  32. server.sin_port = htons(puerto);
  33. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  34. bzero(&(server.sin_zero),8);
  35.  
  36. bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr));
  37. listen(fd,1);
  38.  
  39. char recibido[MAXR];
  40. char *precibido = &recibido;
  41. int i = 0;
  42.  
  43. while (1){
  44. sin_size=sizeof(struct sockaddr_in);
  45. fd2 = accept(fd, (struct sockaddr *)&client, &sin_size);
  46.  
  47. scanf("%s", &cadena);
  48.  
  49. tcadena = 0;
  50.  
  51. while (cadena[tcadena] != '\0' && tcadena < 100){
  52. tcadena++;
  53. }
  54.  
  55. send(fd2,pcadena,tcadena,0);
  56.  
  57. recv(fd2, precibido, MAXR, 0);
  58.  
  59. printf("\n %s", recibido);
  60. }
  61.  
  62. return 0;
  63.  
  64. }

Muchas gracias de antemano!! :)

PD: programo en ubuntu 11.04


Título: Re: Problema con sockets
Publicado por: Sagrini en 6 Enero 2012, 23:10 pm
http://wiki.elhacker.net/programacion/cc/articulos/introducion-a-los-sockets-en-ansi-c
http://wiki.elhacker.net/programacion/cc/articulos/introducion-a-los-sockets-en-ansi-c/Tutorial_Sockets.pdf?attredirects=0&d=1
Código
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <time.h>
  8.  
  9. int socketfd, newsocket;
  10. char *filename;
  11.  
  12. void shutup (int signal)
  13. {
  14. FILE *log;
  15. log=fopen (filename, "a+");
  16. times ();
  17. fprintf (log, "Shutting down...\n\n");
  18. close (newsocket);
  19. close (socketfd);
  20. exit (0);
  21. }
  22.  
  23. int times ()
  24. {
  25. FILE *log;
  26. time_t now=time (0);
  27. struct tm *ahora;
  28. char buffer [40];
  29. ahora=localtime ((const time_t*)&now);
  30. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S", ahora);
  31. log=fopen (filename, "a+");
  32. printf ("%s  ", buffer);
  33. fprintf (log,"%s ", buffer);
  34. return 0;
  35. }
  36.  
  37. int main (int argc, char *argv [])
  38. {
  39. time_t now=time (0);
  40. struct tm *ahora;
  41. char hora [40];
  42. ahora=localtime ((const time_t*)&now);
  43. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  44. printf ("SmallServ 2.0 : By Sagrini : Sagrini2010 : %s\n", hora);
  45. if (getuid()!=0)
  46. {
  47. printf ("This proccess must be run by root.\n\n");
  48. return 1;
  49. }
  50. if (argc<3)
  51. {
  52. printf ("Use: %s <PORT> <LOG>\n\n", argv[0]);
  53. return 1;
  54. }
  55. int cont;
  56. FILE *log;
  57. struct sockaddr_in client, host;
  58. char buffer [1024];
  59. int size=sizeof (client);
  60. filename = argv [2];
  61. socketfd=socket (2, 1 , 0);
  62. host.sin_family=AF_INET;
  63. host.sin_port=htons (atoi (argv [1]));
  64. host.sin_addr.s_addr=0;
  65. bind (socketfd, (struct sockaddr*)&host, sizeof(struct sockaddr));
  66. listen (socketfd, 3);
  67. log=fopen (filename, "a+");
  68. times ();
  69. fprintf (log, "Starting up...\n\n");
  70. signal (SIGTERM, shutup);
  71. signal (SIGINT, shutup);
  72. daemon (1, 0);
  73. while (1)
  74. {
  75. newsocket=accept (socketfd, (struct sockaddr*)&client, &size);
  76. log=fopen (filename, "a+");
  77. times ();
  78. fprintf (log, "Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  79. cont=recv (newsocket, &buffer, 1024, 0);
  80. while (cont>2)
  81. {
  82. log=fopen (filename, "a+");
  83. times ();
  84. buffer [cont-1] = '\0';
  85. fprintf (log, "RECV %d bytes: %s\n", cont-2, buffer);
  86. cont=recv (newsocket, &buffer, 1024, 0);
  87. }
  88. log=fopen (filename, "a+");
  89. times ();
  90. fprintf (log, "Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs(client.sin_port));
  91. close (newsocket);
  92. }
  93. close (socketfd);
  94. return 0;
  95. }
  96.  


Título: Re: Problema con sockets
Publicado por: Fastolfe en 7 Enero 2012, 01:01 am
Muchas gracias Sagrini!! Me pondré a comprender tu programa y repasar de nuevo el tutorial.
Gracias! ;)