Reputation: 177
Everything I am dealing with is written in c language. I am sending a request to a database server(in c), the server returns the information, I am able to read it from the socket. My question is now that I have it in a buffer, it is stored as such:
[info here] [info here] \n
[info here] [info here] \n
I need to be able to extract each line in order to add:
<tr><td> [info here] [info here] \n
put it back onto another buffer and send that back into another socket going into a website.
This is the code I am implementing but it's not exiting the loop:
FILE *fp = fdopen(db_sockfd, "r");
while(fgets(buff, sizeof(buff), fp))
{
sprintf(db_buffer, "<tr><td>%s", buff);
send(newsockfd, db_buffer, strlen(db_buffer),0);
}
Upvotes: 1
Views: 1946
Reputation: 2440
I think you can use fdopen()
function and changed your socket fd (file descriptor) into FILE*
and then use functions like fgets()
in order to read lines. this solution works on linux.
for you while loop I use following code and it worked OK:
int main(int argc, char *argv[])
{
int server_socket_fd;
int sck;
FILE *fp;
char buffer[1024];
char buff[1024];
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(1373);
server_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket_fd == -1)
perror("socket()");
if (bind(server_socket_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr)) == -1)
perror("bind()");
if (listen(server_socket_fd, 10) == -1)
perror("listen()");
sck = accept(server_socket_fd, NULL, NULL);
fp = fdopen(sck, "r");
while(fgets(buff, sizeof(buff), fp)) {
sprintf(buffer, "<tr><td>%s", buff);
send(sck, buffer, strlen(buffer), 0);
}
}
this code exit from while loop when error occurred or when client close connection.
Upvotes: 1