0

i am trying to make a program where i have a server with multiple clients, where a client sends a message to the server and when the server replies to it, the server broadcasts the reply to all the clients.
the code has some error and is not working.
i turned off my firewall. it is still not helping.
the code for my program is:

server:

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h>  /* for sockaddr_in */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include<sys/types.h>
#include<netdb.h>
#include<netinet/in.h>


void DieWithError(char *errorMessage);  /* External error handling function */
void DieWithError(char *errorMessage)
{
    perror(errorMessage);
    exit(1);
}

int main(int argc, char *argv[])
{
        int sock;                         /* Socket */
    int s,n;
        struct sockaddr_in broadcastAddr,client,server; /* Broadcast address */
        char *broadcastIP,b1[100];                /* IP broadcast address */
        unsigned short broadcastPort;     /* Server port */
        char *sendString;                 /* String to broadcast */
        int broadcastPermission;          /* Socket opt to set permission to broadcast */
        unsigned int sendStringLen;       /* Length of string to broadcast */

        if (argc < 4)                     /* Test for correct number of parameters */
        {
            fprintf(stderr,"Usage:  %s <IP Address> <Port> <Send String>\n", argv[0]);
            exit(1);
        }

        broadcastIP = argv[1];            /* First arg:  broadcast IP address */
        broadcastPort = atoi(argv[2]);    /* Second arg:  broadcast port */

        sendString = argv[3];             /* Third arg:  string to broadcast */

    /* Create socket for sending/receiving datagrams */
        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
            DieWithError("socket() failed");

    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
        server.sin_port=2000;
        server.sin_addr.s_addr=inet_addr("127.0.0.1");

        bind(s,(struct sockaddr *)&server,sizeof(server));
        printf("\nServer ready,waiting for client....\n");


    /* Set socket to allow broadcast */
        broadcastPermission = 1;
        if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0)
            DieWithError("setsockopt() failed");

    /* Construct local address structure */
        memset(&broadcastAddr, 0, sizeof(broadcastAddr));   /* Zero out structure */
        broadcastAddr.sin_family = AF_INET;                 /* Internet address family */
        broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);/* Broadcast IP address */
        broadcastAddr.sin_port = htons(broadcastPort);         /* Broadcast port */
    int conn;
        while(1)
        {    unsigned int size = sizeof (client);
        printf("About recv a  datagram\n");
        n=sizeof(client);
        recvfrom(s,b1,sizeof(b1),0,(struct sockaddr *) &client,&n);
        conn=accept(s, (struct sockaddr *) &client, &size);
        read(conn, b1, sizeof(b1) - 1);
            printf("the client sent:: %s \n",b1);

        printf("the code after displaying client side!\n\n");
        printf("ur brooadcast msg:: ");
        scanf("%s",sendString);
            sendStringLen = strlen(sendString);  /* Find length of sendString */
            printf("the expected string length is %d", sendStringLen);


         /* Broadcast sendString in datagram to clients every 3 seconds*/
                sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)&broadcastAddr, sizeof(broadcastAddr));
           //  DieWithError("sendto() sent a different number of bytes than expected ");


                sleep(3);   /* Avoids flooding the network */

     }
    /* NOT REACHED */
}

the code for client is:

#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include<sys/types.h>
#include<netdb.h>
#include<netinet/in.h>

#define MAXRECVSTRING 255 /* Longest string to receive */

void DieWithError(char *errorMessage); /* External error handling function */

void DieWithError(char *errorMessage)
{
    perror(errorMessage);
    exit(1);
}

int main(int argc, char *argv[])
{
    int sock; /* Socket */
    int s,n;
    char b2[100];
    struct sockaddr_in broadcastAddr,client,server; /* Broadcast Address */
    unsigned short broadcastPort; /* Port */
    char recvString[MAXRECVSTRING+1]; /* Buffer for received string */
    int recvStringLen; /* Length of received string */

    if (argc != 2) /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Broadcast Port>\n", argv[0]);
        exit(1);
    }

    broadcastPort = atoi(argv[1]); /* First arg: broadcast port */

    /* Create a best-effort datagram socket using UDP */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    DieWithError("socket() failed");

    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
        server.sin_port=2000;
        server.sin_addr.s_addr=inet_addr("127.0.0.1");

    /* Construct bind structure */
    memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */
    broadcastAddr.sin_family = AF_INET; /* Internet address family */
    broadcastAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */

    /* Bind to the broadcast port */
    if (bind(sock, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr)) < 0)
    DieWithError("bind() failed");

    while(1)
    {
        n=sizeof(client);
        printf("\nenter what u wanna send\n");
        fflush(stdin);
        fgets(b2, 20, stdin);

        printf("the entered string is %s", b2);
        sendto(s,b2,strlen(b2),0,(struct sockaddr *) &client,n);

            printf("message sent\n");   

        /* Receive a single datagram from the server */
        if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0)
        DieWithError("recvfrom() failed");

        recvString[recvStringLen] = '\0';
        printf("Received: %s\n", recvString); /* Print the received string */


    }
    close(sock);
    exit(0);
}

kindly help me with making this code work.
thanks a lot.

3
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by np complete
0

Some one basically had the same question in this forum 1 week ago. Search the forum 1st, as it might already has answer to your question. Anyway here is the link to it.

0

and beej's guide to network programming is the standard for linux sys, if you follow that you should be good.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.