Hi everyone! Is there a way/command in C to find a string that is contained in a longer string? I'm currently developing a C program in Windows that sends requests to a machine and gets its response through socket programming. The response is an image in jpeg/jfif format. The whole response looks like the one below:

Received data is:

HTTP/1.1 200 OK
SERVER: Network Camera
Date: Thu, 01 Jan 1970 00:01:14 GMT
Content-Type: image/jpeg
Content-disposition: filename="something.jpg"
Connection: close

I want to receive the image sent to me and it should be viewable. I tried to write the whole string above into a .jpg file but no image can be seen. I'm thinking of 2 possible solutions:

1. I might need to just get the "filename=something.jpg" part and save it as a .jpg file.
2. Also from some sources I read in the past, what I need to get are the bits that compose the jpeg file itself.

Does anyone here know which option would most likely solve my problem? Answers will be greatly appreciated. Thanks!

Yes. strstr() in string.h

Yes. strstr() in string.h

Alright, thanks. It didn't solve the problem though. The image my program created doesn't contain anything and is not viewable.

So from the above options I mentioned, option 1 is out the window, which leaves option 2 as the next step to take. Which then leads to my next question: is there a way to receive and manipulate bytes that are received through socket programming?

Hi everyone! Is there a way/command in C to find a string that is contained in a longer string?

strstr() is the answer to this problem of yours.

I'm currently developing a C program in Windows that sends requests to a machine and gets its response through socket programming. The response is an image in jpeg/jfif format. The whole response looks like the one below:

Received data is:

HTTP/1.1 200 OK
SERVER: Network Camera
Date: Thu, 01 Jan 1970 00:01:14 GMT
Content-Type: image/jpeg
Content-disposition: filename="something.jpg"
Connection: close

I want to receive the image sent to me and it should be viewable. I tried to write the whole string above into a .jpg file but no image can be seen. I'm thinking of 2 possible solutions:

1. I might need to just get the "filename=something.jpg" part and save it as a .jpg file.
2. Also from some sources I read in the past, what I need to get are the bits that compose the jpeg file itself.

Does anyone here know which option would most likely solve my problem? Answers will be greatly appreciated. Thanks!

I didn' see where you received the image. The content you mentioned above is just some text headers.

If you want to see the image, the first thing you have to do is transfer the image file through the socket.
For that, you can read the whole .jpg file into a byte array and send it via the socket from the sender.
In the receiver side, after recieving the .jpg file content, create a file with same name and just write the byte stream to the file.
And there there you are.
You can collect the file name from the above content that you received.

strstr() is the answer to this problem of yours.


I didn' see where you received the image. The content you mentioned above is just some text headers.

If you want to see the image, the first thing you have to do is transfer the image file through the socket.
For that, you can read the whole .jpg file into a byte array and send it via the socket from the sender.
In the receiver side, after recieving the .jpg file content, create a file with same name and just write the byte stream to the file.
And there there you are.
You can collect the file name from the above content that you received.

Thanks for the reply. Do I really have to save the .jpg file with the same name?

That's upto you.
If you want to give a new name and save you can do it.

But it is better if u use the same name because, then you will be consistent with other transmission protocols(such as ftp).

That's upto you.
If you want to give a new name and save you can do it.

But it is better if u use the same name because, then you will be consistent with other transmission protocols(such as ftp).

There seems to be another problem. How do I check if I already received all the image bytes? I tried to print what I'm receiving from the socket and here's some additional lines being printed (aside from those I've given at the top of this page)

1. using the

printf("Server: Received data is: \"%s\"\n", recvbuf);

call:
Content-Length: 10046

2. using the

printf("Server: Bytes received: %ld.\n", numbytes);

call:
Server: Bytes received: 225

The 225 bytes received are just the bytes comprising the header. Where are the image bytes?

can you share your sender and receiver code snippet so that we have a better idea about how you are sending the image data via a socket.

can you share your sender and receiver code snippet so that we have a better idea about how you are sending the image data via a socket.

Apparently it was just a silly mistake on my part. I assumed something that's why there's a little bit of logic that was missing on my program. It's nearly fixed now so I wanna thank all of you who replied, y'all certainly provided me clues on how to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.