Socket communication between C applications running on the same machine

I am using C language and Linux as a platform to develop two small applications. The first, the client, sends a character over the socket, and the second, the server, reads the message and sends back what was read.

After establishing a connection between apps, the following code should send and receive the same message 5 times:

edited code:

char buf[100];
char message[100];
fd_set readfds, writefds;
int n, rvd;

memset(message, 0, sizeof(message));
message[0] = 'a';

inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
printf("client: connecting to,, %s\n", s);

freeaddrinfo(servinfo);

n = sockfd+1;

for (unsigned long i=0; i<5; i++)
{

    FD_ZERO(&readfds);
    FD_ZERO(&writefds);

    FD_SET(sockfd, &readfds);
    FD_SET(sockfd, &writefds);

    rvd = select(n, NULL, &writefds, NULL, NULL);

    if (rvd > 0)
    {
        printf("client: writing '%s'\n",message);
        if ((numSent = send(sockfd, message, strlen(message), 0)) != -1)
        {

            rvd = select(n, &readfds, NULL, NULL, NULL);
            if (rvd > 0)
            {
                if ((numbytes = recv(sockfd, buf, numSent, 0)) != -1)
                {
                    printf("client: received '%s'\n",buf);
                }
                //timestamp it
                //count successful package sent
            }
            else
            {
                //throw this measurement
            }
        }
    }
}

      

The program sends and receives messages successfully twice. When it tries to send the third time, it fails, although the select function returns a value greater than 0 (which means the server is ready to receive data).

When debugging with eclipse, the send () function fails on its third execution and the following message is displayed:

No source for "send () at 0x7ffff7bcc282"

View Disassembly ... [button]

When I run the server application in the VM everything works fine though.

Any thoughts? Thanks in advance!

+3


source to share


1 answer


There are two problems in the code:

  • you don't reset readfds

    and writefds

    every time you call select()

    , as it changes them every time.

  • you're using it wrong sizeof()

    .



Try something more similar to this:

char message[1024];
char buf[1024];
fd_set readfds, writefds;
int numSent, numRead;

memset(message, 0, sizeof(message));
strncpy(message, "whatever you need to send...", sizeof(message)-1);

for (unsigned long i = 0; i < 5; ++i)
{
    FD_ZERO(&writefds);
    FD_SET(sockfd, &writefds);

    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);

    rvd = select(sockfd+1, NULL, &writefds, NULL, NULL); 
    if (rvd == -1)
        break;

    printf("client: writing '%s'\n", message);
    if ((numSent = send(sockfd, message, strlen(message), 0)) < 1)
        break;

    rvd = select(sockfd+1, &readfds, NULL, NULL, NULL);
    if (rvd == -1)
        break;

    if ((numRead = recv(sockfd, buf, numSent, 0)) < 1)
        break;

    printf("client: received '%*s'\n", numRead, buf);
}

      

+1


source







All Articles