B
bofh1234
Hello,
I am having a problem with linked lists. My program is based on a
client server model. The client sends some packets of data to the
server. The server reads those packets and is supposed to store the
data in a linked list. It looks like everything works except for the
fact that the linked list only stores the last value sent and the
number of nodes in the linked list is way to high. For example the
client sends 4 create requests to the server, there should be 4 nodes
in the server but there turns out to be 12. All 12 contain the values
of the last request. I am sure the problem is with the linked list
code. If I put in an array it works, but I need a varible size so I
have to use a linked list. Here is my server code.
When create is called the correct varname and varval are printed.
However when findvar is called things go wrong. I am including the
output from a run of the server. From the output there appear to be
two problems, 1) the table does not get created correctly 2) the
findvar function is going crazy most likely due to the table not being
created correctly. What am I doing wrong with the linked lists?
The client code is 4 creates followed by reading those 4 values from
the table.
Thanks,
Mike
/* server code */
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <messages.h>
#define QLEN 5 /* maximum connection queue length */
#define or ||
#define and &&
struct Table1 {
int vartype;
char *varname;
char *ptr;
struct Table1 *next;
};
struct Table1 *Table1ptr=NULL, *temp;
extern int errno;
int serverA(int);
void create(int, struct Table1 **);
void findvar(int, struct Table1 *);
void updatevar(int);
void deletevar(int);
void reaper(int);
int errexit(const char *format, ...);
int passiveTCP(const char *service, int qlen);
int i=0;
/*-------------------------------------------------------------------
* main
*-----------------------------------------------------------------*/
int main(int argc, char *argv[])
{
char *service = "3445"; /* service name or port number */
struct sockaddr_in fsin; /* the address of a client */
int alen; /* length of client's address */
int msock; /* master server socket */
int ssock; /* slave server socket */
switch (argc) {
case 1:
break;
case 2:
service = argv[1];
break;
default:
errexit("usage: serverA [port]\n");
}
msock = passiveTCP(service, QLEN);
(void) signal(SIGCHLD, reaper);
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
exit(dsmd(ssock));
}
/*-------------------------------------------------------------------
* serverA
*-----------------------------------------------------------------*/
int serverA(int fd)
{
int size, n, msgtype=0;
char *bufptr;
for ( ; ; ) {
bufptr=(char*)(&msgtype);
size=sizeof(int);
while ((n=read(fd, bufptr, size)) >0) {
size-=n;
bufptr+=n;
}
if (msgtype == 0)
break;
switch(msgtype) {
case 1:
create(fd, &Table1ptr);
break;
case 2:
findvar(fd, Table1ptr);
break;
case 3:
updatevar(fd);
break;
case 4:
deletevar(fd);
break;
default:
errexit("The server is shutting down due to a problem\n");
}
msgtype=0;
}
return 0;
}
/*-------------------------------------------------------------------
* create procedure for creating variables
*------------------------------------------------------------------*/
void create(int fd, struct Table1 **q)
{
int n, size;
char *createpack;
struct Table1 *temp;
createpacket.requesttype=1;
createpack=(char*)(&createpacket)+4;
size=sizeof(createpacket)-sizeof(int);
while ((n = read(fd, (void*)createpack, size)) >0 ){
size-=n;
createpack+=n;
}
temp = *q;
if(*q==NULL) {
*q=malloc(sizeof(struct Table1));
temp = *q;
}
else {
while((temp->next)!=NULL) {
temp=temp->next;
}
temp->next = malloc(sizeof(struct Table1));
temp=temp->next;
}
temp->varname=createpacket.varname;
temp->vartype=createpacket.vartype;
temp->ptr=createpacket.varval;
temp->next = NULL;
rpacket.rescode=0;
strcpy(rpacket.rvarval,createpacket.varval);
strcpy(rpacket.rdescrip,"");
printf("createvar temp-vartype created = %s \n", temp->varname);
printf("createvar pt->ptr = %s \n", temp->ptr);
write(fd, &rpacket, sizeof(rpacket));
}
/*-------------------------------------------------------------------
* findvar
*------------------------------------------------------------------*/
void findvar(int fd, struct Table1 *pt)
{
int n, size;
char *findpack;
findpacket.requesttype=2;
findpack=(char*)(&findpacket)+4;
size=sizeof(findpacket)-sizeof(int);
while ((n = read(fd, (void*)findpack, size)) >0){
size-=n;
findpack+=n;
}
while (pt!=NULL) {
printf("findpacke varname = %s \n", findpacket.varname);
printf("findvar pt->varname = %s \n", pt->varname);
printf("findvar pt->ptr = %s \n", pt->ptr);
printf("size = %d \n", i); i++;
if (strcmp(findpacket.varname,pt->varname) == 0) {
rpacket.rescode=0;
strcpy(rpacket.rvarval,pt->ptr);
break;
}
else {
rpacket.rescode=-1;
strcpy(rpacket.rdescrip,"varible not found");
}
pt=pt->next;
}
write(fd, &rpacket, sizeof(rpacket));
}
/* output from server */
createvar temp-vartype created = a
createvar pt->ptr = 140
createvar temp-vartype created = b
creatvar pt->ptr = 240
createvar temp-vartype created = c
createvar pt->ptr = 340
createvar temp-vartype created = d
createvar pt->ptr = 440
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 0
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 1
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 2
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 3
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 4
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 5
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 6
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 7
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 8
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 9
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 10
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 11
findpacke varname = d
findvar pt->varname = d
findvar pt->ptr = 440
size = 12
/* this should be the output from the server */
createvar temp-vartype created = a
createvar pt->ptr = 140
createvar temp-vartype created = b
creatvar pt->ptr = 240
createvar temp-vartype created = c
createvar pt->ptr = 340
createvar temp-vartype created = d
createvar pt->ptr = 440
findpacke varname = a
findvar pt->varname = a
findvar pt->ptr = 140
size = 0
findpacke varname = b
findvar pt->varname = a
findvar pt->ptr = 140
size = 1
findpacke varname = b
findvar pt->varname = b
findvar pt->ptr = 240
size = 2
findpacke varname = c
findvar pt->varname = a
findvar pt->ptr = 140
size = 3
findpacke varname = c
findvar pt->varname = b
findvar pt->ptr = 240
size = 4
findpacke varname = c
findvar pt->varname = c
findvar pt->ptr = 340
size = 5
findpacke varname = d
findvar pt->varname = a
findvar pt->ptr = 140
size = 6
findpacke varname = d
findvar pt->varname = b
findvar pt->ptr = 240
size = 7
findpacke varname = d
findvar pt->varname = c
findvar pt->ptr = 340
size = 8
findpacke varname = d
findvar pt->varname = d
findvar pt->ptr = 440
size = 9
I am having a problem with linked lists. My program is based on a
client server model. The client sends some packets of data to the
server. The server reads those packets and is supposed to store the
data in a linked list. It looks like everything works except for the
fact that the linked list only stores the last value sent and the
number of nodes in the linked list is way to high. For example the
client sends 4 create requests to the server, there should be 4 nodes
in the server but there turns out to be 12. All 12 contain the values
of the last request. I am sure the problem is with the linked list
code. If I put in an array it works, but I need a varible size so I
have to use a linked list. Here is my server code.
When create is called the correct varname and varval are printed.
However when findvar is called things go wrong. I am including the
output from a run of the server. From the output there appear to be
two problems, 1) the table does not get created correctly 2) the
findvar function is going crazy most likely due to the table not being
created correctly. What am I doing wrong with the linked lists?
The client code is 4 creates followed by reading those 4 values from
the table.
Thanks,
Mike
/* server code */
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <messages.h>
#define QLEN 5 /* maximum connection queue length */
#define or ||
#define and &&
struct Table1 {
int vartype;
char *varname;
char *ptr;
struct Table1 *next;
};
struct Table1 *Table1ptr=NULL, *temp;
extern int errno;
int serverA(int);
void create(int, struct Table1 **);
void findvar(int, struct Table1 *);
void updatevar(int);
void deletevar(int);
void reaper(int);
int errexit(const char *format, ...);
int passiveTCP(const char *service, int qlen);
int i=0;
/*-------------------------------------------------------------------
* main
*-----------------------------------------------------------------*/
int main(int argc, char *argv[])
{
char *service = "3445"; /* service name or port number */
struct sockaddr_in fsin; /* the address of a client */
int alen; /* length of client's address */
int msock; /* master server socket */
int ssock; /* slave server socket */
switch (argc) {
case 1:
break;
case 2:
service = argv[1];
break;
default:
errexit("usage: serverA [port]\n");
}
msock = passiveTCP(service, QLEN);
(void) signal(SIGCHLD, reaper);
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
exit(dsmd(ssock));
}
/*-------------------------------------------------------------------
* serverA
*-----------------------------------------------------------------*/
int serverA(int fd)
{
int size, n, msgtype=0;
char *bufptr;
for ( ; ; ) {
bufptr=(char*)(&msgtype);
size=sizeof(int);
while ((n=read(fd, bufptr, size)) >0) {
size-=n;
bufptr+=n;
}
if (msgtype == 0)
break;
switch(msgtype) {
case 1:
create(fd, &Table1ptr);
break;
case 2:
findvar(fd, Table1ptr);
break;
case 3:
updatevar(fd);
break;
case 4:
deletevar(fd);
break;
default:
errexit("The server is shutting down due to a problem\n");
}
msgtype=0;
}
return 0;
}
/*-------------------------------------------------------------------
* create procedure for creating variables
*------------------------------------------------------------------*/
void create(int fd, struct Table1 **q)
{
int n, size;
char *createpack;
struct Table1 *temp;
createpacket.requesttype=1;
createpack=(char*)(&createpacket)+4;
size=sizeof(createpacket)-sizeof(int);
while ((n = read(fd, (void*)createpack, size)) >0 ){
size-=n;
createpack+=n;
}
temp = *q;
if(*q==NULL) {
*q=malloc(sizeof(struct Table1));
temp = *q;
}
else {
while((temp->next)!=NULL) {
temp=temp->next;
}
temp->next = malloc(sizeof(struct Table1));
temp=temp->next;
}
temp->varname=createpacket.varname;
temp->vartype=createpacket.vartype;
temp->ptr=createpacket.varval;
temp->next = NULL;
rpacket.rescode=0;
strcpy(rpacket.rvarval,createpacket.varval);
strcpy(rpacket.rdescrip,"");
printf("createvar temp-vartype created = %s \n", temp->varname);
printf("createvar pt->ptr = %s \n", temp->ptr);
write(fd, &rpacket, sizeof(rpacket));
}
/*-------------------------------------------------------------------
* findvar
*------------------------------------------------------------------*/
void findvar(int fd, struct Table1 *pt)
{
int n, size;
char *findpack;
findpacket.requesttype=2;
findpack=(char*)(&findpacket)+4;
size=sizeof(findpacket)-sizeof(int);
while ((n = read(fd, (void*)findpack, size)) >0){
size-=n;
findpack+=n;
}
while (pt!=NULL) {
printf("findpacke varname = %s \n", findpacket.varname);
printf("findvar pt->varname = %s \n", pt->varname);
printf("findvar pt->ptr = %s \n", pt->ptr);
printf("size = %d \n", i); i++;
if (strcmp(findpacket.varname,pt->varname) == 0) {
rpacket.rescode=0;
strcpy(rpacket.rvarval,pt->ptr);
break;
}
else {
rpacket.rescode=-1;
strcpy(rpacket.rdescrip,"varible not found");
}
pt=pt->next;
}
write(fd, &rpacket, sizeof(rpacket));
}
/* output from server */
createvar temp-vartype created = a
createvar pt->ptr = 140
createvar temp-vartype created = b
creatvar pt->ptr = 240
createvar temp-vartype created = c
createvar pt->ptr = 340
createvar temp-vartype created = d
createvar pt->ptr = 440
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 0
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 1
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 2
findpacke varname = a
findvar pt->varname = d
findvar pt->ptr = 440
size = 3
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 4
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 5
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 6
findpacke varname = b
findvar pt->varname = d
findvar pt->ptr = 440
size = 7
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 8
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 9
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 10
findpacke varname = c
findvar pt->varname = d
findvar pt->ptr = 440
size = 11
findpacke varname = d
findvar pt->varname = d
findvar pt->ptr = 440
size = 12
/* this should be the output from the server */
createvar temp-vartype created = a
createvar pt->ptr = 140
createvar temp-vartype created = b
creatvar pt->ptr = 240
createvar temp-vartype created = c
createvar pt->ptr = 340
createvar temp-vartype created = d
createvar pt->ptr = 440
findpacke varname = a
findvar pt->varname = a
findvar pt->ptr = 140
size = 0
findpacke varname = b
findvar pt->varname = a
findvar pt->ptr = 140
size = 1
findpacke varname = b
findvar pt->varname = b
findvar pt->ptr = 240
size = 2
findpacke varname = c
findvar pt->varname = a
findvar pt->ptr = 140
size = 3
findpacke varname = c
findvar pt->varname = b
findvar pt->ptr = 240
size = 4
findpacke varname = c
findvar pt->varname = c
findvar pt->ptr = 340
size = 5
findpacke varname = d
findvar pt->varname = a
findvar pt->ptr = 140
size = 6
findpacke varname = d
findvar pt->varname = b
findvar pt->ptr = 240
size = 7
findpacke varname = d
findvar pt->varname = c
findvar pt->ptr = 340
size = 8
findpacke varname = d
findvar pt->varname = d
findvar pt->ptr = 440
size = 9