windows tcp socket programing for ipv6 stack binding problemm?????

networking India
  • 13 years ago

    Here my code i am geting binding problem,if any body can help it,,,,

    it is for windows ipv6 sockets tcp server...simple application

    Thank you in advance..

    // tcpserver6.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <winsock2.h>
    #include <ws2tcpip.h>
    //#include <Wspiapi.h>
    #include <stdio.h>




    int main(int argc, char* argv[])
    {
       SOCKET sd,newsd;
     unsigned short int port = 8888;
     unsigned long int total;
     int err;
     char *buf;
        printf("Hello\n");
     struct sockaddr_in6 local;
    // struct addrinfo local6;








    WSADATA wsaData;

    int wsaret=WSAStartup(0x101,&wsaData);

        //WSAStartup returns zero on success.
        //If it fails we exit.
        if(wsaret!=0)
        {
            return 0;
     }





      //  local.sin6_family=AF_INET6; //Address family
       IN6ADDR_ISANY(&local); //Wild card IP address
      
        local.sin6_port=htons((u_short)8888); //port to use
     
     sd = socket(AF_INET6,SOCK_STREAM,0);
     if(sd == INVALID_SOCKET)
     {
      printf("error\n");
     }









     err=bind(sd,(sockaddr*)&local,sizeof(local));
     if(err != 0)
        {
      printf( "bind return value ==%d\n",err);
            return 0;
        }




     if(listen(sd,10)!=0)
        {
            return 0;
        }


     

       printf("Waiting for data on port %d\n",port);
       int i =0;
       total = 0;
       while(1)
       {
           newsd = accept(sd,NULL,NULL);
          if(newsd < 0)
       {
          perror("Cannot accept connection");
      closesocket(newsd);
      return 0;
       }










           buf = (char *)malloc (10000);
        err = recv(newsd,buf,10000,0);
        if(err <= 0)
        {
         break;
        }
        total += err;
        printf("\n Total recv : %d in %d times \n",total,i);
        free(buf);
        i++;
       }
       if(err < 0)
       {
        perror("cannot recv data");
       }
       else
       {
        printf("Recv %d bytes\n",(int)total);
       }
       closesocket(newsd);
        closesocket(sd);



















     //goto again;
        printf("ending \n");
     WSACleanup();


        return 1;
    }



Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”