Runtime error with C program running Cygwin

N

nick048

Hi to all,

I have written a little Server program in C and I have compiled this
with Cygwin without errors. This is my code:

// Prototype
void wordReceived(int sd,char *s);

int main (unsigned argc, char **argv) {
int SERVER_PORT, sock, client_len, fd;
struct sockaddr_in server, client;
char *theword;
..
.. <OTHER CODE>
..
wordReceived(fd,theword); /* Process the word received from
client*/
..
.. <OTHER CODE>
..
}

void wordReceived(int sd,char *s){
int i=0;
int charRecv = 0;
char c[1];

charRecv = recv(sd, c, 1, 0);
while (c[1] !='\n'){
s[i++]=c[1];
charRecv = recv(sd, c, 1, 0);
} // End while (c !='\n')
s='\0';
}

When the program call wordReceived, Cygwin return to me this message:
"5 [main] myserver 2736 _cygtls:: handle_exception: Error while
dumping state (probably corrupted stack)"
and the program hang.

I have modified my program in this way:

int main (unsigned argc, char **argv) {
int SERVER_PORT, sock, client_len, fd;
struct sockaddr_in server, client;
char theword[30];
char c;
int i, charRecv;
..
.. <OTHER CODE>
..
i=0;
charRecv = recv(fd, &c, 1, 0);
while (c !='\n')
{
theword[i++]=c;
charRecv = recv(fd, &c, 1, 0);
} // End while (c !='\n')
theword='\0';
..
.. <OTHER CODE>
..
}

In this way the program run fine.
Wich is my error in the first program ?
How can modify my code in order to use correctly wordReceived?

I hope in Your help in order to resolve my little problem

Thank You and Best Regards
Nick
 
Q

Quentin Godfroy

Hi to all,

I have written a little Server program in C and I have compiled this
with Cygwin without errors. This is my code:
int main (unsigned argc, char **argv) {
int SERVER_PORT, sock, client_len, fd;
struct sockaddr_in server, client;
char *theword;

You declare a char pointer uninitialized
<OTHER CODE>

Do you do something to this pointer in this code?
wordReceived(fd,theword); /* Process the word received from
client*/

You call wordRecieved with theword (as far as I can see,
uninitialized. That is,you give a random address to your function).
void wordReceived(int sd,char *s){
int i=0;
int charRecv = 0;
char c[1];

charRecv = recv(sd, c, 1, 0);
while (c[1] !='\n'){
s[i++]=c[1];

And then you write to this random address, where the program has
probably no right to write.

When the program call wordReceived, Cygwin return to me this message:
"5 [main] myserver 2736 _cygtls:: handle_exception: Error while
dumping state (probably corrupted stack)"
and the program hang.

I don't know cygwin, but I bet that this error means a segmentation
violation.

Cheers,
Quentin.
 
T

Thad Smith

nick048 said:
charRecv = recv(sd, c, 1, 0);
When the program call wordReceived, Cygwin return to me this message:
"5 [main] myserver 2736 _cygtls:: handle_exception: Error while
dumping state (probably corrupted stack)"
and the program hang.
charRecv = recv(fd, &c, 1, 0);
In this way the program run fine.

What is the prototype for recv and description of parameters?
 
C

Christopher Benson-Manica

nick048 said:
I have written a little Server program in C and I have compiled this
with Cygwin without errors.

I was going to say "turn up the warning level on your compiler", but
I found to my dismay that gcc will accept your original code silently;
perhaps I have merely been coding in Java for too long.
char theword[30];
charRecv = recv(fd, &c, 1, 0);
while (c !='\n')
{
theword[i++]=c;
charRecv = recv(fd, &c, 1, 0);
} // End while (c !='\n')
In this way the program run fine.

Note that it only "runs fine" if you receive 29 characters or less.
If you can't arrange to know how many characters you are going to
receive, you will want to use malloc() and realloc().
Wich is my error in the first program ?
How can modify my code in order to use correctly wordReceived?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top