简体   繁体   中英

Echo Server Client using UDP Sockets in C

The following program has the motive of sending a data gram from client to the server and the data gram being echoed back to the client as an acknowledgment.

I have tried to terminate the program when an empty string is accepted from the server side. I executed the program in Fedora 14. The error I encounter is that there is an "Error in SendTo"- Socket on Non-Socket.

I am unable to figure out the mistake. Please do point out the mistake. Suggestions are welcome.

I have tried reading other resources regarding this, but have not been able to get a hold out of it.

Thanks in advance for your answer.

Server:

#include<stdio.h> 
#include<sys/types.h> 
#include<netinet/in.h>
#include<sys/socket.h> 
#include<string.h> 
#include<unistd.h> 
#include<stdlib.h>
#define PORT 7844

int main(int argc,char *argv[]) {
    char buf[2000];
    int sockfd,len,a;
    struct sockaddr_in servaddr,cliaddr;
    if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        printf("Error creating socket\n"); 
        exit(0);
    }
    printf("UDP Server Socket Created Successfully.\n"); 
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(PORT); 
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY); 
    if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
    {
        perror("Error binding socket."); 
        exit(0);
    }
    printf("UDP Server Socket Binded.\n"); 
    len=sizeof(cliaddr);
    do {
        strcpy(buf," "); 
        if((a=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&cliaddr,&len))<0){
            perror("ERROR"); 
            exit(0);
        }
        printf("From Client : %s",buf); 
        if((sendto(sockfd,buf,a,0,(struct sockaddr *)&cliaddr,len))<0)
        {
            perror("NOTHING SENT"); 
            exit(0);
        }
        printf("Server : ");
        fgets(buf,sizeof(buf),stdin);
        if((sendto(sockfd,buf,a,0,(struct sockaddr *)&cliaddr,len))<0)
        {
            perror("NOTHING SENT"); 
            exit(0);
        }
    } while(strcmp(buf," ")!=0); 
    close(sockfd); 
    return 0;
}

CLIENT

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

int main(int argc,char *argv[]) {
    int sockfd,len;
    struct sockaddr_in serv,cliaddr;
    char buff[2000]; 
    if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        perror("ERROR creating socket");
        exit(0);
    }
    printf("UDP Client Socket Created Successfully.\n"); 
    memset(&serv,0,sizeof(serv)); 
    serv.sin_family=AF_INET; 
    serv.sin_port=htons(PORT);
    serv.sin_addr.s_addr=inet_addr(argv[1]);
    do
    {
        printf("Client : ");
        fgets(buff,sizeof(buff),stdin);
        if((sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&serv,sizeof(serv)))<0) {
            perror("ERROR IN SENDTO");
        }
        if(strcmp(buff," ")==0) {
            exit(0);
        } 
        strcpy(buff," ");
        if((recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&cliaddr,&len))<0) {
            perror("ERROR IN RECVFROM"); 
            exit(0);
        }
        fputs("From Server : ",stdout); 
        fputs(buff,stdout); 

    } while(strcmp(buff," ")!=0);
    close(sockfd);
    return 0;
}

Please let me know if I can terminate a program using empty string. Also let me know of flaws in the program. Thank you, once again.

You are using fgets

From man fgets

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.

So when you enter a space and press return, you will get " \n" not the " " you are testing for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM