|
here my code ,written for tcp server for ipv6 socket programing it is giving binding problem???
// 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;
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
local.sin6_addr=in6addr_any; //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");
}
if(bind(sd,(sockaddr*)&local,sizeof(local))!=0)
{
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;
}
|
|
|
|