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"

Did you know?Explore Trending and Topic pages for more stories like this.
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

PHP5 - Verifying a secure mail is secure
I need to send an e-mail from a form to a external department and because it contains personal custo

unoconv doc convert to pdf code prob
PHP/5.3.1

Hi. I am trying to use this code to convert docs to .pdf utilizing unoconv. Howe

Simple code help with redirects
I am trying to figure out a redirect script that detects if the browser is IE 7 and if it does, redi

problems with script
I made a small script wich exchanges points in my website:


<?php
session_start

Web Host List
Here is a list of web hosts that offer PHP and MySQL. It is no where near a full list, but it is mos

Dynamic Data + Sql Server 2005 Enterprise?
Hi! I have just started to learn ASP.NET, and it looks like it is quite a lot to learn. Im not reall

Help with usergroups? prolly really quick
So were coding our own forums for a game system we made an we have the users level setup but now I n

what business processes?
Hi,

A general question. What business processes (like OTC - Order to Cach, P2P, R2R etc)

md5 is it unique
I know this is most likely one of many simple questions that can be found semi easily. But I'm press

Loop column after 2 results
Hello All.

Here is what I have:

Client ID Company name Clien

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