0

Client code

  #include  <stdio.h>
#include    <stdlib.h>
/*
#include    <modes.h>
#include    <events.h>
#include    <process.h>
#include    <alarm.h>
#include    <signal.h>
#include    <module.h>
#include    <sg_codes.h>
#include    <sgstat.h>
#include    <sys/types.h>
*/
#include    <netinet/in.h>
#include    <arpa/inet.h>
#include    <sys/socket.h>
#include    <string.h>
#include    <time.h>
#define     MAX 1024
int main ( int argc, char ** argv)
{

    printf("Check point 1\n");
    if (argc != 2)
    {
        printf("Error!! Provide a correct port\n");
        exit (0);
    }


    unsigned char buff[MAX];
    int port = atoi(argv[1]);
    int sockfd;
    struct sockaddr_in myAddr, Addr;
    unsigned char temp[MAX];
    char buffer [MAX];

    socklen_t addr_size;
    addr_size = sizeof(Addr);


    //_os_getime();
    printf("Check point 2\n");
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))< 0)
    {
        printf ( "%d", sockfd);
            perror("socket failed");
            exit(EXIT_FAILURE);
    }

    memset(&myAddr,'\0', sizeof(myAddr));
    myAddr.sin_family = AF_INET;
    myAddr.sin_port = htons(port);
    myAddr.sin_addr.s_addr =inet_addr("127.0.0.1");

    printf ("Check point 3");
    strcpy(buff, "ABCDE\n");
    if((sendto(sockfd, buff, MAX, 0 , (struct sockaddr*) &myAddr, sizeof(myAddr)))<0)
    {
        perror("Sending failed--");
            exit(EXIT_FAILURE);

    }

    printf ("Check point 4");

    if((recvfrom(sockfd, buffer, sizeof(buffer), 0 , (struct sockaddr*) &Addr, &addr_size))<0)
    {
        perror("Receiving failed");
            exit(EXIT_FAILURE);
    }

    printf ( "%s", buff);


    close(sockfd);
    return 0;

}   

Server Code

#include    <stdio.h>
#include    <stdlib.h>
/*
#include    <modes.h>
#include    <events.h>
#include    <process.h>
#include    <alarm.h>
#include    <signal.h>
#include    <module.h>
#include    <sg_codes.h>
#include    <sgstat.h>
#include    <sys/types.h>
*/
#include    <netinet/in.h>
#include    <arpa/inet.h>
#include    <sys/socket.h>
#include    <string.h>
#include    <time.h>
#define     MAX 1024


int main (int argc, char ** argv)
{
    if (argc != 2)
    {
        printf("Error!! Provide a correct port\n");
        exit (0);
    }   


    int port = atoi(argv[1]);
    char buff[MAX];
    char buffer [MAX];
    int sockfd, clientfd;
    struct sockaddr_in myAddr, Addr;


    socklen_t addr_size;

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))< 0)
    {
        printf ( "%d", sockfd);
            perror("socket failed");
            exit(EXIT_FAILURE);
    }

    memset(&myAddr,'\0', sizeof(myAddr));
    myAddr.sin_family = AF_INET;
    myAddr.sin_port = htons(port);
    myAddr.sin_addr.s_addr =inet_addr("127.0.0.1");
    if ((bind (sockfd, (struct sockaddr*) &myAddr, sizeof(myAddr)))<0)
    {
        perror("Binding failed");
            exit(EXIT_FAILURE);
    }

    addr_size = sizeof(Addr);


    int recv = recvfrom(sockfd, buff, MAX, 0 , (struct sockaddr*) &Addr, &addr_size);
    printf ( "%s", buff);   

    strcpy(buffer, "EFGH");
    if((sendto(sockfd, buffer, MAX, 0 , (struct sockaddr*) &myAddr, sizeof(myAddr)))<0)
    {
        perror("Sending failed");
            exit(EXIT_FAILURE);

    }   
    close(sockfd);

    return 0;
}

I have edited my question, this time I wrote a simple code without any structures, and added error checking. When I am starting the server and then the client, the message ABCDE is sent to server and message is getting printed at the server and the server is getting terminated. But at the client side the code is not getting terminated and getting executed only till checkpoint 2. I am not understanding my atleast checkpoint 3 is not getting printed even though the next line is working. And ofcourse, the message from server to client is also not getting printed

2
  • 5
    Your code is missing lots of error checking, it leaks files, writes incorrect informational output, and attempt to print uninitialized data as strings. Not to mention that the structures in the server and client programs doesn't match! Use header files for common structures and constants! Commented Feb 1, 2018 at 14:28
  • ok, even when I writing a simple code, without any structures and with error checking m having same problem. Is it possible to use the same socket i.e. sockfd here, for reveiving and sending. if yes, what checking that has to be done to make sure that the socket has recevied all the data and ready to send the data Commented Feb 1, 2018 at 15:45

1 Answer 1

1

You must use same socket client side. It receives an arbitrary address when you send the first packet to the server. The server knows that address because it gets it from its call to recvfrom and uses it to send its own packet.

That means that client side, the packet is received on the socket bound to that address, that is the one that was used to send the first packet. As you try to read from a different socket, you never read the packet.

And as Someprogrammerdude said in its comment, the sizes of packets in client and server do not match.

So changing 2 lines in client.c is enough to fix the problem:

#define     MAX 500

and

        int  rec_size = recvfrom(sockfd, &recv, sizeof(frame), 0 ,
             (struct sockaddr*) &Addr, &addr_size); 

But there are still numerous problems here, because you should also use one single socket server side and add error checking.

Sign up to request clarification or add additional context in comments.

2 Comments

@user2650443: not working does not help. You should say what exactly happens. But see my edit.
I have edited the question , with a simple code , and included error checking. Please look into it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.