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 8634429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:43:19+00:00 2026-06-12T09:43:19+00:00

I am trying to create a basic client/server system in C for a project.

  • 0

I am trying to create a basic client/server system in C for a project. I have the basis of the idea down…I have a while loop in my client executing infinitely until the server sends the kill signal for now. I am reading/writing blocks. I managed to get the first read/write to work with the server, but then it’s not working afterwards.

I am fairly sure I am doing something wrong with the sockets. The idea is I want the server to wait until the client sends another write() command. Here is what I have on the server/client:

Client

short connected = 1;
    do
    {
            // Send initial request on first pass
            // Afterwards send the resposne we were given
            n = write(sockfd, buffer, strlen(buffer));
            if (n < 0)
                     error("Failed to send messege. Terminating.");
            printf("%s\n", buffer);
            // Any errors?
            if (strcmp(buffer, "Session key does not match. Terminating.") == 0)
                    error(buffer);

            // Get session key response
            bzero(buffer, BUFFER_LENGTH);
            n = read(sockfd, buffer, BUFFER_LENGTH);
            if (n < 0)
                    error("Could not fetch result. Terminating.");

            printf("%s\n", buffer);
            sleep(1);
    } while (connected == 1);

Server

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
        printf("Error opening socket.\n");
        exit(0);
}

// Taken from reference
bzero((char *) &serv_addr, sizeof(serv_addr));
portNumber = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portNumber);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
        printf("ERROR on binding.\n");
        close(sockfd);
        exit(0);
}

// Add listner to socket and wait until we are given something to do
// Taken from reference
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
        printf("ERROR on accept.\n");
        close(sockfd);
        exit(0);
}

// Get initial session key request
bzero(buffer,BUFFER_LENGTH);
n = read(newsockfd, buffer, BUFFER_LENGTH);
if (n < 0)
{
        printf("ERROR reading from socket.\n");
        close(newsockfd);
        close(sockfd);
        exit(0);
}

// ... a bunch of processing of data

n = write(newsockfd, response, lengthOfKey + 15 + 1);
if (n < 0)
{
        printf("ERROR writing to socket.\n");
        close(newsockfd);
        close(sockfd);
        exit(0);
}

close(newsockfd);

// Get the previous key(s) now (we hope)
short connected = 1;
while (connected < 100)
{
        printf("waiting...\n");
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

        //... more processing
}
  • 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-12T09:43:20+00:00Added an answer on June 12, 2026 at 9:43 am

    Thanks to the comments I found out that the read() block will stop the server code until it is given something from the client via write(). All working 🙂 Thanks

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

Sidebar

Related Questions

Im trying to create a Speex Voip client and server. I have the basics
I am trying to create a very basic sprite image. First off i have
I'm trying to create a very basic Setup and Deployment project using Visual Studio.
I am trying to create a client server application. I am going to make
I'm trying to create a client for a WCF service that I have hosted
Im learning basic C on my own and trying to create a poker client.
I'm trying to create a very basic server in python that listens in on
I'm trying to create a basic HTTP server to learn more about how it
I have a server for a client-server game (ideally the basis for small MMO)
I'm trying to create a basic Node application, every time a client connects I

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.