i'm trying to create server with TCP IP protocol
But it doesn't accept connection, or may be because of listen
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd,newsockfd,num_port,serveur_T;
socklen_t client_T;
char buffer[200];
struct sockaddr_in adr_serveur, adr_client;
int n;
if (argc < 2)
{
fprintf(stderr, "nombre d'arguments est insuffisant\n");
exit(1);
}
sockfd=socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
error("erreur de creation de socket");
}
serveur_T=sizeof(adr_serveur);
bzero((char*)&adr_serveur, serveur_T);
num_port=atoi(argv[1]);
adr_serveur.sin_family=AF_INET;
adr_serveur.sin_addr.s_addr=INADDR_ANY;
adr_serveur.sin_port=htons(num_port);
serveur_T=sizeof(adr_serveur);
if (bind ( sockfd,(struct sockaddr *) &adr_serveur,serveur_T)<0)
{
error(" Erreur de binding");
}
listen (sockfd,5);
client_T= sizeof(adr_client);
newsockfd= accept(sockfd,(struct sockaddr *) &adr_client,&client_T);
if ( newsockfd<0)
{
error("Erreur socket accept");
}
bzero(buffer, 200);
return 0;}
When I execute server I got this error
Erreur socket accept: Operation not supported
Second question: Can I use an IRC client and connect it to my server ? In my school we have Linux servers so I'm wondering if I can use them as a hostname ? Thanks
i'm trying to create server with TCP IP protocol
You have created a SOCK_DGRAM
(UDP) socket, not a SOCK_STREAM
(TCP) socket. You cannot call listen()
or accept()
on a UDP socket, only on a TCP socket. listen()
is reporting an EOPNOTSUPP
error:
EOPNOTSUPP
The socket is not of a type that supports the listen() operation.
You are ignoring that error, and then accept()
is reporting the same error:
EOPNOTSUPP
The referenced socket is not of type SOCK_STREAM.
There are no connections in UDP, so there is nothing to accept. Once you have bound a UDP socket to a port, you can start calling recvfrom()
and sendto()
on it.
In order to connect an IRC client to this server code, you need to change the socket type to SOCK_STREAM
. IRC runs on TCP, not on UDP.