bind error during socket programming


Posted on 16th Feb 2014 07:03 pm by admin

i am kinda new to socket programming. wrote the below code for a server. but getting the bind error everytime. i even tried changing the port number(currently is 13 daytime) but doesnt work. please suggest.

#include"header.h"

int main(int argc,char **argv)
{
int listenfd,connfd,retbind;
struct sockaddr_in servaddr;
char buff[MAX];
time_t ticks;

if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("socket error");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY); //instead of this use
servaddr.sin_port=htons(13);

if((retbind=bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))<0)
{
printf("bind error %dn",retbind);
exit(1);
}
listen(listenfd,1024);
for(;
{
connfd=accept(listenfd,(struct sockaddr *) NULL,NULL);
ticks=time(NULL);
snprintf(buff,sizeof(buff),"%srn",ctime(&ticks));
write(connfd,buff,strlen(buff));
close(connfd);
}
return 0;
}

No comments posted yet

Your Answer:

Login to answer
184 Like 40 Dislike
Previous forums Next forums
Other forums

Do something every fifth time?
I'm trying to write a loop, but I want it to do something different after every fifth instance. Like

Implementing Single Sign-On using SAML 1.1, x.509, LDAP in C#.net
Hi, I got a requirement from the client i.e implementing single sing on using SAML 1.1( LDAP &

Need help in log in and log out?
hi there all of u. i have recently created a site for someone. i have placed log in and log out and

Results from Db outputted twice
Hey guys.. for come reason my data is outputted twise shown in the image below and i cant figure out

Need help making login verification cleaner...
I use the below to verify user login. I first check the cookie for the id, ip and a unique id. If it

phpmailer class & pop.gmail.com?
Code: <?php
$mail->IsSMTP();
$mail->Host = "pop.gmail.com";

login box shows up again after logging in 2nd login works
Hello,

I am having trouble on my site Lockerz.rack111.com where when you login the login box

Struct/union and scope problem!
HI all , I have
Code: in header.h typedef struct Node Link; /* ---------

How do I get the row number from from an sql table query
Hi all

I have a table that I query and it returns a number of rows.
mysql_num_rows($query)

losing variables between php brackets
Hi

have got this code:
Code: $id=mysql_result($result,0,"itemid");
$title=mys

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash