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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:14:39+00:00 2026-06-12T18:14:39+00:00

I am building a basic client and server in TCP. it’s working except when

  • 0

I am building a basic client and server in TCP. it’s working except when the key gets kinda large. It just sends back and forth keys until the end of time. When it gets bigger (aka 4096) the read() statement will randomly get a null and then break the key into two msgs which then breaks the cycle.

I’m really not sure why it’s doing this, the server code is working as it has been tested with other clients, so something is wrong with my client. Any ideas (it’s pretty short):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

#define BUFFER_LENGTH 5120

// Error message taken from reference
void error(const char *msg)
{
    printf("%s\n", msg);
    exit(0);
}
int main(int argc, char *argv[])
{
    int sockfd, portNumber, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    // Invalid arguments
    if (argc < 4)
        exit(0);
    else if (atoi(argv[3]) < 1 || atoi(argv[3]) > 4096)
        exit(0);

    char buffer[BUFFER_LENGTH];
    bzero(buffer, BUFFER_LENGTH);
    char buffer2[BUFFER_LENGTH];
    bzero(buffer2, BUFFER_LENGTH);
    strcpy(buffer2, "Connect.  Key length:  ");
    strcpy(buffer, strcat(buffer2, argv[3]));
    portNumber = atoi(argv[2]); // Get port number in int format
    sockfd = socket(AF_INET, SOCK_STREAM, 0); // Create socket connection to server using internet constants

    // Did we open the socket succesfully?
    if (sockfd < 0)
        error("Error opening socket.");

    // Is the IP Address valid?
    server = gethostbyname(argv[1]);
    if (server == NULL)
        error("Could not connect to server. Terminating.");

    // Initialize to zero and then set
    // Taken from reference
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portNumber);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("Could not connect to server. Terminating.");

    short connected = 1;
    do 
    {
        // Send initial request on first pass
        // Afterwards send the resposne we were given
        printf("Sending: %s\n", buffer);
        printf("Length of msg: %d\n", (strlen(buffer) + 1));
        n = write(sockfd, buffer, strlen(buffer) + 1);
        if (n < 1) 
             error("Failed to send Message. Terminating.");

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

        // Stop
        if (strcmp(buffer, "Invalid session key.  Terminating.") == 0)
            break;

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

    // Done (this should never be reached in this client)
    error("Could not fetch result.  Terminating.");
    close(sockfd);
    return 0;
}
  • 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-12T18:14:40+00:00Added an answer on June 12, 2026 at 6:14 pm

    Don’t use strcpy(), or any str*() function, on binary data. You’re not dealing with strings. Use memcpy() and specify the number of bytes you want to copy.

    The str*() functions operate on strings; a string is defined by the C standard as “a contiguous sequence of characters terminated by and including the first null character”. But in binary data, a null character (zero byte) is just another chunk of data that doesn’t necessarily have any significance.

    If your binary data looks like this (in hex):

    4b d9 e7 b3 00 96 89 fb
    

    then strcpy() will ignore everything after the 00 byte. Worse, if your binary data looks like:

    4b d9 e7 b3 2f 96 89 fb
    

    with no null bytes, then strcpy(), or strlen(), or any string function will continue past the end of your buffer, with unpredictable results.

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

Sidebar

Related Questions

I'm building a client/server iPhone game, where I would like to keep third-party clients
I am building a small front-end for a television client. I know the basic
I'm building an app, essentially a very basic Facebook client using the Facebook SDK
I'm playing around with a really basic client<->server project for fun, and thought it
I'm working on building a mobile application for a client using jQuery Mobile. The
I'm building a solution for a client which allows them to create very basic
I'm building a basic file server and my program cannot find files. def sendfile(sock,
I'm building a client server application from an android device to a server over
I'm building a web application and I just received some basic foundation data from
I'm building a basic site and thought of using the flatpages app for a

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.