LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2006, 05:54 PM   #1
nickraj
LQ Newbie
 
Registered: Sep 2006
Location: usa
Posts: 25

Rep: Reputation: 15
socket programming


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.

nick
 
Old 09-01-2006, 06:43 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You need to #include <sys/socket.h>
 
Old 09-01-2006, 08:52 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
If you forgot the #include you should receive
Code:
implicit declaration of function `socket'
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
 
Old 09-01-2006, 09:55 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
On Solaris you also need to add -lresolv if you use any of the gethostby*() functions.
 
Old 09-02-2006, 12:08 PM   #5
nickraj
LQ Newbie
 
Registered: Sep 2006
Location: usa
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by randyding
If you forgot the #include you should receive
Code:
implicit declaration of function `socket'
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 ?
 
Old 09-02-2006, 12:45 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Byte order.

http://en.wikipedia.org/wiki/Little_endian

Last edited by paulsm4; 09-02-2006 at 12:49 PM.
 
Old 09-30-2006, 01:07 PM   #7
nickraj
LQ Newbie
 
Registered: Sep 2006
Location: usa
Posts: 25

Original Poster
Rep: Reputation: 15
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.

Server.c

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/select.h>
#include<netinet/in.h>

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 */

if(argc!=3)
{
printf("SYNTAX ERROR !!\n " );
printf("Syntax :<Ip Address> <PORT>\n");
exit(0);
}

if( (fd=socket(AF_INET,SOCK_STREAM,0))==-1 )
{perror("error creating socket");
exit(-1);
}
server.sin_family=AF_INET;
server.sin_port=htons(atoi(argv[2]));
server.sin_addr.s_addr=htonl(INADDR_ANY);
bzero( &(server.sin_zero), 8);

if( (bind(fd,(struct sockaddr*)&server, sizeof(server)) )<0 )
{perror("error in bind ");
exit(-1);
}

/* 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
{

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);


}
}


}


}

for( i =0;i<=maxfd;i++)
{
printf("in for loop" );
if(FD_ISSET(soc[i],&readset)>0)
{
printf("hello in read ");
n=read(fd1,buf,51);
printf("%d",n );
if(write(fd1,buf,51)<0)
perror("error in writing");

if(n<0)
perror("error in read ");

if(n==0)
{
close(fd1);
FD_CLR(fd1,&readset);
}

if((strcmp(buf,"quit")==0) )
{
int j =0 ;
j=write(fd1,buf,51);
if(j<0) perror("error in send");
nclient[i]=-1;
close(fd1);
FD_CLR(fd1,&readset);

}

}
}

} // Main Class ends
 
Old 09-30-2006, 01:49 PM   #8
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
I didn't look at your code, but the reason select() is used is to avoid threading, generally.

Its worth while getting a book on socket programming since its so complicated. At the very least take a look at "man select_tut".
 
Old 09-30-2006, 02:26 PM   #9
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
why you need to add the compile options
'-lnsl -lsocket' under solaris but not under linux?
 
Old 09-30-2006, 02:32 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Because that's how Solaris is.

Anyway, if you actually want someone to read your code, please put [code] tags on it to preserve indentation.
 
Old 10-02-2006, 12:23 AM   #11
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 10-06-2006, 06:36 AM   #12
nickraj
LQ Newbie
 
Registered: Sep 2006
Location: usa
Posts: 25

Original Poster
Rep: Reputation: 15
WEll this is the same program with select() system call
can you tell me the problem . The server must be multithreaded echo server.
server.c
Code:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/select.h>

int main(int argc , char *argv[])
 {
        int fd,fd1;
        int maxfd;
        int i;
        int nclient[5];
        char buf[50];
        struct sockaddr_in server , client ;
        fd_set rset;
        fd=socket(AF_INET,SOCK_STREAM,0);
        server.sin_family=AF_INET;
        server.sin_port=htons(atoi(argv[1]));
        server.sin_addr.s_addr=htonl(INADDR_ANY);
        bzero(&(server.sin_zero),8);
        bind(fd,(struct sockaddr*)&server,sizeof(server));
        listen(fd,5);
        for(i=0;i<5;i++)
        {
                nclient[i]=-1;
        }
        for(;;)
        {
                FD_ZERO(&rset);
                FD_SET(fd,&rset);
                for(i=0;i<5;i++)
                {
                        if(nclient[i]>0)
                        {
                                FD_SET(nclient[i],&rset);
                        }
                        printf("\nclient[%d]=%d\n",i,nclient[i]);
                }
        for(;;)
        {
                FD_ZERO(&rset);
                FD_SET(fd,&rset);
                for(i=0;i<5;i++)
                {
                        if(nclient[i]>0)
                        {
                                FD_SET(nclient[i],&rset);
                        }
                        printf("\nclient[%d]=%d\n",i,nclient[i]);
                }
                select(maxfd+1,&rset,NULL,NULL,0);
                if(FD_ISSET(fd,&rset)>0)
                {
                        fd1=accept(fd,0,0);
                        printf("fd id =%d\n",fd1);
                        if(fd1>maxfd)maxfd=fd1;
                        FD_SET(fd1,&rset);
                        for(i=0;i<5;i++)
                        {
                                if(nclient[i]<0)
                                {
                                        nclient[i]=fd1;
                                        break;
                                }
                        }

                }
                if(FD_ISSET(fd1,&rset)>0)
                {
                        read(fd1,buf,sizeof(buf));
                        write(fd1,buf,sizeof(buf));
                        printf("msg from client=%s",buf);
                }
        }
 }


and the client is 
Code:

#include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<netdb.h> #include<unistd.h> int main (int argc , char*argv[]) { struct sockaddr_in rserver; struct hostent *he; int fd ; char buf[50]; fd_set rset; fd = socket(AF_INET,SOCK_STREAM,0); he = gethostbyname(argv[1]); rserver.sin_family=AF_INET; rserver.sin_port=htons( atoi ( argv[2]) ) ; bzero(&(rserver.sin_zero), 8); memcpy((unsigned char *) &(rserver.sin_addr) , (unsigned char *)he->h_addr , he ->h_length); connect(fd,(struct sockaddr *)&rserver , sizeof(rserver)); while(1) { FD_ZERO(&rset); FD_SET(fd,&rset); FD_SET(0,&rset); select(100,&rset,NULL,NULL,0); if(FD_ISSET(0,&rset)) { read(0,buf,sizeof(buf)); write(fd,buf,sizeof(buf)); } if(FD_ISSET(fd,&rset)) { read(fd,buf,sizeof(buf)); printf("from server=%s",buf); } } close(fd); }
 
Old 10-06-2006, 06:53 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 10-07-2006, 02:17 AM   #14
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 10-07-2006, 11:09 AM   #15
nickraj
LQ Newbie
 
Registered: Sep 2006
Location: usa
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Wim Sturkenboom
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 .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C socket programming Help! qinglau Programming 5 05-03-2006 09:26 AM
Socket Programming jawadhashmi Programming 4 05-24-2005 03:04 AM
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 04:50 PM
Socket programming maldini1010 Programming 4 02-11-2005 11:16 PM
Socket Programming cxel91a Programming 4 03-19-2003 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration