question on TCP/IP code that works on SUN but not on VAX under ULTRIX

Ed Fox FOX%vtcs1.cs.vt.edu at RELAY.CS.NET
Fri Jan 16 03:06:15 AEST 1987


Hi!  Following is a message from one of my students, Sachit Apte, who
would like assistance with developing some simple code using TCP/IP
sockets in client/server form on a VAX running ULTRIX.  Please reply
directly to me since we are not on the list.  Thanks! 
  Ed Fox (fox%vt at csnet-relay.arpa or fox%vt at relay.cs.net)
PS If there is another place I should send this, please advise.
 
/**********************************************************************/
/* BRIEF DESCRIPTION OF THE PROBLEM FOLLOWS:                          */
/**********************************************************************/
This client-server code works on a SUN-2/170 called "gremlin" but 
does not work on on a VAX 11/785 called "vtopus" runing ULTRIX (4.2 Bsd Unix).
That is, if either the client or server (or both) is on vtopus, we
fail but if both are on gremlin then things work fine!  We execute
these programs by typing "server" to bring up the server and, using
another terminal or window, typing "client vtopus" or "client gremlin"
depending on whether the server is on vtopus or gremlin, respectively.
The server goes thru it's normal socket, bind, listen calls and
prints the msg: "server ready to accept" on the screen.  It then hangs.
The client always comes back with the error: 
	"client: connect: Connection refused" 
 
/**********************************************************************/
/* THE SERVER CODE FOLLOWS:                                           */
/**********************************************************************/ 
 
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h> 
#include <stdio.h>
#define STRINGLEN 80
 
extern errno;
 
main(argc,argv)
int argc;
char **argv[];
{
    int                 f; 
    struct  sockaddr_in from;
    struct  servent     *sp;
    struct servent test;
     
    sp = getservbyname("ingreslock","tcp"); 
    if (sp == NULL) {
        perror(stderr, "sockmess: tcp/sockmess: unknown service\n");
        exit(1);
    }
   from.sin_port = sp->s_port;
   f = socket(AF_INET,SOCK_STREAM,0);
   if (f < 0) {
       perror("sm_server: socket");
       exit(3);
       }
   if (bind(f, (caddr_t)&from, sizeof(from)) < 0) {
       perror("sm_server: bind");
       exit(5);
       }
   if (listen(f, 5) < 0) {
       perror("sm_server: listen");
       exit(6);
       }
  for(;;)   {
       int g, len = sizeof(from);
       
       printf("server ready to accept\n");
       g = accept(f, &from, &len); /* here is where it hangs!! */
       if (g < 0) {
           if (errno != EINTR)
               perror("sm_server: accept");
           continue;
           }  
       if (fork() == 0) {
           close(f);
           doit(g,&from);
           printf("child process exited OK\n");
           exit(0);
           }
       close(g);
       }
}
 
int doit(mysocket,sockptr)
int mysocket;
struct sockaddr_in *sockptr;
{
    struct sockaddr name;
    int namelen = sizeof(name.sa_data);
    char buf[STRINGLEN];
 
    if (getsockname(mysocket, &name, &namelen) < 0) {
        perror("sm_server: getsockname\n");
        }
    if (read(mysocket, buf, sizeof(buf)) < 0) {
        perror("sm_server: read");
        exit(8);
        }
    printf("The message from client was:- %s\n",buf);
    close(mysocket);
}
 
 
/**********************************************************************/
/* THE CLIENT CODE FOLLOWS:                                           */
/**********************************************************************/ 
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#define STRINGLEN 80
 
main(argc,argv)
char *argv[];
int argc;
{
    int                 s; 
    struct  sockaddr_in sin;
    struct  servent     *sp;
    struct  hostent     *hp;
    struct servent test;
    
    if (argc < 2) {
        printf("usage: sm_client serverhost \n");
        exit(1);
        }
    sp = getservbyname("ingreslock","tcp");
    if (sp == NULL) {
        perror("sm_client: udp/sockmess: unknown service\n");
        exit(2);
        }
   hp = gethostbyname(argv[1]);
   if (hp == NULL) {
       perror("sm_client: unknown host\n");
       printf("host %s",argv[1]);
       exit(3);
   }
   bzero((char *)&sin, sizeof(sin));
   bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
   sin.sin_family = hp->h_addrtype;
   sin.sin_port = ntohs(sp->s_port);
   s = socket(hp->h_addrtype,SOCK_STREAM,0);
   if (s < 0) {
       perror("sm_client: socket");
       exit(4);
       }
   if (connect(s, (char *)&sin, sizeof(sin)) < 0) {
       perror("client: connect"); /* here is where error occurs! */
       exit(5);
       }
   doit(s,&sin);
}      
 
 
int doit(mysocket,sockptr)
int mysocket;
struct sockaddr_in *sockptr;
{
   char buf[STRINGLEN]; 
   int  result;
 
   printf("Input string to send to server:\n> "); /* less than 80 chars */
   if (gets(buf) != NULL)
       {
     fprintf(stderr,"buffer size is = %d",sizeof(buf));
       if (write(mysocket, buf, sizeof(buf)) < 0) {
           perror("sm_client: read");
           exit(8);
           }
       }
   close(mysocket);
}



More information about the Comp.unix.questions mailing list