By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Runtime error with C program running Cygwin

P: n/a
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[i]='\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[i]='\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

May 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 29, 1:57 am, nick048 <nicosia.gaet...@moonsoft.itwrote:
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.
May 29 '07 #2

P: n/a
nick048 wrote:
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?

--
Thad
May 29 '07 #3

P: n/a
nick048 <ni*************@moonsoft.itwrote:
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?
I hope in Your help in order to resolve my little problem
Thank You and Best Regards
Nick

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
May 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.