Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:06:09+00:00 2026-05-27T14:06:09+00:00

While working in client-server programming, I have passed 3 strings in client, which will

  • 0

While working in client-server programming, I have passed 3 strings in client, which will be received by server and it should be printed in there 3 times. (i.e I have used a ‘for’ loop which will do the read & write operations in client & server side respectively.), but in server only the 1st string is getting printed.

Please explain,

Here is my code

server.c

#include "head.h"

void readstr(int connfd ,char [][20]);
//void writestr(char * ,int);

int main(int c ,char *v[])
{

         // socket declarations,etc
        sd =socket( AF_INET ,SOCK_STREAM ,0);


          // Binding socket
        retbind =bind(sd ,(struct sockaddr*)&serveraddress ,sizeof(serveraddress
));


        listen(sd ,4);
        for(;;)
        {
        printf("i am waiting for client\n");
        len =sizeof(cliaddr);
        connfd = accept(sd ,(struct sockaddr*)&cliaddr ,&len);
        readstr(connfd ,databuf);
        close(connfd);
        }
        return 0;
}

void readstr(int connfd ,char str[3] [20])
{
        int pointer=0 ,i=0, n,pos=0;
        memset(str ,'\0',sizeof(str));
        for(i=0;i<3;i++)
        {
        while((n=read(connfd ,str[i] ,20)) >>0)
        {
                printf("Looping while\n");
                pos =pos +n;

        }
        str[i][pos] ='\0';
        }
        for(i=0;i<3;i++)
        {
                printf("\n%s",str[i]);
        }
}

client.c

 #include "head.h"
    void send1(int ,char*);

    int main(int c,char*v[])
    {

            //Socket declarations, etc..
            sd = socket(AF_INET ,SOCK_STREAM ,0);
            //Connect
            if(connect(sd,(struct sockaddr*)&serveraddress ,sizeof(serveraddress)) <
     0)

            {
                    printf("cannot connect to server");
                    exit(1);
            }
            for(i=0;i<3;i++)
            {
                    memset(buf ,'\0',sizeof(buf));
                    printf("\n Enter the string : ");
                    fgets(buf[i],20,stdin);
                    len =strlen(buf[i]);
                    if(buf[i][len] =='\n')
                      buf[i][len]='\0';

                   send1(sd ,(char *)buf);
            }
            shutdown(sd ,SHUT_WR);
    }

    void send1(int sd ,char *str)
    {

            int n ,byteswritten =0, wr;
            char buf[1024];
            strcpy(buf ,str);
            n =strlen(buf);
            while(byteswritten < n)
            {
            printf("\nStarting to write in client side\n");
            wr = write(sd , buf+byteswritten ,(n-byteswritten));
            byteswritten+=wr;
            }
            printf("\n string sent %s" ,buf);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:06:10+00:00Added an answer on May 27, 2026 at 2:06 pm

    In server.c in readstr() you are not setting pos to zero before the next for iteration.
    Also, there is strange line:

    while((n=read(connfd ,str[i] ,20)) >>0)
    

    Note “>>”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a (relatively) simple tcp client/server chat program for my
While working between a Windows MySQL server and a Debian MySQL server, I noticed
While working on a WebSocket server in Java I came across this strange bug.
I'm working on a client/server application in C#, and I need to get Asynchronous
I'm working with a persistent client/server protocol and I need to design a RESTful
I am working on a server client application right now (just for learning purposes),
I've just begun socket programming, and I'm working on an Echo server in Java.
I'm working on a socket based client-server app. When a user running any iDevice
I'm using .NET 3.5 and WCF for developing a server-client application. Binding=BasicHttp. I'm working
I am programming a Client/Server application for my project in C++. The whole application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.