Socket Program in C

Socket Program in C

Post by Jingya » Wed, 21 Jun 2000 04:00:00



Hi,
I am new on UNIX and Socket programming and tried to run a sample code. when
I compiled server.c(below), it is OK, but when I linked it. I got
below error message:
% gcc -o server server.o
Undefined                       first referenced
 symbol                             in file
socket                              server.o
accept                              server.o
bind                                server.o
getservbyname                       server.o
listen                              server.o
ld: fatal: Symbol referencing errors. No output written to server
collect2: ld returned 1 exit status
I compiled server.c on SunOS 5.6 and 5.7. How should I do to pass the
link(gcc -o...)?

Thank you every much!

Jingyan

/*********** server.c ****************
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>

main(argc, argv)
int argc;
char *argv[];
{
 struct servent *servp;
 struct sockaddr_in server, remote;
 int request_sock, new_sock;
 int nfound, fd, maxfd, bytesread, addrlen;
 fd_set rmask,mask;
 static struct timeval timeout = {0,500000}; /* one half second */
 char buf[BUFSIZ];

 if (argc!=2) {
  (void) fprintf(stderr, "usage: %s service\n", argv[0] );
  exit(1);
 }
 if ((request_sock = socket(AF_INET, SOCK_STREAM, 0))<0) {
  perror("socket");
  exit(1);
 }
 if(isdigit(argv[1][0])) {
  static struct servent s;
  servp=&s;
  s.s_port=htons((u_short)atoi(argv[1]));
 }else if ((servp=getservbyname(argv[1],"tcp")) == 0) {
  fprintf(stderr,"%s: unknow service\n");
  exit(1);
 }
 bzero((void *) &server, sizeof server);
 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=servp->s_port;//htons(13)

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

 if (listen(request_sock,SOMAXCONN)<0 ){
  perror("listen");
  exit(1);
 }

 FD_ZERO(&mask);
 FD_SET(request_sock, &mask);
 maxfd=request_sock;
 for(;;){
  rmask=mask;
  nfound=select(maxfd+1,&mask, (fd_set *)0, (fd_set *)0, &timeout);
  if(nfound<0) {
   if (errno==EINTR){
    printf("interrupted system call\n");
    continue;
   }
   perror("select");
   exit(1);
  }
  if(nfound==0) {
   printf("."); fflush(stdout);
   continue;
  }
  if(FD_ISSET(request_sock, &mask)){
   addrlen=sizeof(remote);
   new_sock=accept(request_sock, (struct sockaddr *)&remote, &addrlen);
   if(new_sock<0) {
    perror("accept");
    exit(1);
   }
   FD_SET(new_sock, &mask);
   if(new_sock>maxfd)
    maxfd=new_sock;
   FD_CLR( request_sock, &mask);
  }
  for (fd=0;fd<=maxfd;fd++) {

   if(FD_ISSET(fd, &mask)){

    bytesread=read(fd, buf, sizeof buf-1);

    if(bytesread<0){
    perror("read");
   }
   if(bytesread<=0){

    FD_CLR(fd, &mask);
    if (close(fd)) perror("close");
    continue;
   }
   buf[bytesread]='\0';
   printf("%s: %d ",argv[0],bytesread,fd,buf);
   }
  }
 }

Quote:};

 
 
 

Socket Program in C

Post by Mohd Idaha » Thu, 22 Jun 2000 04:00:00


Try to compile with
gcc -o server server.o -lsocket

this will include libsocket.a at /usr/lib/.


> Hi,
> I am new on UNIX and Socket programming and tried to run a sample code. when
> I compiled server.c(below), it is OK, but when I linked it. I got
> below error message:
> % gcc -o server server.o
> Undefined                       first referenced
>  symbol                             in file
> socket                              server.o
> accept                              server.o
> bind                                server.o
> getservbyname                       server.o
> listen                              server.o
> ld: fatal: Symbol referencing errors. No output written to server
> collect2: ld returned 1 exit status
> I compiled server.c on SunOS 5.6 and 5.7. How should I do to pass the
> link(gcc -o...)?

> Thank you every much!

> Jingyan


 
 
 

Socket Program in C

Post by Kenneth P. Turv » Thu, 22 Jun 2000 04:00:00



>Hi,
>I am new on UNIX and Socket programming and tried to run a sample code. when
>I compiled server.c(below), it is OK, but when I linked it. I got
>below error message:
>% gcc -o server server.o
>Undefined                       first referenced
> symbol                             in file
>socket                              server.o
>accept                              server.o
>bind                                server.o
>getservbyname                       server.o
>listen                              server.o
>ld: fatal: Symbol referencing errors. No output written to server
>collect2: ld returned 1 exit status
>I compiled server.c on SunOS 5.6 and 5.7. How should I do to pass the
>link(gcc -o...)?

You aren't linking in all the libraries you need.  Which ones you need
depend on your OS and I don't know about SunOS.  Try doing this:

% gcc -o server server.o -lnet

This says to link in the library libnet.a   .. it might be what you
need.  Otherwise look at your documentation and find out what library
Sun put the functions: socket, accept, bind, getserverbyname, and listen
in.  

--

  http://www.tranquility.net/~kturvey/resume/resume.html
