Hi to all,
I have a problem in a little server program written with C.
This is the code of main:
Code:
int main (unsigned argc, char **argv) {
int sock, client_len, fd, received;
struct sockaddr_in server, client;
/* Setup of transport end point */
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("system call socket failed");
exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(SERVER_PORT);
/* binding of transport end point address*/
if (bind(sock, (struct sockaddr *)&server, sizeof(server) == -1) {
perror("system call bind failed");
exit(2);
}
listen(sock, 1);
/* connection of client */
while (1) {
client_len = sizeof(client);
if ((fd = accept(sock, (struct sockaddr *)&client, &client_len)) < 0) {
perror("accepting connection");
exit(3);
}
fprintf(stderr, "Connection Opened.\n");
send(fd, "Wellcome!\n\n", 9, 0);
send(fd, "Send to me Your strings (The last ended with ..)!\n\n", 44, 0);
while(terminated)
{
received = recv(new_sd, &c, sizeof(c), 0);
/* Here the control on the end of trasmission '..' */
}
close(fd);
fprintf(stderr, "Connection closed.\n");
}
}
When the program server run, attend a connection from client; the client (for example a telnet) send your string (many lines) until the last terminate with '..'
The construnct for receiving is: received = recv(new_sd, &c, sizeof(c), 0);
If I use only this construct, without the condition while, the program work fine.
But I must to store each string in a list, in order to process after; how can declare this list ?
Each string has a variable lenght; how can allocate the memory?
Someone has suggested to me the use of malloc, but I don't know how to use.
I hope in Your help; I have worked a little with c++, but with C I have difficulty.
Thank You and Best Regards
Nick