-1

In my code, clients only send data to the server. However, I would like what client1 says to be sent to the server and then the server to relay it to client2. I have tried everything I can think of but can't figure it out.

Server

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <stdlib.h> 

int main() 
{ 
    int sock; 
    int addr_len, bytes_read; 
    char recv_data[1024]; 
    struct sockaddr_in server_addr , client_addr; 


    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 
    { 
        perror("Socket"); 
        exit(1); 
    } 

    server_addr.sin_family = AF_INET; 
    server_addr.sin_port = htons(5000); 
    server_addr.sin_addr.s_addr = INADDR_ANY; 
    bzero(&(server_addr.sin_zero),8); 


    if (bind(sock,(struct sockaddr *)&server_addr, 
    sizeof(struct sockaddr)) == -1) 
    { 
        perror("Bind"); 
        exit(1); 
    } 

    addr_len = sizeof(struct sockaddr); 

    printf("\nUDPServer Waiting for client on port 5000"); 
    fflush(stdout); 

    while (1) 
    { 

        bytes_read = recvfrom(sock,recv_data,1024,0,(struct sockaddr *)&client_addr, &addr_len); 

        recv_data[bytes_read] = '\0'; 
        if(strlen(recv_data)>1)
        {
            printf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); 
            printf("%s", recv_data); 
            fflush(stdout); 
        }

        bytes_read = sendto(sock,recv_data,1024,0,(struct sockaddr *)&client_addr, &addr_len); 
        recv_data[bytes_read] = '\0';

    } 
 return 0; 
} 

Client

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, char** argv) 
{ 
    int sock,i=0,bytes_read; 
    struct sockaddr_in server_addr; 
    struct hostent *host; 
    char send_data[1024],recv_data[1024];
    char nickname[20];
    if(argc != 2)  
    {  
        printf("Usage : %s <Server-IP>\n",argv[0]);  
        exit(0);  
    }  

    printf("Enter a nickname:");
    scanf("%s",&nickname);

    printf("WELCOME %s ..Please (Enter) to start chat or Quit(q): ",&nickname);
    host= (struct hostent *) gethostbyname((char *)argv[1]); 


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

    server_addr.sin_family = AF_INET; 
    server_addr.sin_port = htons(5000); 
    server_addr.sin_addr = inet_addr(argv[1]); //IP ADDRESS 
    bzero(&(server_addr.sin_zero),8); 


    while (1) 
    { 
        if(i>1)
        {
            printf("%s:",&nickname); 
        }
        gets(send_data); 

        if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0) 
            break; 
        else
        {
            sendto(sock, send_data, strlen(send_data), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 


        bytes_read = recvfrom(sock, send_data, strlen(send_data), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 
        recv_data[bytes_read] = '\0';
        }
    i++;
    } 
} 
savanto
  • 4,470
  • 23
  • 40
  • 2
    You have `bytes_read` which contains the number of bytes read. Why do you then call `strlen`? Also, do you realize you are sending it back out the same socket you received it from? – David Schwartz Jun 04 '14 at 21:16
  • My main problem is MULTIPLE CLIENTS with ONE SERVER. I don't send server data to client.How I can this? – user3624223 Jun 04 '14 at 21:32
  • 2
    A server is normally used to manage multiple client connection. You code is just managing a single connection. You should clarify how a server/client model works. I would suggest reading this excellent online tutorial: http://beej.us/guide/bgnet/ – sergico Jun 04 '14 at 21:34
  • I shouldn't say "out the same socket", I should say "to the very destination from which you received it". – David Schwartz Jun 04 '14 at 21:42
  • Also note that bytes_read can be 1024, so `recv_data[bytes_read] = '\0';` could write beyond the size of the buffer. – wildplasser Jun 04 '14 at 23:32

1 Answers1

0

Right now, your server is sending a received message back to the same client that sent the message. To do what you are asking for, you need to keep a list of all the clients that have sent messages to your server.

When you receive a message from an unknown client, add the client to the list. If you don't receive a new message from that client for a period of time, remove it from the list. If you need to allow a client to remain "connected" to the server for an extended period of time, the client will have to send keepalive/heartbeat messages to the server at regular intervals to avoid timing out.

Each time you receive a message that needs to be relayed, you can loop through the list sending the message to each client as needed.

For example (pseudo-code, I'll leave the implementation as an exercise for you to figure out):

int main() 
{ 
    CreateAndBindServerSocket();

    while (1) 
    { 
        if (ReadAMessage(&msg, &sender))
        {
            if (!ClientIsInList(sender))
                AddClientToListAndStartIdleTimer(sender);
            else
                RestartIdleTimerForClient(sender);

            if (ShouldRelayMessageToAClient(msg, &recip))
            {
                if (FindClientInList(recip, &recip_addr))
                {
                    SendMsgToClient(recip_addr, msg);
                }
            }
            else if (ShouldRelayMessageToAllClients(msg))
            {
                for (each recip_addr in list)
                {
                    SendMsgToClient(recip_addr, msg)
                }
            }
        }

        RemoveTimedOutClientsFromList();
    } 

    return 0; 
} 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770