How do I ping a server port with iOS 5 SDK - objective-c

Basically, I want to check to see if a game server is online/offline but the server is hosted through a port, how would I go about pinging it to check this. For testing purposes the server I want to ping is fr7.mooshroom.net:25667, however eventually I will be importing the server IP and port from an online plist so could the ip address and port be separated
If you could give me a step by step guide on how to do this, that would be much appreciated.
I am using the Reachability by tonymillion because the apple one doesnt work with iOS 5.

First, it depends on if you are talking about TCP or UDP.
If it's TCP, then the answer may be simple: try to open a TCP connection to the server at that port. If the connection opens, drop the connection and report success. Otherwise, failure. This is the simplistic view. It's possible if there are load balancers or firewalls in front of the server, the TCP connection may open but the backend server is down.
UDP would be harder. There is no way to know when you send UDP data that the server got the data unless it sends you some kind of response. It's possible that if the UDP server is down and you sent a message to the port, your computer might get an ICMP Error message back. That would definitely let you know the server is down. But firewalls may block this message getting back to your device, so that might not be reliable.
Otherwise, you need to send a properly formatted message to the server to get some kind of response. This is protocol-dependent but is the most reliable.
The traditional "ping" message is an ICMP echo query and response message. As such, there is no "port" associated with ICMP.

Related

Why is my UDP saying it is connected when it is not?

My issue is is that my UDP client is reporting that it is connected when I have nothing on the network. I'm trying to connect to a PLC module with IP 10.10.10.10 at port 7775 from a computer port of 7777. The Connect() function from the client is working, even though there is no Ethernet or wireless connection turned on.
The core of my problem revolves around my connection code I believe
Public Sub Connect()
Try
If (plcType = 0) Then
udpClient = New UdpClient(7777)
udpClient.Client.Connect(IPAddress, port)
SendCommand("ME")
End If
Catch
MsgBox("Could not connect to the controller, please check IP address." & vbCrLf & Err.Description)
'Connect()
End Try
End Sub
If I insert a break point after the Connect() command I get extremely strange behavior. After the break point is hit if I look into udpClient.Client's properties it says it is connected, but if I leave those properties and then look at it again it will suddenly change and say it is not connected and the program will correctly read that it is not connected after that point.
This issue only appeared After I connected a second computer and attempted to run the same program on it to the same device. Neither computer can correctly talk to the device anymore, that I can understand, but I can't understand why one of my computers believes that it is talking to the device when it clearly isn't.
Thank you for any help.
Update: Any connection available will trigger this condition, only if all networks are disable or disconnected will it behave as expected. However, it is still reporting as connected when there is no device on the network of that IP.
So #CodeCaster is right saying that "UDP has no notion of being connected". UDP is a connectionless fire-and-forget protocol. When you send a datagram, you have no guarantee it will reach its destination.
Different "connect" operations that are available across a multitude of languages merely mean "ok, we'll use this socket for talking to this specific address and port". This knowledge does not translate to packets being sent over the network. It is a local thing that allows you to use send function instead of sendto.
If you want to know if there's a device on your network listening to a specific IP and UDP port, then you're going to need your own synchronizing mechanism. The protocol does not support that out of the box. So examples are:
Heartbeat. Let your remote peer send a special multicast message every 5 seconds. If you haven't received a heartbeat message in 15 seconds, assume the remote peer is down. Adjust numbers at your own discretion.
Ping-pong, or echo request - echo response. Periodically send a special datagram to the remote peer and expect a response.
Handshakes, acknowledges, retries... The more robust channel you need, the more it is going to remind TCP. This is a kind of a sign to switch protocols!

Use wireshark to detect problems with webRTC

so i started to work in this summer and the first task they have given to me is to use wireshark to understand why an application that uses webRTC doesn't use the turn server.
Can you guys help me out, to understand which steps should i do to understand better where is the problem.
I already run the wireshark and only get protocols STUN, that bind to a UDP connection.
TURN is a STUN extension so you will only see STUN packets in Wireshark.
You can easily test WebRTC+TURN in isolation using this sample from the WebRTC project. Remove the default stun server and add the url and credentials for your own TURN server.
Fire up wireshark, start capturing.
Click the "gather candidates" button on that page. You should see candidates with host type at least. You should, if the browser can reach the TURN server usually also see candidates with a srflx type.
If the TURN server is working and your credentials are valid, then you will get candidates with type relay. But you probably wouldn't be asking then.
Now go back to wireshark. Set the display filter to 'stun'. You should see some packets sent to the ip address of the TURN server. Right-click on one of them, 'follow' and 'udp stream'. That should show you all the packets between the browser and the TURN server.
You should be seeing binding requests (message_type=0x01) as well as binding success responses (message_type=0x101) from the server. If you don't see those, your turn server is not responding or something is blocking the client. You will also not get srflx candidates on the candidate gathering demo page.
You should also see packets wireshark interprets as 'allocate request udp' (the message type is 0x101). These are the important ones for TURN.
You should see an error from the TURN server with a message type 0x113 and an error code 401 (unauthorized) because in the first packets, there is no username attribute. In response to those the browser will start sending allocate requests that contain both a username and a message-integrity.
If things go well, those should be answered with an allocate success response (message type=0x103) indicating a xor-relayed-address.
If not and you see more 401 errors that usually means your username and password is wrong.
You might also find the articles on using wireshark to reverse-engineer Amazon Mayday and Whatsapp on WebRTChacks useful -- both use Wireshark.
The WebRTC project has some notes on Wireshark, too.

