🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #53679

gcc blues

#53679
From: Steve Berson
Date: Tue, 06 Aug 1991 02:13
116 lines
3690 bytes
I am having the strangest problem with gcc on our various
sun 3 and sun 4 networks around the department here.  I
have tried gcc versions 1.35, 1.37.1, and 1.39 and I have
had the same problem.  I am trying to broadcast a message
on a local network and have some code to do this.  I have
also included a program to monitor broadcasts.  When I
compile this code using cc, everything works fine; the
receiver receives the message.  When I compile the
transmitting program using gcc, the receiver does not
receive the message.  Has anyone else had this same problem?
What can be done?

Please respond directly to me as I do not usually read this
group.

Steven Berson
steven@cs.ucla.edu

P.S.  The code for broadcast and then for receive follow.  Run
      the receive code on one machine and then the broadcast
      code on another machine (on the same network of course).
      The receive code should print out the received string.

P.P.S.  Remember to change the getnetbyname call in the broadcast
        code to reflect your specific network configuration

--------------------------------------------------------------------------

/*  Here is the broadcast code */

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

main()
{
    char buf[1024];
    int sock2, bytes, length2;
    struct sockaddr_in dest;
    struct in_addr inet_makeaddr();
    struct netent *net;

    sock2 = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock2 < 0) {
	perror("opening broadcast socket");
	exit(1);
    }

    length2 = sizeof(dest);

    if (getsockname(sock2, &dest, &length2) < 0)
       return(-1);

    if ((net = getnetbyname("cs-wsr")) == NULL)
       perror("getnetbyname failed");

    dest.sin_addr = inet_makeaddr(net->n_net, INADDR_ANY);
    dest.sin_family = AF_INET;
    dest.sin_port = 3456;

    printf("Broadcasting on port #%d\n", ntohs(dest.sin_port));
    strcpy(buf, "this is a string");
    bytes = sendto(sock2, buf, strlen(buf)+1, 0, &dest, sizeof(dest));
    printf("broadcasted %d bytes.\n", bytes);

    close(sock2);
    exit(0);

}


--------------------------------------------------------------------------

/* Here is the receive code */

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

main()
{
    int sock, length;
    struct sockaddr_in name;
    char buf[1024];

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
	perror("opening datagram socket");
	exit(1);
    }

    name.sin_family = AF_INET;
    name.sin_addr.s_addr = INADDR_ANY;
    name.sin_port = 3456;
    if (bind(sock, (struct sockaddr *)&name, sizeof name) < 0) {
	perror("binding datagram socket");
	exit(1);
    }

    printf("Socket port #%d\n", ntohs(name.sin_port));

    while (1)
    {
     if (read(sock, buf, 1024) < 0)
 	perror("receiving datagram packet");
  
     printf("-->%s\n", buf);
    }

    close(sock);
    exit(0);
}
Technical Details
Message-ID: <1991Aug6.021317.23098@cs.ucla.edu>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!zaphod.mps.ohio-state.edu!usc!ucla-cs!maui.cs.ucla.edu!steven

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,f70862d0ba730950,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1991-08-06 01:36:34 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!zaphod.mps.ohio-state.edu!usc!ucla-cs!maui.cs.ucla.edu!steven
From: steven@cs.ucla.edu (Steve Berson)
Newsgroups: gnu.gcc.help
Subject: gcc blues
Message-ID: <1991Aug6.021317.23098@cs.ucla.edu>
Date: 6 Aug 91 02:13:17 GMT
Sender: usenet@cs.ucla.edu (Mr. News Himself)
Organization: UCLA Computer Science Department
Lines: 116
Originator: steven@maui.cs.ucla.edu
Nntp-Posting-Host: maui.cs.ucla.edu