[ale] IPv6 programming question
Michael H. Warfield
mhw at wittsend.com
Fri Nov 2 13:37:19 EDT 2007
On Fri, 2007-11-02 at 10:14 +0530, Srikanth Pokkuluri wrote:
>
> Hi All,
> I have written client server program on linux using ipv6
> programming ... (stream sockets)
>
> But when i am sending data ...The data is not been received at
> server .... please correct where the error is thanks
>
> int main()
> {
> int s,r; int ret;
> uint16_t val=0;
> char *buff;
> struct sockaddr_in6 ser;
>
> s = socket(AF_INET6,SOCK_STREAM,PF_INET6);
> if ( s <0)
>
> perror ("socket 6 error");
> else
> /* ser.sin6_addr.s6_addr="fe02::1"; IN6ADDR_ANY_INIT; */
> ser.sin6_port=htons(2000);
> ser.sin6_family=AF_INET6;
> ser.sin6_flowinfo=0;
> ser.sin6_scope_id=0;
>
> inet_pton(AF_INET6,"::1"/*IN6ADDR_LOOPBACK_INIT*/,&(ser.sin6_addr));
> buff = (char *) malloc (20* sizeof(char));
> strcpy(buff,"srikanth");
> ret = connect(s,(struct sockaddr*)&ser,sizeof(struct sockaddr_in6));
> if( ret <0)
> perror ( " coonect error ");
> else
> r = send(s,buff,20,0);
> if ( r <= 0)
> perror ("send error");
> close(s);
>
>
>
> SERVER PROGRAM
> int main()
> {
> int s,consd; int ret;
> uint16_t val=0;
> char buff[20];
> struct sockaddr_in6 ser,cli; char ipAddr[128];
> socklen_t t,clilen;
> s = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP6);
> if ( s <0)
>
> perror ("socket 6 error");
> else
> ser.sin6_port=htons(2000);
> ser.sin6_family=AF_INET6;
> ser.sin6_flowinfo=0;
> ser.sin6_scope_id=0;
>
> //inet_pton(AF_INET6,"fe80000000000000020795fffee4801f",&(ser.sin6_addr));
> inet_pton(AF_INET6,"::1",&(ser.sin6_addr));
> t = sizeof(struct sockaddr_in6);
> ret = bind(s,(struct sockaddr*)&ser,(struct sockaddr*)t);
> if( ret <0)
> listen(s,5);
> clilen = sizeof(struct sockaddr_in6);
> memset(buff,'\0',sizeof(buff));
> ipAddr[0]='\0';
>
> inet_ntop(int af, const void *src, char *dst, socklen_t cnt */
I agree with what Jim pointed out. What's up with the line above?
>
> while(1)
> {
> printf("Server waiting :) \n\n ");
> consd = accept(s,(struct sockaddr*)&cli,(socklen_t*)&(clilen));
Accept returns the file descriptor to the accepted socket.
> if( consd <-1 )
> perror("accept error");
> else
> {
> recv(s,buff,sizeof(buff),0);
You're doing a recv on the wrong socket. You should be recv'ing on
consd. Socket "s" is your listening socket.
> printf("Received from client data =%s",buff);
> printf("Received from client port =%d",ntohs(cli.sin6_port));
> break;
> }
> }
> close(s)
> }
> Thanks in advance
Mike
--
Michael H. Warfield (AI4NB) | (770) 985-6132 | mhw at WittsEnd.com
/\/\|=mhw=|\/\/ | (678) 463-0932 | http://www.wittsend.com/mhw/
NIC whois: MHW9 | An optimist believes we live in the best of all
PGP Key: 0xDF1DD471 | possible worlds. A pessimist is sure of it!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: This is a digitally signed message part
More information about the Ale
mailing list