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

Try:

connectSocket= accept(listenSocket, NULL, NULL);

Also I would check if an error returned on the two lines

bind(listenSocket,(struct sockaddr*)&serverAddress,sizeof(serverAddress));

listen(listenSocket,BACKLOG);

well,

The error actually returned from BIND?

I quickly tried this an it doesn't fail...Note I never worked with AF_INET6...Also i took the liberty of changing some of your constants..

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <strings.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

char message[] = "This is the server socket message!\n";

int main(){

  int listenSocket;
  int connectSocket;
  int numberOfBytes;
  int yes=1;
  char recvData[4096 + 1];
  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");
	exit(EXIT_FAILURE);	
  }

 /* 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(50000);

  if ((bind(listenSocket,(struct sockaddr*)&serverAddress,sizeof(serverAddress))) < 0)
  {
  	perror("bind");
	exit(EXIT_FAILURE);
  }

  

  if ((listen(listenSocket,5)) < 0)
  {
  	perror("listen");
	exit(EXIT_FAILURE);
  }

  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", 4096, 0);

	fputs("client connected!\n", stdout);
write(connectSocket, message, strlen(message));

	close(connectSocket);

	}
  printf("I am IPv6 Server\n");
  return 0;
}

Note...I tried the above code with a client program I have here and it worked...Meaning it send the data and the client received it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.