Originally Posted by
bremenpl
My client runs ok now, althrough now i also need to write a server. So I do, but in one of the lines i am receiving segmentation error, which i understand is an event that occurs when i try to acces memory that doesnt exist(?)
And this happens at this line:
without it i can connect to my server and receive answer properly, but receiving answer from dict.org server is faulty.
I have to honest with you, im not sure what is the need for this line.
Would you mind explaining it?
The if-statement checks whether the reply from the server contains the terminating sequence. If it's true it searches for the next newline character which should terminate the status line (the line starting with "250"). But your server doesn't send a correct status message, thus "pos" will be NULL and you are trying to dereference an invalid address.
Code:
if ((pos = strstr(pos, "\r\n.\r\n250")))
{
pos = strstr(pos + strlen(WORLD_2_SEND), "\n");
I've used pos + 6 on purpose in the second call of strstr(). After the first call, "pos" will point to the first '\r' of the sequence "\r\n.\r\n250". Thus pos + 6 will point to '5' and you are able to look for the next '\n' which will terminate the status message ("250 <possible some text>\r\n").
I admit this was a hack and in hindsight I shouldn't have used it. It would be better to split the reply from the server into lines (terminated by '\n'). Then you should simply wait for a line which contains only ".\r\n" which is the sign that the text message is over. Finally you would parse the following status line (parsing a status line is a good candidate for a function).
Code:
message = "151 Definicja Pingwina yo\nDefinicja Pingwina yo\nDefinicja Pingwina yo\n\r\n.\r\n250";
That's the line which is wrong in the server code. The end of the message should read "250\r\n" to send a valid status message.
I've also noticed some other things:
Code:
int socket_desc , new_socket , c , *new_sock;
...
new_sock = malloc(1);
*new_sock = new_socket;
"new_sock" is a pointer to an int but you allocate only 1 byte for it. And int is usually at least 4 bytes nowadays.
What do you want to achieve here?
Code:
char prefix[3];
...
memcpy(prefix, server_reply, sizeof(char) * 3);
if (0 == strcmp("150", prefix))
"prefix" has only room for 3 characters, thus you are missing the terminating '\0'. But strcmp() expects a '\0'-terminated string.
Bye, Andreas