--------------------------------------------------------
  It would be an absurdity for jurors to be required to accept the judge's
  view of the law, against their own opinion, judgment, and conscience.
        -- John Adams

 
 
 

Socket Program in C

Post by Ian Collin » Thu, 22 Jun 2000 04:00:00


Solaris requires -lsocket -lnsl

    Ian


> Hi,
> I am new on UNIX and Socket programming and tried to run a sample code. when
> I compiled server.c(below), it is OK, but when I linked it. I got
> below error message:
> % gcc -o server server.o
> Undefined                       first referenced
>  symbol                             in file
> socket                              server.o
> accept                              server.o
> bind                                server.o
> getservbyname                       server.o
> listen                              server.o
> ld: fatal: Symbol referencing errors. No output written to server
> collect2: ld returned 1 exit status
> I compiled server.c on SunOS 5.6 and 5.7. How should I do to pass the
> link(gcc -o...)?

> Thank you every much!

> Jingyan

> /*********** server.c ****************
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <sys/time.h>
> #include <netinet/in.h>
> #include <netdb.h>
> #include <arpa/inet.h>
> #include <ctype.h>
> #include <errno.h>
> #include <stdio.h>

> main(argc, argv)
> int argc;
> char *argv[];
> {
>  struct servent *servp;
>  struct sockaddr_in server, remote;
>  int request_sock, new_sock;
>  int nfound, fd, maxfd, bytesread, addrlen;
>  fd_set rmask,mask;
>  static struct timeval timeout = {0,500000}; /* one half second */
>  char buf[BUFSIZ];

>  if (argc!=2) {
>   (void) fprintf(stderr, "usage: %s service\n", argv[0] );
>   exit(1);
>  }
>  if ((request_sock = socket(AF_INET, SOCK_STREAM, 0))<0) {
>   perror("socket");
>   exit(1);
>  }
>  if(isdigit(argv[1][0])) {
>   static struct servent s;
>   servp=&s;
>   s.s_port=htons((u_short)atoi(argv[1]));
>  }else if ((servp=getservbyname(argv[1],"tcp")) == 0) {
>   fprintf(stderr,"%s: unknow service\n");
>   exit(1);
>  }
>  bzero((void *) &server, sizeof server);
>  server.sin_family=AF_INET;
>  server.sin_addr.s_addr=INADDR_ANY;
>  server.sin_port=servp->s_port;//htons(13)

>  if (bind(request_sock,(struct sockaddr *) &server, sizeof server )<0) {
>   perror("bind");
>   exit(1);
>  }

>  if (listen(request_sock,SOMAXCONN)<0 ){
>   perror("listen");
>   exit(1);
>  }

>  FD_ZERO(&mask);
>  FD_SET(request_sock, &mask);
>  maxfd=request_sock;
>  for(;;){
>   rmask=mask;
>   nfound=select(maxfd+1,&mask, (fd_set *)0, (fd_set *)0, &timeout);
>   if(nfound<0) {
>    if (errno==EINTR){
>     printf("interrupted system call\n");
>     continue;
>    }
>    perror("select");
>    exit(1);
>   }
>   if(nfound==0) {
>    printf("."); fflush(stdout);
>    continue;
>   }
>   if(FD_ISSET(request_sock, &mask)){
>    addrlen=sizeof(remote);
>    new_sock=accept(request_sock, (struct sockaddr *)&remote, &addrlen);
>    if(new_sock<0) {
>     perror("accept");
>     exit(1);
>    }
>    FD_SET(new_sock, &mask);
>    if(new_sock>maxfd)
>     maxfd=new_sock;
>    FD_CLR( request_sock, &mask);
>   }
>   for (fd=0;fd<=maxfd;fd++) {

>    if(FD_ISSET(fd, &mask)){

>     bytesread=read(fd, buf, sizeof buf-1);

>     if(bytesread<0){
>     perror("read");
>    }
>    if(bytesread<=0){

>     FD_CLR(fd, &mask);
>     if (close(fd)) perror("close");
>     continue;
>    }
>    buf[bytesread]='\0';
>    printf("%s: %d ",argv[0],bytesread,fd,buf);
>    }
>   }
>  }
> };

 
 
 

1. socket timeout? (socket programming question)

I have a process that opens a TCP server socket via socket/bind/listen.
Sometimes the process dies unexpectedly. When I try to start another
process to bind to the same port, I get EADDRINUSE returned from bind;
the socket is already in use, even though the other process that had
it open is dead. After about 30 seconds, though, I can bind to that
port again.

I know almost all Unix kernels do this, but why? Is there some sort of
garbage collection process in the kernel that occasionally checks all
sockets that are marked in use to check that they really are?

What should I be doing in my own server process to ensure that it
closes the socket properly so Linux doesn't think the socket is in use?

Thanks
  Nelson

2. Konqueror 2.1b2 accepting cookies?

3. Anyone do and socket programming and modem programming on AIX?

4. Can't ping WFW or NT to Linux..

5. Sockets sockets and more sockets

6. concatenate MPEG files?

7. Socket, Socket, whose got the socket?

8. linux firewalling--HELP!!

9. Socket, socket, who's got the socket?

10. Sockets sockets and more sockets

11. socket programming question.

12. Socket programming: difference between Unix and Windows

13. socket programming miracles