save jpg from a webserver

Windows specific questions.
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

save jpg from a webserver

Post by baddzs »

hi all!
I would like to save a jpg from a webserver to my pc. ip and port are fix.
This webserver is webcamXP http server and the page continuous are refreshing to load the new jpg picture in web browser.
Please help!
THX! (and I am a beginner programmer! :) )
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

A beginner programmer is going to have a hard time writing that on his own. There is a library by D. J. Peters for this sort of thing, search the forums and perhaps you can figure it out.
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

the example getfile in fbnet.zip are only for TEXT and HTML sites but with short changes for pictures too.

here are the download: get: fbNet.zip

As beginner you must only know how the HTTP GET sequence looks for pictures.

Joshy
Last edited by D.J.Peters on Jan 14, 2014 8:57, edited 3 times in total.
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

it works, but i can't found how can i use the get for pictures... pf.. i'am searching...
thx!
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

this is the right http get request
accept = "Accept: image/jpeg"

get my nice image :-)

Joshy
"getpicture.bas"

Code: Select all

#include "net.bi"
#define crlf chr(13) & chr(10)

dim as FDSOCKET    hClient,hServer
dim as SOCKADDR_in ServerAddr,Iam
dim As HOSTENT ptr lpResolver
dim as string      msg
dim as integer     ret
dim as uinteger    ip
dim as string      servername="alice-dsl.net"

' create socket
hClient=NetSocket(AF_INET,SOCK_STREAM)
if hClient<0 then
  ? "error: create socket!"
  sleep:end 2
end if

' get ip from server
lpResolver=NetGetHostByName (servername)
if lpResolver=0 then
  ? "error: NetGetHostByName!"
  beep:sleep:end 3
else
  ip=*lpResolver->h_addr_ip_list[0] 'server ip
end if

with ServerAddr
  .sin_family = AF_INET    ' protocol
  .sin_port   = htons(80)  ' port 80   in network byte order
  .sin_addr   = ip         ' server ip in network byte order
end with

' connect to server
ret=NetConnect(hClient,@ServerAddr)
if ret<0 then
  ? "error: can't connect to server!"
  beep:sleep:end 4
end if

dim as string method,host,accept
method    = "GET /d.j.peters/images/joshy.jpg HTTP/1.1"  & crlf
host      = "Host: alice-dsl.net"  & crlf
accept    = "Accept: image/jpeg"   & crlf
msg       = method & host & accept & crlf

ret=NetSendString(hClient,msg)
if ret<=0 then
  ? "error: send string!"
  NetClose hClient
  beep:sleep:end 6
end if

print "get file joshy.jpg please wait ..."
print "---------------------------------"
ret=NetReceiveString(hClient,msg)
' close the connection
NetClose hClient

if ret<0 then
  ? "error: get file/site!"
  beep:sleep
else
  dim as string header
  dim as string image
  ret =instr(msg,crlf & crlf) 'search for header terminator
  header=left(msg,ret)
  print header
  print "---------------------------------"
  ret+=3 ' jump over the header
  image =right(msg,len(msg)-ret)
  'optional save the answer
  open exepath + "\joshy.jpg" for output as #1
  print #1,image
  close #1
  print "file:" & exepath + "\joshy.jpg saved"
  beep:sleep
end if
end
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

thank you very much!!
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

it works, but too slow...:(, because the refreshing of picture is 15fps...can this work faster? (more faster) ?? i tried to do a loop without same parts of prg but error error error...:(... I would like to use in real-time for robot control...
thank you again!
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Wait you are talking about one webserver that writes 15 jpeg files per seconds and the same server sends you the curent picture after you made a http GET request?

i can't belive that

15 fps with compression normaly the result should be a video stream

i think a seperate video capture app. puts the pictures on the HD and the other app. the WEB server answers your http get requests.

while the capture app. writes a picture to the HD normaly the file is locked
the web server must wait before it can read it again.
(or the result will be an corrupt jpeg file)

curently you use:

do
__create socket
__get ip
__connect to server
__send one http request (close conection after transfer is the default mode)
__wait for answer
__close conection
__cut the jpg from transmision
loop

but it should be:

create socket
get ip
connect to server
send one http request (with keep alive mode)
do
__wait for answer
__cut the jpg from transmision
__send next http request
loop
send last http request (with close connection mode)
close socket

bla bla bla ;-)

Joshy
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

thanks!! i will try....
oh, infos: this is a LAN server, the server made by WebcamXP. Tis software makes the pictures from a webcam. And refreshing for page...
And if i type to webbrowser the ip of this server i can see a page with a javascript that refreshing the jpg 15 times per second...
(the 10fps enought for me.)

i can try do it yourself.. but pf.. i am too stupid for network, http, get, and same things..
(sorry but I am a .... a beginner and the other is i think my english isn't the best...:) )
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

google for the http request specs. you need the "keep alive" part
without it the web server will close the connection after every picture
you know what i mean?

what are the domain of the the pictures and the file filename?

Joshy
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

there are only ip, : i type to browser this:
http://192.168.0.3:8080/cam_1.jpg
and i can see only the picture

if i type only http://192.168.0.1:8080 i can see the page , that automatic load the new jpg (15 per sec)
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

hello baddzs
i was wondering since HTTP1.1 keepalive is the default mode
sorry about this mistake.

Joshy
baddzs
Posts: 22
Joined: Jul 28, 2006 14:18

Post by baddzs »

hi, i'm here again...

new problem: the saved image isn't perfect, half of image is lost.. why? how can I solv the problem??? thanks!
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

hello baddzs
how large is the image on HD in bytes?

Joshy
Post Reply