Main Site Documentation

Cobra II webserver in adhoc mode


#1

Im trying to run a webserver on a Cobra II with onboard wifi. My goal is to run the server with no external router, so ad-hoc. The Cobra will be gathering data, and the idea is to access a webpage (or a custom socket connection) and display the data on a phone/webbrowser.
The adhoc setup runs fine from what I can see, but when trying to run a server or bind a socket, I start to get exceptions. I dont know if the ip 0.0.0.0 is invalid. If I use it, the socket will bind with no exception, but throw at .Accept() … Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1)… #### SocketException ErrorCode = 10050.
If I use a valid ip, ex 192.168.1.1 I get the same exception when binding


Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the listening socket to the port
IPAddress hostIP = IPAddress.Parse(NI.IPAddress);
IPEndPoint ep = new IPEndPoint(hostIP, 80);
     
listenSocket.Bind(ep);

Has anyone tried this type of setup with this card? If there is a better way to solve the problem, Id be glad to hear.


#2

I don’t know the answer to your challenge, but there’s a few things I can point out.

Ad-Hoc is a wireless concept. You need to establish the wifi module as an ad-hoc access point. At that point it behaves like a router. You most likely will need it to be allocating IP addresses (unless Ad-hoc AP mode uses a known range of addresses like a 169 address?). You will need to research this function separate to any use of that. I’d attempt to get the AP publishing itself and then establish a connection to it from the external device. You say it “runs fine from what I can see”, what can you see?? What have you done to set it up?

The webserver just needs to listen on a port on the local network. This will require a non-zero IP address, again the ad-hoc mode research is key here, figuring out what is a valid IP in this mode is key, and whether the wifi module allocates it or you have to assign it will dictate what you need to do. You probably should start the webserver on a valid IP address being received or assigned, otherwise you might still lead to exceptions

So the key next step for you is understanding ad-hoc and IP addresses with your wifi module, so you can then leverage that


#3

As Brett pointed out, first setup your WiFi module in adhoc mode.
Set IP address to 169.254.0.x
When you use a Windows PC or Apple device you will see the SSID as defined in adhoc setup.

Once you connect your PC or Apple device to your adhoc device it will try to get an IP address using the DHCP protocol. Since the adhoc device does not support DHCP server the PC or Apple fall back to an internal assigned IP address. This address will be in the 169.254.x.x range. Once its in this mode you are able to communicate

Some Android devices will work in adhoc mode, others don’t… For those who worked for me needed manual WiFi / adhoc mode setup


#4

I’m able to create the network. My phone receives an address (169.254.200.120). The problem is that as soon as I try to bind a System.Net.Socket, I get a SocketException.

Ip init:
NI.EnableStaticIP(“169.254.100.2”, “255.255.0.0”, “169.254.100.1”);

Exception when binding:

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### CobraWebserver.Program::Main [IP: 0015] ####
#### SocketException ErrorCode = 10022
#### SocketException ErrorCode = 10022

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10022
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

The error code is “Invalid argument”, but I cannot figure out what would be wrong. I guess it should be the IP adress, since the address and portnr is the argument. But what ip to send then?


#5

Part of your problem now is subnet masks. try with 255.255.0.0 because otherwise you have no “route” to the endpoint.


#6

@ John Karbin - Can you show us your code that setup your wifi and network?


#7

255.255.0.0 is what I used.

Binding throws exception regardless if another device is connected to the network or not.

Does the binding require an active endpoint? I thought that I could bind to a socket, and then wait for a connection.


#8

Solved the problem I think. I moved the socket binding to _WirelessConnectivityChanged. The network was probably not fully initialized when binding right after creating the adhoc network.