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

function to return url
Hi,

Can you guys help me giving a function that takes all the contents in a particular field

Javascript or not?
How many people prefer javascript/ajax sites? How many prefer the good old fashion straight php sit

Reduce redundancies in switch functions?
Hello all! I somewhat new to PHP, and was wondering if anyone could give some suggestions on a swit

Loop Through Date Range
Hi guys,
I have date range as parameter like 01/JAN/2009 TO 16/JAN/2009 now i want to loop thro

How to calculate days from variable date?
This will be easy for one of you gurus. I want to fetch the date from a variable date, for example:<

Extracting URL pointer within XML tag
Hi.

I'm trying to extract text between two quotation marks in XML. For example, I want to ex

form problem
Hi all, I think this is going to be easy to resolve but for I have been looking at it to long and I

gather checkbox data from form into email
Hello all,

First, i'd like to thank everyone who responded to my previous posts regarding set

upload only text files
hi guys i want to upload only text like like (pdf,note pad and ofiice files) so can any one please t

why does my session end?
my connect.php starts the session just so you know
i can navigate arround my site fine except whe

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