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

foreach result into a single variable
Hi,

I have this code...

Code: [Select]foreach ($_POST['Interests'] as $interest =&

Points for Wiki contribution
Hi everyone,

I want to know how long does it take for the moderator to award points for a

2 decima places & How to reload my page
Hello There,

How do i put full-stop (.) after second figure from behind? ie if i have 123456

PHP Tab Control
Hi All,

I would like to have PHP tab control with/without Javascript.

But I want to r

RTF fomatting to email content
Im trying to sen an email with content is picked up from a rtf-file (file_get_contents('*.rtf'). Mai

Links not updating
I'm trying to fuel a simple navigation system for a news section. It works fine when it comes to ret

Need help to identify this error please
:confused:Can anyone help me tell what this error message means? Maybe tell me where to look to fix

Retrieving innerHTML with cURL?
Hey all (sorry I know I'm a leecher, but I soon won't be. This is my first PHP project, but not my l

Help with some dates
I have a list of dates in an array:

$mondays = array(
strtotime("October 12, 2009

Oracle Connectivity
Hi Every One,

Can we access SAP from oracle database.If it possible then please spec

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