harlequin-tech / WiFlyHQ

WiFly RN-XV Arduino Library
Other
110 stars 68 forks source link

How could I creat an Arduino TCP server #25

Closed leolite closed 10 years ago

leolite commented 11 years ago

Thanks to your great lib for Arduino, I have tested TCP client and it's working well!

But, How to build a TCP server on arduino? (e.g ip "192.168.1.100" and port "2000") I want to use my laptop as a client to send some chars to Arduino Server

here is the wifi shield(RN171) which I used http://www.seeedstudio.com/depot/wifi-shield-p-1220.html

Thank you!

harlequin-tech commented 11 years ago

The httpserver example is a TCP server that accepts client connection on port 80. You should be able to base your code on that, change the port to the port you want to use, and strip out all of the HTTP handling code.

Nice shield, looks like you'll be able to do much faster data transfers using the SPI interface instead of the Serial interface.

leolite commented 11 years ago

Thank you for your reply!

The httpserver example is working well after I changed the baud to "9600".(I think it's default configuration of my board)

As you said I have change the example as follow: void loop() { if (wifly.available() > 0) { Serial.write(wifly.read()); } }

But could not connected, I try to ping the server with CMD Pinging 192.168.1.104 Reply from 192.168.1.100: Destination host unreachable

Should I use open(), read(), or available()? I don't understand how the TCP server is working T.T

I found some other example and it looks like // listen for incoming clients if (server.available()) { while (client.connected()) { if (client.available()) { char c = client.read(); }}}

What should I do!?

Thanks a lot for your support!

harlequin-tech commented 11 years ago

Your change for the loop() looks fine. It will send anything received from the TCP connection to the serial monitor.

Do you have a telnet program you can run? Try "telnet 192.168.1.104 80". (80 was the port used in the httpserver example code, unless you have changed that). If that connects then anything you type will be sent to the WiFly module over the TCP connection.

No need to call open() - that is used to make a connection from the WiFly to a server. With the code you have the WiFly should be waiting for a TCP connection, and then sending anything it receives over that connection to your serial monitor.

leolite commented 11 years ago

here is the serial monitor output: .Starting Free memory: 1251 Joining network Joined wifi network MAC: 00:06:66:80:55:51 IP: 192.168.1.104 Netmask: 255.255.255.0 Gateway: 192.168.1.1 Set DeviceID DeviceID: Wifly-TCP-Server Set port to 80, rebooting to make it work Ready

Then I try to telnet the server(I turn on the features from Windows Control panel - Telnet Client, Telnet Server just now) CMD telnet 192.168.1.104:80 Connecting to 192.168.1.104:80...Could not open connection to the host, on port 23: Connect failed

harlequin-tech commented 11 years ago

Can you try "telnet 192.168.1.104 80"? There should be no colon between the IP address and the port, otherwise telnet will try to connect to port 23.

harlequin-tech commented 10 years ago

I assume all is well. Closing this issue.