473,804 Members | 3,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to recieve a jpg from server using socket programming?

18 New Member
Hi all,

I have a small problem.

I am trying to download a JPG image from a server using C socket programming.

I use the following to "recv" the byte stream and store it into a file I call "test.jpg".

Expand|Select|Wrap|Line Numbers
  1. int fp = open("c:\\test.jpg", O_RDWR|O_CREAT);
  2.  
  3. /* set up the socket connection */
  4.  
  5. while(recvdata != 0){
  6.    recvdata = recv(sockfd, buff, 100, 0); 
  7.    if(recvdata != 0){
  8.        write(fp, buff, recvdata);
  9.    }
  10. }
  11.  
The problem is, after I try to open the test.jpg, it gives me a "Windows cannot open this image as it appears to be damaged, corrupted, or is too large" message.

If I use fopen to create the file though, I'm able to open the image in a select few image browser softwares, but not in the Windows Photo Viewer.

Can anybody point out what I am doing wrong? Is it possible to transfer an image over sockets like this?

Thanks,
Sid
Aug 23 '10 #1
9 2511
Oralloy
988 Recognized Expert Contributor
Sid,

Try setting your file mode to binary. I'm pretty sure that the I/O system is translating LF to CRLF on you.

If I recall correctly, the uSoft open-mode flag is O_BINARY, but don't quote me on that.

Expand|Select|Wrap|Line Numbers
  1. int fp = open("c:\\test.jpg", (O_RDWR|O_CREAT|O_BINARY));
Cheers!
Aug 23 '10 #2
johny10151981
1,059 Top Contributor
What is your OS? I think Vista or above wont let you create file on root of OS directory.(sorr y didnt read the problem properly)
Aug 24 '10 #3
Airslash
221 New Member
You need to make sort of wrapper around the data, or transmit in a seperate packet first the amount of bytes you're going to read from the socket in order to collect the whole image.

Just because you send out 100 bytes for example, does not guarantee that calling recv with 100bytes will read 100 bytes. You basicly need to keep looping on the receiv function untill all the required bytes are read.

I'm currently doing the same in C#.NET, so I have the advantage that the winsock library etc gets loaded for me, but the idea on both platforms is still the same.
Aug 24 '10 #4
Oralloy
988 Recognized Expert Contributor
@Airslash,

Good thought.

He might be having network difficulties. The odds are that he isn't, though. TCP/IP is basically guaranteed delivery (the error rates are so low in most cases that they are negligable, and certainly difficult to repeat for any small test image). TCP/UDP would be a different thing, but he's using recv(), and not recvfrom().

Unfortunately the code doesn't show how his network connection is set up, so we have no clue about the protocol he's using. It is entirely possible there's a one- or two- byte error in his protocol, resulting in a corrupt file. Check by sending a small plain text file of known size and content.
Aug 24 '10 #5
Airslash
221 New Member
@Orally

You're right.
But I encountered the same problem when dealing with TCP connections to transfer Jpeg images. I often got exceptions on the data transfer and image assignment, but when viewing the image manually it worked fine for me. Kinda the opposite of the OP's case.

It's perfectly possible the buffer used to hold the data is not large enough to contain the whole image.
Aug 24 '10 #6
Oralloy
988 Recognized Expert Contributor
@Airslash,

Well, read buffer length is 100, so it's possible that he's got a buffer overrun. The better code would use:
Expand|Select|Wrap|Line Numbers
  1.    recvdata = recv(sockfd, buff, sizeof buff, 0);    
  2.  
Oh well. Hopefully srbakshi gets back with us and lets us know.
Aug 24 '10 #7
srbakshi
18 New Member
Hello all! :D

Sorry for the delay in replying, too much work in office. :'(

Oralloy you were spot on with your first suggestion about opening the file in binary mode. When I was using fopen to create the file, i *was* using the "b" flag to open it in binary mode, and that was the reason why I could open it in IrfanView.

The reason for the file not opening in Windows Photo Viewer was actually a pretty silly mistake. :P I was trying to get the google logo image from the google home page. This is what I was doing:
1. Create a socket connection to port 80 of www.google.com
2. Fire a GET /intl/en_com/images/srpr/logo1w.png request.
3. Receive the response and store it in a file.

The mistake was that the HTTP response had the HTTP header + the image data and both were getting stored into the file test.png. So I simply skipped the HTTP header and wrote the rest of the incoming bytes into the file (made binary using O_BINARY flag) and I got myself the google logo. :D

Thank you Airslash for your inputs as well. I incorporated them in my code. :)

Thank you so very much. :D
Sid
Aug 25 '10 #8
Oralloy
988 Recognized Expert Contributor
Sid,

Thanks for the feedback. I do appreciate it.

Especially the part about storing the response headers with the file. I've done that sort of thing before, and its good to have periodic reminders of the silly things that we can do wrong.

Cheers!
Aug 25 '10 #9
Airslash
221 New Member
Glad we could help out.
Goodluck with the project.
Aug 25 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3674
by: Henrik | last post by:
I want to download a file using socket programming (and not httprequest) but the DNS keeps giving me errors "IP is ok, but the attached data is not available" I am doing like this and the address is ok. does any one know what is wrong here? IPHostEntry lipa = Dns.Resolve("192.168.0.2/image.jpg");
5
3689
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
2
5293
by: Alpha | last post by:
Hi, I'm able to make connection to a server using socket connection. However, when I send a command string the server just ignores it. All command string needs to start with "0xF9" at Byte 0. During the run-time debug, I see it to be "u" with a "~" on top of it. Is that OxF9? Can someone tell me what I'm doing wrong? Thanks, Alpha private void Connect(String server, String message) { //Socket Connect
8
4686
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data using com component. It is just set of some characters like
0
1016
by: avinashibs | last post by:
how to connect the domain server using socket programing ? actually i have a userid , password . that userid and password go through the server then it will check the email id and password is valid or not . example:- email id :-xyz@gmail.com password:-1234
7
2443
by: nagasrinivas05 | last post by:
hi, this is srinivas here i am using socket programming to send data to a remote computer using clssocket with namespace system.net and it is working well, but when i am sending continuous data in a packet format to one more remote computers there i am facing the problem of mixing status one packet format is #010211* other is $010228&&&&1111* using visual studio 2005 , oracle 10g when continuously sending the packet data is been mixed ...
9
11644
by: phantom3008 | last post by:
Hi i want to record an audio file and send it using UDP . I have server/client program and i'm able to send text file and recive that into another file .But how do i send audio file ..can anyone help me out here ?????? I'm using Unix stations and C .
1
993
by: nagasrinivas05 | last post by:
hi all, i am using socket programming to display status using class socket my application is working fine when i set the port no as 8221 and when i use the same applcation in another location then it is throwing the error as "a request to send or receive the data was disallowed because the socket is not connected and (when sending a datagram socket using a sendto call) no address was supplied" . pls help me as soon as possible
1
1584
by: govind161986 | last post by:
When i execute the below mentioned code in .NET 1.1 i get the following error message "A socket operation was attempted to an unreachable host" but when i execute the same code in .NET 2.0 it executes successfully. main_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); main_ipEndPoint = new IPEndPoint(Dns.GetHostByName(server).AddressList, port); try { ...
2
16149
by: kashifjawed | last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks. Application sometimes transfer file from client to server and sometimes not, because connection betwenn client and server closed Abrupt. I cannot understand why connection between client and server is closed while i cannot closed the connection until a single block tranfered to server. ...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.