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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:51:13+00:00 2026-06-15T00:51:13+00:00

It is a school assignment and I need build a network that client sends

  • 0

It is a school assignment and I need build a network that client sends a input to server and server will compare it to a file’s content, if the input is exsist on the file as a keyword, server will send back a message of that item in the file. the server will send a message of no match found if the input does not exsist on the file.


I was givern a template of server.cpp and client.cpp by the instructor in order to build a simple network, all I have to do is write the parts of read file, compare input with the file. So far the program of server can send back the item if I input the keyword correctly on client side. the problem is when I input anything that it is not the keyword, server side breaks down and reports me there is a memory problem.


I am not familiar with pointer and memory in C++, so I can’t find out what the problem is, thus I am looking for the reason of occuring this problem and ways of solution. Here is that part of code:

typedef struct {
int Number;
char* Subject_Code;
char* Subject_Title;
char* Timetable;
char* Instru_Name;
} time;

int parseNumber(char *line){
return atoi(line + 8);}
char *parseCode(char *line){
return line + 14;}
char *parseTitle(char *line){
return line + 15;}
char *parseTime(char *line){
return line + 11;}
char *parseInst(char *line){
return line + 13;}

int main(int argc, char **argv){
    char szBuff[1024];
int msg_len;
int addr_len;
struct sockaddr_in local, client_addr;

SOCKET sock, msg_sock;
WSADATA wsaData;

char *fileLocation ="data.txt";
FILE *file = fopen(fileLocation, "r");

if (!file)printf("%s\n",strerror(errno));
char buffer[1024];
time times[128];
int timeindex = 0;

    if (fread(buffer, 1, sizeof(buffer), file)){
        char *line = strtok(buffer, "\n");
        times[timeindex].Number = parseNumber(line);

        // 0 number, 1 code, 2 title, 3 table, 4 name
        int lineType = 1;
        while ((line= strtok(NULL, "\n"))){
            if(lineType == 5)
            {
                timeindex++;
                lineType = 0;
            }
            switch (lineType) {
            case 0:
                {
                    times[timeindex].Number = parseNumber(line);
                    break;
                }
            case 1:
                {
                    times[timeindex].Subject_Code = parseCode(line);
                    break;
                }
            case 2:
                { 
                    times[timeindex].Subject_Title = parseTitle(line);
                    break;
                }
            case 3:
                {
                    times[timeindex].Timetable = parseTime(line);
                    break;
                }
            case 4:
                {
                    times[timeindex].Instru_Name = parseInst(line);
                    break;
                }
            }
            lineType++; 
        }
    }

...
msg_len = recv(msg_sock, szBuff, sizeof(szBuff), 0);
...

if (msg_len > 0){       

    for (int c = 0; c <= timeindex; c++){
        if(strcmp(szBuff,times[c].Subject_Title)==0 || strcmp(szBuff,times[c].Subject_Code)==0 || strcmp(szBuff,times[c].Instru_Name)==0){
            msg_len = send(msg_sock, times[c].Timetable, sizeof(buffer), 0);
            break;
        }
    }




        msg_len = send(msg_sock, "No matchs were found.\n",20, 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-15T00:51:16+00:00Added an answer on June 15, 2026 at 12:51 am

    strtok() requires its input string to be null terminated. fread() does not append the null terminator (nor does it have any room to in the posted code) and buffer is not initialized. If permitted use fgets() instead of fread() and strtok() for processing lines. Otherwise, read one less character with fread() and ensure the last character in buffer is '\0'.

    strcmp() requires its input strings to be null terminated and recv() does not append the null terminator and szBuff is not initialized. Read one less character with recv() and ensure the last character in szBuff is '\0'.

    If times[c].Timetable is pointing to memory that has less bytes than buffer then this will be invalid accessing memory:

    msg_len = send(msg_sock, times[c].Timetable, sizeof(buffer), 0);
    

    Only write the number of characters in times[c].Timetable if it is the case that there is a difference in size.

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

Sidebar

Related Questions

For a school assignment I have to create a C++ program that will create
For a school assignment we have to make a client server chat program in
Hello is have a question for a school assignment i need to : Read
this is for an assignment at school, where I need to determine the size
I am working on a school assignment that requires me to be able to
As a school assignment, I need to find a way to get the L1
I just began studying WCF because i need it for a school assignment. But
I have a school assignment that should be written in C++98-compliant code. How can
In an assignment for school do we need to do some image recognizing, where
For an school project, I need to parse a text/source file containing a simplified

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.