ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have just started network programming in c (unix). After compiling the programm i m getting undefined reference to `socket', undefined reference to `bind' etc. I am unable to figure out the problem , can you help.
So that may not be the problem. It sounds like a link problem.
If you are on Unix (like solaris for example), then add the following to your link options.
So that may not be the problem. It sounds like a link problem.
If you are on Unix (like solaris for example), then add the following to your link options.
Code:
-lnsl -lsocket
Thankx
can u tell me why host to netwok and n/w to host conversion are done ?
I am writting a multithreaded echo server using select() system call. When i run the program either it handles only one client. Or it handles multiple client but no message can be exchanged. Do select() automatically updates the value of readable FD (file descriptor). The following code accepts multiple connection but hangs when clint send msg. I think the problem with the program is that it only checks fd(which is the listen socket of the server) but do not check whether the client FD is readable or not.
int main(int argc , char* argv[])
{
int fd, fd1;
int x,y;
int size ;
ssize_t n;
int soc[20], sock=0;
int i=0;
int readynum=0;
int maxfd =0,max=-1 ;
int nclient[5];
char buf[21];
struct sockaddr_in server,client;
fd_set readset;
void cfd(int );
/* Validating command line arguments , Error handling for socket()method ,
Assigning the vlaues to the structure server */
/* Listening for the connections , accepting the connections using accept()and s
ending the message to the client after validating the message received from the
client */
if( listen(fd,5)<0 )
{
perror("error in listen");
exit(-1);
}
for( i =0 ;i<5;i++)
nclient[i]=-1;
maxfd=fd;
for(;
{
printf("in loop");
FD_ZERO(&readset);
FD_SET(fd,&readset);
readynum= select(maxfd+1,&readset,NULL,NULL,0);
printf("the FD reay are =%d",readynum);
if(readynum<0) perror("error in select");
else
{
Two little things that I see:
1)
You never get out of the endless for-loop (the loop that accepts connections)
2)
The loop in bold is only executed once in ANY situation; further client is not an array. I'm amazed that the code generates something that you can execute (my compiuler stops on errors). So if it does, client is probably the cause of the crash.
Code:
for(;;)
{
printf("in loop");
FD_ZERO(&readset);
FD_SET(fd,&readset);
readynum= select(maxfd+1,&readset,NULL,NULL,0);
printf("the FD reay are =%d",readynum);
if(readynum<0)
perror("error in select");
else
{
if(( FD_ISSET(fd,&readset))>0)
{
if((fd1=accept(fd,0,0) ) <0 )
perror("Error while accepting connection");
else
{
printf("connection esteblished\n");
if(fd1>maxfd)
maxfd=fd1;
for(i=0;i<=5;i++)
{
if(client[i]==-1)
client[i]=fd1;
break;
}
FD_SET(fd1,&readset);
}
}
}
} // end_of_for(;;)
// you'll never get here
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by pankaj99
why you need to add the compile options
'-lnsl -lsocket' under solaris but not under linux?
Because Linux use Glibc which includes libraries from diverse sources, including SystemV, BSD and others. As an implicit link with glibc is done by the compiler, you do not need to specify it.
Solaris libc on the other hand is strictly SystemV.
Sockets and name service libraries are separated and must be specified.
You're turning some things around:
You are supposed to tell us what in your program does not work (error messages etc) and we will try to point you in the right direction or solve it.
Having said that,
You can check the server part by using telnet; i.e. telnet localhost portnumber. Once that is working, you can concentrate on the client side.
You're turning some things around:
You are supposed to tell us what in your program does not work (error messages etc) and we will try to point you in the right direction or solve it.
Having said that,
You can check the server part by using telnet; i.e. telnet localhost portnumber. Once that is working, you can concentrate on the client side.
The problem with the above program is that , when one client connects with the server they both can communicate with each other . What ever client sends , server echo that back to the client. But when the second client connect, the first client can't send any message while second client work fine, So the problem is whenever a new client connects to the server, the previous client do no receive the echo message from the server .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.