Socket Problem............

C

Clement

Please help me.......
I am getting blocked in bind() system call.......

i don't know why

can you please any one tell me why........


#include<stdio.h>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>
#define SOCK_PATH "mysock1"
void error(char *);
main()
{
printf("OK");
int sockfd,newsockfd,serverlen,clilen,n;
struct sockaddr_un cli_addr,serv_addr;
char buf[80];
printf("Ok");
if((sockfd=socket(AF_UNIX,SOCK_STREAM,0))<0)
perror("creating socket");
serv_addr.sun_family=AF_UNIX;
printf("OK");
unlink(SOCK_PATH);
strcpy(serv_addr.sun_path,SOCK_PATH);
if(bind(sockfd,(struct sockaddr
*)&serv_addr,sizeof(serv_addr))<0)
perror("binding socket");
listen(sockfd,1);
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr
*)&cli_addr,&clilen);
printf("Ok\n");
if(newsockfd<0)
error("accepting");
for(;;)
{
printf("Waiting for Client......\n");
read(newsockfd,buf,sizeof(buf));
printf("**CLIENT** :%s",buf);
if(strcmp("end\n",buf)==0)
{
printf("Disconnecting\n");
exit(0);
}
printf("**SERVER** :");
fgets(buf,sizeof(buf),stdin);
write(newsockfd,buf,sizeof(buf));
}
}
void error(char *msg)
{
perror(msg);
exit(0);
}
 
S

santosh

Clement said:
Please help me.......
I am getting blocked in bind() system call.......

i don't know why

can you please any one tell me why........

<snip>

Since bind() is not defined by ISO C, you should try a group specific to
your system, most likely <comp.unix.programmer>
 
C

CBFalconer

Clement said:
I am getting blocked in bind() system call.......
i don't know why
can you please any one tell me why........

#include<stdio.h>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>

So far the only thing mentioned that exists in standard C is
<stdio.h>. Try a newsgroup that deals with your particular
system.
 
H

husterk

Please help me.......
I am getting blocked in bind() system call.......

i don't know why

can you please any one tell me why........

#include<stdio.h>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>
#define SOCK_PATH "mysock1"
void error(char *);
main()
{
printf("OK");
int sockfd,newsockfd,serverlen,clilen,n;
struct sockaddr_un cli_addr,serv_addr;
char buf[80];
printf("Ok");
if((sockfd=socket(AF_UNIX,SOCK_STREAM,0))<0)
perror("creating socket");
serv_addr.sun_family=AF_UNIX;
printf("OK");
unlink(SOCK_PATH);
strcpy(serv_addr.sun_path,SOCK_PATH);
if(bind(sockfd,(struct sockaddr
*)&serv_addr,sizeof(serv_addr))<0)
perror("binding socket");
listen(sockfd,1);
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr
*)&cli_addr,&clilen);
printf("Ok\n");
if(newsockfd<0)
error("accepting");
for(;;)
{
printf("Waiting for Client......\n");
read(newsockfd,buf,sizeof(buf));
printf("**CLIENT** :%s",buf);
if(strcmp("end\n",buf)==0)
{
printf("Disconnecting\n");
exit(0);
}
printf("**SERVER** :");
fgets(buf,sizeof(buf),stdin);
write(newsockfd,buf,sizeof(buf));
}}

void error(char *msg)
{
perror(msg);
exit(0);



}- Hide quoted text -

- Show quoted text -

Try configuring your socket for non-blocking (i.e. asynchronous)
operation prior to calling the bind function. This will allow you to
place a timeout on the bind() call which will return some type of
error code instead of just hanging your system. You can then review
this error code to determine what the problem is. Don't forget to
change the socket back to blocking.

Keith
http://www.doubleblackdesign.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top