scan the network for a server while using AsyncSocket as a client

My app communicates with an external server using AsyncSocket as a Client.
(the working code can be found here)
When the app starts, the user types in the IP address of the server computer.
assuming both iOS and server is sitting on the same subnet
Question: is there a way to "scan" the network for the server thus avoiding the user manual input for server IP ?
I can iterate the IPs one by one in a loop (10.0.1.x 10.0.1.x++)
yet it seems wrong and wasteful.
is there another more elegant way to do so?
I had an iOS project doing server discovery in the current (Wi-Fi) network. The typical solution is to use UDP broadcasting to ask for server info and then listen to a UDP response. As soon as you get the response with the server address you can establish connection using TCP sockets.
CocoaAsyncSocket is good enough for this. I used GCDAsyncUdpSocket and GCDAsyncSocket.
I understand you probably need more info on the topic. I'll try to extend the answer when I have time to.

UDP configuration in webserver to accept connection from GPRS mobile device

Could someone please help me to configure UDP in web server to accept connection from GPRS mobile device. The GPRS mobile device will send data packets which i need to write as a file in web server to process a web page. Please help.
i need to know the following:
1) How to configure UDP in web server
2) How can a GPRS mobile device get connected to the server using UDP
3) Write the data packets to a txt file
Please help.
A "web server" usually means am "http-server", which is a process listening for incoming "http"-requests.
Now http is a protocol that is standardized to be accessed via TCP/IP port 80.
Therefore most webserver implementations will implement a TCP/IP server that listens to port 80 by default. Usually you can change the port if need be, but usually you cannot change the transport layer to e.g. UDP.
Therefore it might not really be possible to make your "web-server" react on UDP packets.
If, however, by "web server" you mean a host accesible on the internet (that eventually might happen to run an http-daemon), you might have more luck: simply run a small server-application on that machine that listens to incoming UDP traffic on a given port, and do "something" with it (e.g. forward it to an http-daemon to display the current location)
Answers to your 3 questions:
Creating a small application listening on UDP-traffic:
a trivial server could be:
`netcat -l -u -p 8888`
This will create a UDP-server listening on port 8888, using netcat.
Whatever client connects to your server, everything that the client sends to the server will be output via stdout, and you can talk back to the client via stdin.
You shouldn't use netcat in a production environment; but it is a good and simple tool to check whether a connetion can be established and whether the protocol works.
In production, you will probably want to use a "real" UDP-server, e.g. by writing your own in the language of your choice.
How to make your gprs device talk to your server? This is impossible to answer without knowing more about the device, it's OS and the software installed that produces the data and is supposed to send it to the.
netcat -l -u -p 8888 > myfile.txt

How do all the requests connect to a web-server using the same port?

How does a web-server serve its client using the same port(80) for a TCP connection. For a UDP connection, i understand that there is no connection, per se, so we can have multiple clients send packets to same port. If i try to use an already used port on my localhost, i get BindException.
One solution i see to this is starting a thread for each connection, but wouldnt this be cumbersome for site like google/yahoo where there a >100000 connections in each server?
What solutions do web servers employ for this problem?
Server listens on a well-known port (80) and delegate the request to a worker socket once it receive the request. That way it can serve the next request. You can write your own simple server to understand whats going on. Oracle site has a nice example code. [1]
[1] http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServer.java
first it creates a server socket;
ServerSocket ss = new ServerSocket(port);
then it listnes on the specified port and create a new socket once it accepts the request;
Socket s = ss.accept();
As shown in the code, it has a worker thread pool, so at a given moment you can control the number of request get served by the server at a given time. Others wait in a Queue may be.
You only have one port for listening, but a connection has two ports, one on each side of the connection. This pare must be unique.
So, say you connect to google.com port 80, then your connection will have some port on your machine, say 42312 and port 80 at google.com. You can see your connections with netstat -a. To get a shorter list: netstat -an| grep ESTABLISHED" Which shows all established connections without resolving their IPs to names.
AFAIK, Apache will start a new thread for every request, which is a big reason that event driven servers like Node.js are a little faster. Google and Yahoo also have TONS of servers and spread this large processing load among them. What Roger says also makes sense, although I'm not 100% sure on the details of how exactly google doing output on port 42312 would reach your computer at port 80 :P