initiating a connection to a server / using the spinneret as a TCP client
laser-vector
Posts: 118
im sure this has been asked millions of times, but i have an application where i must make a connection to a server from a spinnerete and post some data. i have been looking and Looking and LOOKING for good examples. but i can not find any of the code examples that are mentioned (not very excited about that), even the Google code repository is empty!
anyways, i have been reading A LOT.
and from what i have grasped i should be able to do this with the genaric driver (eg: W5100_Indirect_Driver.spin).
i am using the HTTPServer.spin by Mike Gebhard.
i have started commenting out a lot of the unneeded parts (in an attempt to simplify and make it easier to understand whats going on).
i am not opening any listener sockets!!
that is what i have for my first method.
is this correct?
however, calling socket.Connected always returns False. even though my server socket is up and waiting
server is written in Python
PYTHON:
The Python socket listens on that port forever...
i know my network is ok as i am able to ping the spinnerete.
what am i doing wrong? or better yet, not doing??
i am not setting listeners, i have tried many different things, different drivers (if i can find them) and even tried UDP. still no go...
Please Help & Thank You!!
anyways, i have been reading A LOT.
and from what i have grasped i should be able to do this with the genaric driver (eg: W5100_Indirect_Driver.spin).
i am using the HTTPServer.spin by Mike Gebhard.
i have started commenting out a lot of the unneeded parts (in an attempt to simplify and make it easier to understand whats going on).
i am not opening any listener sockets!!
PUB Initialize | id, size, st, temp pst.Start(115_200) pause(200) 'Start the W5100 driver if(Socket.Start) pst.str(string(13, "W5100 Driver Started", 13)) pst.str(string(13, "Status Memory Lock ID : ")) pst.dec(Socket.GetLockId) pst.char(13) if(debugSemId := locknew) == -1 pst.str(string("Error, no HTTP server locks available", 13)) else pst.str(string("HTTP Server Lock ID : ")) pst.dec(debugSemId) pst.char(13) 'Set the Socket addresses SetMac(@mac) SetGateway(@gateway) SetSubnet(@subnet) SetIP(@ip) socket.Initialize(0, TCP_PROTOCOL, 2020, 5009, @remoteIp) pause(200) repeat pause(200) socket.Connect(0) pause(200) bytemove(@data_ptrn, string("test"), strsize(string("test"))) socket.txTCP(0, @data_ptrn, strsize(string("test"))) pst.dec(socket.Connected(0)) socket.Close(0)
that is what i have for my first method.
is this correct?
however, calling socket.Connected always returns False. even though my server socket is up and waiting
server is written in Python
PYTHON:
import socket def do_loop(): sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind(("",5009)) sock.listen(5) s = sock.accept() string = s[0].recv(1024) s[0].close() print string do_loop()
The Python socket listens on that port forever...
i know my network is ok as i am able to ping the spinnerete.
what am i doing wrong? or better yet, not doing??
i am not setting listeners, i have tried many different things, different drivers (if i can find them) and even tried UDP. still no go...
Please Help & Thank You!!
Comments
The following thread has some good info about HTTP POST.
http://forums.parallax.com/showthread.php/147329-How-does-a-POST-header-work
Up-to-date drivers and libraries are on the following Google Code repo
http://code.google.com/p/propeller-w5200-driver/source/browse/trunk/#trunk%2F%20propeller-w5200-driver%2FSpinneret
There's a TCP Client Demo that should get the ball rolling.
http://code.google.com/p/propeller-w5200-driver/source/browse/trunk/%20propeller-w5200-driver/Spinneret/TcpSocketClientDemoDhcpDns.spin
Attached is a TCP Client. It hits a web server but you should be able to update the config to target the Python service.
Thank you very much for those references! i have not had much time the last few weeks to work on this but today i decided to try to pick back up from where i left off.
i am using the TcpSocketClientDemoDhcpDns.spin demo and the Apache web server to accept the HTTP requests from the spinneret.
the issue im having is that apache seems to receive the data from the spinneret but its not the expected length, for example:
Apache will see the spinneret and will echo the "hello from spinner 2" but not as i would have expected. actual output per each request (sometimes it take a few requests before apache will spit this info out):
jakes-iphone.mynetwork.com - - [06/May/2013 10:27:30] code 400, message Bad request syntax ('\xc2\x82^\xdb\xe1y,CV\\\xe8G\xf4p\xf0\x87\x9f\xb2O\x9e\xd1\xaa\x9b\x12\x03\x99\xdd\xcb\xc1\xe4\x92\xc7!\xc1\x9e\xd2\xcf\x8a\x9c\xac\xedM\xdd\xc3\xe7B\xbfoK\xbad?e\xae\xf0\xd3\x15\x93xhello from hello from spinnhhello fromhello from spinner 2 spinner 2\xf6\x18\xb0:[\xda\xa1\xd5\x85\x95L\xa9\xc1\x93^\xf4\xd3o\x1c\xcfKG\x1a\xc0\x063\xb9\xacM\x1f\x15\x13hello from spinner 2\xf7B\xb4\xb7c\x1c\xfd\xe7\x99rE\xf2\xe3\xa9\xf4\x11\xeeR\xcd\xcd\x948\x9dl\xca\xd8_\xdc}\x113u\x9dhello from spinner 2hello from spinner 2I\xa2\x89\xa8\xafhello from spinner 2hellhello from spinner 2\\\xfdc\xff\xe8W\xcf\xf3\xbe\xd2u\x94\x93w\x03##[\xd7\xdai&\x9eZ\xe1hello from spinner 2\xbe`31\xe3\xd3\xd9a+\xba\x16\xc4\xfc\x97\xcf\xdahello from spinner 2m spinner 2\xdf\x88\xcd\xd0{\xf7hello from spinner 2hello from spinner 2s\x12S\xc0\xb3)\xac\xd1L\x1b\xf0\x17{\xd2E\x8chello frohello from spinner 2\xc1hello from spinner 2hello from hello from spinner 2spinnhello frhello from spinner 2o from spinner 2hello from spinner 2hello from spinner hello from spinner 2llo from spinner 2r 2hello frhello from spinner 2om spinner 2hellohello from spinner 2hello from spinner 2hello from spinner 2 frohello from spinner 2m spinner 2hello from spinner 2hello from hello from shello from spinner 2pinner 2hello from spinner 2hello from spinner 2hello from spinner 2hello from spinner 2spinner 2hello from shello from spinnehello from spinner 2hello from spinner 2\x1d$hello from spinner 2\x82\xfa\x0e:\xd8\xc4\xdf\xa4hello from spinner 2}\xc1\xbc\x94\x8a\xe4\xe9\x08\x91\xd5\x9c\xd4\xac\xcdA\xf6C\xcb\x1d\xef\x7f)W\x1fb\xd4\xbe\x10\x02Pd9lhello from spinner 28\x93p\xcd\xd4q\xd4\xe4 \xa4\xb9\x1f\xb1GZ\xc8\xd1\x03\xcc\xdc\xc8A\\\xd4\r7\x93\x18\xda\xc15\x8d\xf8hello from spinner 2\\E<\xe3\x7f\xa8\xa4>\xc7\xa6\xb8TQi\xe3\xb6NC\x85\xa3Z\xd8!b\xf3\xf4\x8c\xe8\x0b\x12\x7f\xed|\x92\x90y\x08\xc8\x7f;?\xc3"\xfao,B\xfc\x13\x8b\x1b`\xe2\x1f)i\xfe\xd6\xd3#{\xc6\xda\xd9\xe8\x94L\xc9\xe5\xc9\xb7j\xd2-\xdaZ\xc7\xe7\xe4\x9c\'R\x01"\xfb\xf09\x02\xd3o\xb6`helhello from spinner 2 2inner 2pinnehello from spinner 2s\x08c\xc7f\xc2D`:AW\xad)\\\x9dI\xf5\x13\xb9w\xbc\x05Y\x83\xba\x86\x18\xbeE\x11qi\xa97\x05\x9f\xfca\x8b\x029\x84\xc7\x95\x84\xe2\xd3y\xdf\x92\xf8\x93\xcb\xd6Rw\xceS\x90>P\x9b\xf9\x1eG4\'f\xff\xa3\xb9\x98\xb4<\x91\x8e\xa9F\xcfm\xe9\xb8X_%\xe62\xfd\xddgz=@m3\x13\xafSa\xc4\xc2\x94\xe4\x88hello from shello from spinner 2hello from spinnehello from spinner 2r 2hello from spinner 2o from spinner 2rom spinner 2\\;1J\xbc\xe5')
res-ssridhara.mynetwork.com - - [06/May/2013 10:27:30] "
ok i feel a little silly, i have actually changed the code so that im not just sending some data, but actually sending a header:
but i still get the bad request 400 errors syntax...
res-ssridhara1.mynetwork.com - - [06/May/2013 11:11:46] code 400, message Bad HTTP/0.9 request type ('m')
jakes-iphone.mynetwork.com - - [06/May/2013 11:11:46] "m HTTP/1.1" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:11:56] code 400, message Bad HTTP/0.9 request type ('r-Agent:')
jakes-iphone.mynetwork.com - - [06/May/2013 11:11:56] "r-Agent: Wiz5200" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:01] code 400, message Bad request syntax ('.1')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:01] ".1" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:06] code 400, message Bad HTTP/0.9 request type ('.htm')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:06] ".htm HTTP/1.1" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:11] code 400, message Bad HTTP/0.9 request type ('/index.htm')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:11] " /index.htm HTTP/1.1" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:16] code 400, message Bad HTTP/0.9 request type ('r-Agent:')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:16] "r-Agent: Wiz5200" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:21] code 400, message Bad request syntax ('nt: Wiz5200\rGET /index.htm HTTP/1.1')
GET /index.htm HTTP/1.1" 400 -m - - [06/May/2013 11:12:21] "nt: Wiz5200
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:26] code 400, message Bad request syntax ('iz5200')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:26] "iz5200" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:31] code 400, message Bad HTTP/0.9 request type ('ser-Agent:')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:31] "ser-Agent: Wiz5200" 400 -
res-ssridhara1.mynetwork.com - - [06/May/2013 11:12:36] code 400, message Bad HTTP/0.9 request type ('gent:')
jakes-iphone.mynetwork.com - - [06/May/2013 11:12:36] "gent: Wiz5200" 400 -
I'm guessing here... but I believe the HTTP header has a problem. If the Apache instance does not have a default site configured then you must send a HOST: header. Otherwise the web server can properly route the request. BTW, the client has been tested with an Apache web server.
Did you try any of the demo HTTP requests?
turns out i was not specifying any delays (shame on me.. i thought i knew this stuff :P ), anyways by adding a few simple pause(200) statements, Apache is now seeing the headers correctly..
I was able to send a post command by adding it to the header, eg: but i wonder if there are easier ways to do this??
either way, its time to have fun
for any interested users,
this may not be the best way to do this but heres what got me up and running using TopSocketClientDemoDhcpDns.spin:
furthermore, i have not removed any of the unnecessary delays, so this could be optimized.
further optimized: