problems for TCP/IP read

New Message Reply Date view Thread view Subject view Author view

Sam Chu (c00chu00++at++nchc.gov.tw)
Sat, 9 Aug 1997 15:29:03 +0800 (CST)


Hi performers:

  thought this is a communicatin problem, I think lots of simulation
progrmmers already have this question and may know how to solve it.

I try to do the inter-Chassis communication and use the
socket communication codes from examples - "accept and
connect" under directory "/usr/people/4Dgifts/examples/network"
but the "read" command not always read the size which I specify
in "read(sock, data, size)". Is there any way I can ask "read" always
to read size of data?

Thanks for any suggestion.

the following is my code modified from accept.c (performer site)

{
    netDataType *netData;
    ....

    /* Create a socket */
    if ((sock = socket (AF_INET,SOCK_STREAM,0)) < 0) {
        perror("opening stream socket");
        exit(1);
    }

    /* Initialize the socket's address structure */
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(SERVER_PORT);

    /* Assign an address to this socket */
    if (bind (sock,&sin,sizeof(sin)) < 0) {
        perror("binding stream socket");
        exit(1);
    }
    printf("Listening for connect requests on port %d\n", SERVER_PORT);

    /* Prepare the socket queue for connection requests */
    listen(sock,5);

    length = sizeof(sin);
    msgsock = accept(sock, &sin, &length);
    if (msgsock < 0) {
        perror("accept");
        exit(1);
    }

    printf("Connection from host %s, port %u\n",
    inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));

    init_performer();

    /* Read from the message socket and write to standard output */
    while ((cnt = read(msgsock,(char *)net_Data,sizeof(netDataType)))> 0){
        x=netData->x;
        y=netData->y;
        z=netData->z;
        h=netData->h;
        p=netData->p;
        q=netData->r;
        performer_cmd();
    }
    close(msgsock);
    printf("Connection closed\n");
}

and my connect.c as following: (data site)
{
    netDataType *netData;
    .....
    if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Can't open socket");
        exit(1);
    }

    /* Initialize the socket address to the server's address. */
    bzero((char *) &sin, sizeof(sin));
    sin.sin_family = AF_INET;
    hp = gethostbyname(argv[1]); /* to get host address */
    if (hp == NULL) {
        herror(argv[1]);
        exit(1);
    }
    bcopy (hp->h_addr, &(sin.sin_addr.s_addr), hp->h_length);
    sin.sin_port = htons(SERVER_PORT);

       /* Connect to the server. */
    if (connect(sock, &sin,sizeof(sin)) < 0) {
        close(sock);
        perror("Connect to server");
        exit(1);
    }
    printf("Connection established. "\n);
    /*
     * If the server goes away while sending data, we'll get a SIGPIPE signal.
     * Catch it so we can print an error message.
     */
    (void) signal(SIGPIPE, (void *)die);

   /* the real-time loop */
    while( 1 )
    {
          wait_for_33msec(); /* 30Hz */
          update_position(&x, &y, &z, &h, &p, &r);
          netData->x =x;
          netData->y =y;
          netData->z =z;
          netData->h =h;
          netData->p =p;
          netData->r =r;
          if ((cnt= write(sock, (char *)netData, sizeof(netDataType))) < 0) {
             perror("Error writing to socket");
             exit(1);
          }
    }
}

Sam Chu
National Center for High-Performance Computing
Scientific Visualization Lab Email: c00chu00++at++nchc.gov.tw
Tel: (886)35-776085 Ext 248 Fax : (886)35-773538
=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
            Submissions: info-performer++at++sgi.com
        Admin. requests: info-performer-request++at++sgi.com


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:55:43 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.