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

  • SEARCH
  • Home
  • 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 8882127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:29:59+00:00 2026-06-14T20:29:59+00:00

Well i am trying to run the server side and the client side in

  • 0

Well i am trying to run the server side and the client side in a for loop. The first time I run it, it runs fine but the second time either the connect fails or it gets stuck at accept(). Here’s my code:

Client code:

for(i=0;i<2;i++)
{
        if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nError : Connect Failed\n");
        return 1;
    }   

    //***************Writing_I***************
    memset(recvBuff, '0',sizeof(recvBuff));
    ticks = time(NULL);
    snprintf(recvBuff, sizeof(recvBuff), "%.24s\r\n", ctime(&ticks));
    if((n = send(sockfd, recvBuff, strlen(recvBuff), 0))<0)
    {
        printf("Error : send operation failed\n");
    }

    //***************Reading_I***************
    memset(recvBuff, '0', sizeof(recvBuff)); 
    n= recv(sockfd, recvBuff, sizeof(recvBuff)-1, 0);
    {
        printf("bytes read: %d\n", n);
        recvBuff[n] = '\0';
        printf("Data: %s\n",recvBuff);
    } 
    if(n < 0)
    {
        printf("\n Read error \n");
    } 
}
close(sockfd);  

Server code:

if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL))>=0)
{ 
    for(i=0;i<2;i++)    
    {   
        fd= open("/home/t/Desktop/CS II/A4/test.txt", O_RDONLY);
        //***************Reading***************
        memset(sendBuff, '0', sizeof(sendBuff)); 
        count = recv(connfd, sendBuff, sizeof(sendBuff)-1, 0);
        {
            printf("bytes read: %d\n", count);
            sendBuff[count] = '\0';
            printf("Data: %s\n",sendBuff);
        }
        if(count < 0)
        {
            printf("Read error \n");
        }

        //***************Writing***************
        ticks = time(NULL);
        memset(sendBuff, '0', sizeof(sendBuff)); 
        printf("Reading from file\n");
        if((noOfBytesRd= read(fd, sendBuff, sizeof(sendBuff)-1))<0)
            printf("\n Error : read operation failed\n");
        sendBuff[noOfBytesRd]='\0';
        if((count = send(connfd, sendBuff, strlen(sendBuff), 0))<0)
            printf("\n Error : wtite operation failed\n");
    }
    close(connfd);  
 }
  • 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-06-14T20:30:00+00:00Added an answer on June 14, 2026 at 8:30 pm

    A socket can be connect‘ed only once in its life cycle. You should create a separate socket for each connection you intend to make, and close it when you’re done with the connection.

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

Sidebar

Related Questions

Well, I'm trying to run a client-server application with encryption using AES on VS
Well im trying to write my own custom control extender but i can't get
Well I am trying to learn the java MVC pattern, but I can't understand
I am trying to create a simple voucher program. Client connects to server and
I'm trying to follow this tutorial on configuring server-side SOAP tracing for my WCF
well iam trying to get version of IE used by user by using $.browser.version
Hi guys thanks for pay attention to this. Well im trying to replace every
Well,I am trying to do something similar what I did in Adobe Flex.Consuming a
Well I'm trying to set a variable to use in a thread, it works
Alright well I am trying to create a dictionary from a text file so

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.