N
nick048
Hi to All,
Foollowing Your suggestion, I have written this code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define BUFLEN 100
struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;
typedef struct node* newlist ;
void insert_myString (newlist *test, const char *buf);
int wordCounter(char *a, char *b);
int main( void )
{
char myString1[ ] = "this is the first string and after is the
second string";
char myString2[ ] = "this is the second string";
char myString3[ ] = "this is the third string; no strings after
this string";
newlist mytest = NULL ;
char myWord[ ] = "string";
int counter = 0;
int n = 0;
insert_myString(&mytest, myString1) ;
insert_myString(&mytest, myString2) ;
insert_myString(&mytest, myString3) ;
while (mytest != NULL)
{
counter = wordCounter(mytest->info, myWord);
n = n + counter;
mytest = mytest->pun ;
}
printf("The occurrences number of word 'string' in the entire
struct is: %d\n", n);
sleep(3);
}
void insert_myString(newlist *test, const char *buf)
{
struct node *prec = *test ;
// create element
struct node *p = (newlist) malloc(sizeof(struct node)) ;
if (p == NULL) {
fprintf(stderr, "Error: memory allocation\n" ) ;
exit(-1) ;
}
strcpy(p->info, buf) ;
p->pun = NULL ; // init of last element
// the list is empty
if (prec == NULL)
*test = p ;
else {
while (prec->pun != NULL)
prec = prec->pun ;
prec->pun = p ;
}
}
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}
After I have compiled the program and it's work fine.
Now, I have inserted the same solution in a my little server program
(sorry for the lenght)
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <malloc.h>
#define MYPORT 10000
#define MAXCONN 5
#define BUFLEN 100
struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;
typedef struct node* newlist ;
typedef int Boolean;
/* Prototype */
void insertList (newlist *test, const char *buf);
int occurr_counter(newlist p, const char *buf);
int word_counter(char *a, const char *b);
int main(int argc, char *argv[])
{
struct sockaddr_in myServer, Client;
int socketfd, newsocketfd, client_len, char_recv, i, n, occur;
newlist newtest = NULL ;
char c;
char myString[BUFLEN];
char myWord[20];
Boolean done = 0;
char terminator[3]={'.','.','\n'};
/* local socket descriptor */
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("system call socket failed");
exit(1);
}
memset ( &myServer, 0, sizeof(myServer) );
myServer.sin_family = AF_INET;
myServer.sin_addr.s_addr = htonl(INADDR_ANY);
myServer.sin_port = htons(MYPORT);
if (bind(socketfd, (struct sockaddr*) &myServer, sizeof(myServer))
== -1) {
perror("system call bind failed");
exit(2);
}
listen (socketfd, MAXCONN);
while (1) {
client_len = sizeof(Client);
if ((newsocketfd = accept(socketfd, (struct sockaddr *)&Client,
&client_len)) < 0) {
perror("Connection accept error");
exit(3);
}
printf("Connection Opened.\r\n");
send(newsocketfd, "Wellcome!\r\n", 11, 0);
send(newsocketfd, "Send to me Your strings !\r\n", 24, 0);
send(newsocketfd, "Close the last with .. !\r\n", 34, 0);
while (!done)
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
myString[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
myString='\0';
/* insert myString in the struct */
insertList(&newtest, myString) ;
/* Is the last ? */
if (i > 2 && memcmp(&myString[i - 3], terminator, 2) == 0)
{
done = 1;
} /* End di if */
} /* while (!done) */
/* Query myWord */
send(newsocketfd,"Wich word will You to search?\r\n",30,0);
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
myWord[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
myWord='\0';
/* I search the occurrences of myWord in the entire struct */
occur = occur_counter(newtest, myWord) ;
/* Connection closed */
send(newsocketfd,"Now I close the connection\r\n",24,0);
sleep(3);
close(newsocketfd);
printf("Connection closed.\r\n");
}
}
int occur_counter(newlist p, const char *buf)
{
int n, occ;
occ = 0;
n = 0;
while (p != NULL)
{
printf("Element: %s\n",p->info);
occ = word_counter(p->info, buf);
n = n + occ;
p = p->pun ;
}
return(n) ;
}
void insertList (newlist *test, const char *buf)
{
struct node *prec = *test ;
// element creation
struct node *p = (newlist) malloc(sizeof(struct node)) ;
if (p == NULL) {
fprintf(stderr, "'allocation memory error\n" ) ;
exit(-1) ;
}
strcpy(p->info, buf) ;
p->pun = NULL ;
// the list is empty?
if (prec == NULL)
*test = p ;
else {
while (prec->pun != NULL)
prec = prec->pun ;
prec->pun = p ;
}
}
int word_counter(char *a, const char *b)
{
int cnt = 0;
char *sptr = a;
printf("%s\n", sptr);
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
printf( "%d\n", cnt );
return(cnt);
};
I have compiled the program (used cygWin) without errors. From cygwin
I run this program and with windows telnet I send to server the same
strings of first sample.
I have tested and the server receive and store correctly the string
sended from client.
When I search the occurrences of word "string", the server return a
mistaken number for each string.
Have You an "idea" ? I hope in Your help.
Thank You and Best Regards
Gaetano
Foollowing Your suggestion, I have written this code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define BUFLEN 100
struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;
typedef struct node* newlist ;
void insert_myString (newlist *test, const char *buf);
int wordCounter(char *a, char *b);
int main( void )
{
char myString1[ ] = "this is the first string and after is the
second string";
char myString2[ ] = "this is the second string";
char myString3[ ] = "this is the third string; no strings after
this string";
newlist mytest = NULL ;
char myWord[ ] = "string";
int counter = 0;
int n = 0;
insert_myString(&mytest, myString1) ;
insert_myString(&mytest, myString2) ;
insert_myString(&mytest, myString3) ;
while (mytest != NULL)
{
counter = wordCounter(mytest->info, myWord);
n = n + counter;
mytest = mytest->pun ;
}
printf("The occurrences number of word 'string' in the entire
struct is: %d\n", n);
sleep(3);
}
void insert_myString(newlist *test, const char *buf)
{
struct node *prec = *test ;
// create element
struct node *p = (newlist) malloc(sizeof(struct node)) ;
if (p == NULL) {
fprintf(stderr, "Error: memory allocation\n" ) ;
exit(-1) ;
}
strcpy(p->info, buf) ;
p->pun = NULL ; // init of last element
// the list is empty
if (prec == NULL)
*test = p ;
else {
while (prec->pun != NULL)
prec = prec->pun ;
prec->pun = p ;
}
}
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}
After I have compiled the program and it's work fine.
Now, I have inserted the same solution in a my little server program
(sorry for the lenght)
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <malloc.h>
#define MYPORT 10000
#define MAXCONN 5
#define BUFLEN 100
struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;
typedef struct node* newlist ;
typedef int Boolean;
/* Prototype */
void insertList (newlist *test, const char *buf);
int occurr_counter(newlist p, const char *buf);
int word_counter(char *a, const char *b);
int main(int argc, char *argv[])
{
struct sockaddr_in myServer, Client;
int socketfd, newsocketfd, client_len, char_recv, i, n, occur;
newlist newtest = NULL ;
char c;
char myString[BUFLEN];
char myWord[20];
Boolean done = 0;
char terminator[3]={'.','.','\n'};
/* local socket descriptor */
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("system call socket failed");
exit(1);
}
memset ( &myServer, 0, sizeof(myServer) );
myServer.sin_family = AF_INET;
myServer.sin_addr.s_addr = htonl(INADDR_ANY);
myServer.sin_port = htons(MYPORT);
if (bind(socketfd, (struct sockaddr*) &myServer, sizeof(myServer))
== -1) {
perror("system call bind failed");
exit(2);
}
listen (socketfd, MAXCONN);
while (1) {
client_len = sizeof(Client);
if ((newsocketfd = accept(socketfd, (struct sockaddr *)&Client,
&client_len)) < 0) {
perror("Connection accept error");
exit(3);
}
printf("Connection Opened.\r\n");
send(newsocketfd, "Wellcome!\r\n", 11, 0);
send(newsocketfd, "Send to me Your strings !\r\n", 24, 0);
send(newsocketfd, "Close the last with .. !\r\n", 34, 0);
while (!done)
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
myString[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
myString='\0';
/* insert myString in the struct */
insertList(&newtest, myString) ;
/* Is the last ? */
if (i > 2 && memcmp(&myString[i - 3], terminator, 2) == 0)
{
done = 1;
} /* End di if */
} /* while (!done) */
/* Query myWord */
send(newsocketfd,"Wich word will You to search?\r\n",30,0);
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
myWord[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
myWord='\0';
/* I search the occurrences of myWord in the entire struct */
occur = occur_counter(newtest, myWord) ;
/* Connection closed */
send(newsocketfd,"Now I close the connection\r\n",24,0);
sleep(3);
close(newsocketfd);
printf("Connection closed.\r\n");
}
}
int occur_counter(newlist p, const char *buf)
{
int n, occ;
occ = 0;
n = 0;
while (p != NULL)
{
printf("Element: %s\n",p->info);
occ = word_counter(p->info, buf);
n = n + occ;
p = p->pun ;
}
return(n) ;
}
void insertList (newlist *test, const char *buf)
{
struct node *prec = *test ;
// element creation
struct node *p = (newlist) malloc(sizeof(struct node)) ;
if (p == NULL) {
fprintf(stderr, "'allocation memory error\n" ) ;
exit(-1) ;
}
strcpy(p->info, buf) ;
p->pun = NULL ;
// the list is empty?
if (prec == NULL)
*test = p ;
else {
while (prec->pun != NULL)
prec = prec->pun ;
prec->pun = p ;
}
}
int word_counter(char *a, const char *b)
{
int cnt = 0;
char *sptr = a;
printf("%s\n", sptr);
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
printf( "%d\n", cnt );
return(cnt);
};
I have compiled the program (used cygWin) without errors. From cygwin
I run this program and with windows telnet I send to server the same
strings of first sample.
I have tested and the server receive and store correctly the string
sended from client.
When I search the occurrences of word "string", the server return a
mistaken number for each string.
Have You an "idea" ? I hope in Your help.
Thank You and Best Regards
Gaetano