Pages : 1
#1 Le 13/01/2019, à 21:02
- cosmoff
langage C probleme avec une socket
bonjour,
voile je souhaite communiquer avec un serveur en local mais je rencontre des problemes du coté Client.
voici mon code :
int main(void)
{
int socket_fd;
/* ouvre fd pour le protocole tcp*/
if ( (socket_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP) ) < 0)
{
fprintf(stdout, "error socket");
return -1;
}
struct sockaddr_in sock_addr_init;
sock_addr_init.sin_port = htons(15000);
sock_addr_init.sin_family = AF_INET;
sock_addr_init.sin_addr.s_addr = inet_addr("127.0.0.1");//retire htonl de merde
if ( bind( socket_fd, (struct sockaddr*)&sock_addr_init,
sizeof(sock_addr_init) ) < 0 )
{
fprintf(stdout, "error bind");
close(socket_fd);
return -1;
}
close(socket_fd);
return 0;
}
Le terminal me retourne error bind que j'avais placé dans mon fprintf() mais je ne comprend pas pourquoi. Je souhaite parler à un serveur en local sur le port 15000. Si je ne lance pas mon serveur le programme se déroule sans soucis, mais des que je lance mon server puis mon client je tombe sur error bind.
je vous montre mon code pour le server :
int main(void)
{
int socket_fd;
/* ouvre fd pour le protocole tcp*/
if ( (socket_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP) ) < 0)
{
fprintf(stdout, "error socket");
return -1;
}
struct sockaddr_in sock_addr_init;
sock_addr_init.sin_port = htons(15000);
sock_addr_init.sin_family = AF_INET;
sock_addr_init.sin_addr.s_addr = htonl(INADDR_ANY);
socklen_t longueur = sizeof(sock_addr_init);
if ( bind( socket_fd, (struct sockaddr*)&sock_addr_init,
sizeof(sock_addr_init) ) < 0 )
{
fprintf(stdout, "error bind");
close(socket_fd);
return -1;
}
listen(socket_fd, 5); //5 connexion max dans la file d'attente
if ( accept( socket_fd, (struct sockaddr*)&sock_addr_init,
&longueur ) < 0 )
{
fprintf(stdout, "error accept");
close(socket_fd);
return -1;
}
}
Merci d'avance pour votre aide
Hors ligne
#2 Le 14/01/2019, à 09:11
- DonutMan75
Re : langage C probleme avec une socket
Hello,
ça fait longtemps que je n'ai plus fait ce genre d'exercice et peut-être y'a t'il une erreur flagrante dans ton code.
En tout cas, une première piste de voir ce que contient la variable errno (cf. errno.h) ?
Bon courage pour la suite.
Donut
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.ERRORS
EACCES The address is protected, and the user is not the superuser.EADDRINUSE
The given address is already in use.EADDRINUSE
(Internet domain sockets) The port number was specified as zero in the socket address structure, but, upon attempting to bind to an ephemeral
port, it was determined that all port numbers in the ephemeral port range are currently in use. See the discussion of
/proc/sys/net/ipv4/ip_local_port_range ip(7).EBADF sockfd is not a valid file descriptor.
EINVAL The socket is already bound to an address.
EINVAL addrlen is wrong, or addr is not a valid address for this socket's domain.
ENOTSOCK
The file descriptor sockfd does not refer to a socket.The following errors are specific to UNIX domain (AF_UNIX) sockets:
EACCES Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
EADDRNOTAVAIL
A nonexistent interface was requested or the requested address was not local.EFAULT addr points outside the user's accessible address space.
ELOOP Too many symbolic links were encountered in resolving addr.
ENAMETOOLONG
addr is too long.ENOENT A component in the directory prefix of the socket pathname does not exist.
ENOMEM Insufficient kernel memory was available.
ENOTDIR
A component of the path prefix is not a directory.EROFS The socket inode would reside on a read-only filesystem.
Hors ligne
#3 Le 14/01/2019, à 20:58
- cosmoff
Re : langage C probleme avec une socket
Merci pour ta réponse.
J'ai ajouté
fprintf(stdout, "errno = %s\n", strerror(errno));
et ca me retourne errno = Address already in use
pourquoi? je ne peux pas utiliser l'adresse 127.0.0.1 ?
Hors ligne
#4 Le 14/01/2019, à 22:48
- DonutMan75
Re : langage C probleme avec une socket
Bonsoir,
y'a ce fil sur Stack Overflow qui semble assez similaire à ton problème : https://stackoverflow.com/questions/510 … e-port-num
Le port 15000 ne semble pas réservé (sauf par un truc assez obscur : http://www.networksorcery.com/enp/proto … 15000.htm)
Tiens-nous au courant
Bonne soirée à tous,
D.
Hors ligne
#5 Le 15/01/2019, à 12:37
- cosmoff
Re : langage C probleme avec une socket
l'erreur venait que dans mon client j'avais mis la fonction bind() au lien de connect() voila tout
Merci pour votre aide
Hors ligne