Hi,
I am trying to develop a small IPv6 application in C. I am stuck at server side. GCC issues the error while call to ACCEPT function "socket function on non-socket". Can anyone help me?
1. Client Code
#include "shahab.h"
int main(int argc, char **argv){
int connectSocket;
int numberOfBytes;
char recvData[MAX_SIZE];
int yes=1;
struct sockaddr_in6 serverAddress;
if(argc!=2){
perror("Command Line Arguments");
exit(1);
}
if((connectSocket=socket(AF_INET6,SOCK_STREAM,0))<0){
perror("socket error");
exit(1);
}
bzero(&serverAddress, sizeof(serverAddress));
serverAddress.sin6_family=AF_INET6;
serverAddress.sin6_port=htons(SERVER_A_PORT);
if(inet_pton(AF_INET6,argv[1],&serverAddress.sin6_addr)<=0){
perror("Server Address");
exit(1);
}
if(connect(connectSocket,(struct sockaddr*)&serverAddress,sizeof(serverAddress))<0){
perror("connect error");
exit(1);
}
for(;;){
numberOfBytes=read(connectSocket,recvData,MAX_SIZE);
if(numberOfBytes<0){
perror("Recv Error");
exit(1);
}
recvData[numberOfBytes]='\0';
printf("Received : %s\n",recvData);
}
close(connectSocket);
printf("I am in IPv6 Client\n");
return 0;
}
2. Server Code
#include "shahab.h"
int main(){
int listenSocket;
int connectSocket;
int numberOfBytes;
int yes=1;
char recvData[MAX_SIZE];
char ip6[INET6_ADDRSTRLEN];
socklen_t sin_size;
struct sockaddr_in6 serverAddress;
struct sockaddr_in6 clientAddress;
if(listenSocket=socket(AF_INET6,SOCK_STREAM,0)<0){
perror("Connect Problem");
}
/* if(setsockopt(listenSocket,SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int))==-1){
perror("Sock Option Problem");
exit(1);
}*/
bzero(&serverAddress,sizeof(serverAddress));
serverAddress.sin6_family=AF_INET6;
serverAddress.sin6_addr=in6addr_any;
serverAddress.sin6_port=htons(SERVER_A_PORT);
bind(listenSocket,(struct sockaddr*)&serverAddress,sizeof(serverAddress));
listen(listenSocket,BACKLOG);
for(;;){
sin_size=sizeof serverAddress;
connectSocket= accept(listenSocket, (struct sockaddr *)&clientAddress, &sin_size);
if(connectSocket==-1){
perror("Problem");
}
/* inet_ntop(AF_INET6, &(clientAddress.sin6_addr), ip6, INET6_ADDRSTRLEN);
printf("The address is: %s\n", ip6);*/
send(connectSocket, "Hello World from Server\n", MAX_SIZE, 0);
close(connectSocket);
}
printf("I am IPv6 Server\n");
return 0;
}
Thanks