• Domingo 19 de Mayo de 2024, 16:10

Autor Tema:  Socket C, Programa Servidor  (Leído 1067 veces)

neohex

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Socket C, Programa Servidor
« en: Martes 15 de Junio de 2004, 23:48 »
0
Bueno pues tengo un pequeño problema...voy a empezar poniendo mi source haber si encontrais algún error, yo creo que no tiene ninguno pero simplemente no funciona, es el primer programa(por llamarlo de alguna manera) que hago con socket en C:
Código: Text
  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5.  
  6. #define MYPORT 3333 //Puerto que se deja a la escucha
  7. #define NUBACK 5 //Numero maximo de conexiones a la espera.
  8.  
  9. int main()
  10. {
  11. int nSocket;
  12. int nSocket2;
  13. int nBind;
  14. int nListen;
  15. struct sockaddr_in my_dire;
  16. struct sockaddr_in su_dire;
  17. int sin_zero;
  18.  
  19. if ((nSocket=socket(AF_INET, SOCK_STREAM, 0)) == -1)
  20. {
  21. perror("[-] Socket");
  22. exit(1);
  23. }
  24. printf("[+] Socket iniciado.\n");
  25. my_dire.sin_family = AF_INET;
  26. my_dire.sin_port = htons(MYPORT);
  27. my_dire.sin_addr.s_addr = INADDR_ANY;
  28. memset(&(my_dire.sin_zero), '\0', '8');
  29. if ((nBind=bind(nSocket, (struct sockaddr *)&my_dire, sizeof(struct sockaddr))) == -1)
  30. {
  31. perror("[-] Bind");
  32. exit(1);
  33. }
  34. printf("[+] Bind iniciado.\n");
  35. if ((nListen=listen(nSocket, NUBACK)) == -1)
  36. {
  37. perror("[-] Listen");
  38. exit(1);
  39. }
  40. printf("[+] Listen iniciado.\n");
  41. sin_zero = sizeof(struct sockaddr_in);
  42. if ((nSocket2=accept(nSocket, (struct sockaddr *)&su_dire, &sin_zero)) == -1)
  43. {
  44. perror("[-] Accept");
  45. exit(1);
  46. }
  47. printf("[+] Accept iniciado.\n");
  48.  
  49. return 0;
  50. }
  51.  

Esto me sale al ejecutarlo:
Código: Text
  1. [neohex@localhost neohex]$ gcc server.c -o server
  2. [neohex@localhost neohex]$ server
  3. [+] Socket iniciado.
  4. [-] Bind: Socket operation on non-socket
  5. [neohex@localhost neohex]$
  6.  

Haber si alguien puede ayudarme a solucionarlo..... :huh:

neohex

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Re: Socket C, Programa Servidor
« Respuesta #1 en: Miércoles 16 de Junio de 2004, 09:51 »
0
Bueno pues ya está corregido el error, lo pongo por si ha alguien le sirve:

El error estaba en esta linea:
Código: Text
  1. memset(&(my_dire.sin_zero), '\0', '8');
  2.  
porque había puesto el 8 entre ' '.