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);
}
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) andbufferis not initialized. If permitted usefgets()instead offread()andstrtok()for processing lines. Otherwise, read one less character withfread()and ensure the last character inbufferis'\0'.strcmp()requires its input strings to be null terminated andrecv()does not append the null terminator andszBuffis not initialized. Read one less character withrecv()and ensure the last character inszBuffis'\0'.If
times[c].Timetableis pointing to memory that has less bytes thanbufferthen this will be invalid accessing memory:Only write the number of characters in
times[c].Timetableif it is the case that there is a difference in size.