How to create connection with tcp server - iphone

I need to create a TCP Connection with a server running on device , I need to send the binary data after connection in following format.
Field Byte Value
1: packet id 1 0X01
2: length 1 2
3:Buadrate 4 The bit rate in bps used by the can
BUS. Maximum value is 1000000.
4:extended 1 If this is set to one the device will use
the extended frame format
Please help that how can create tcp connection with server running on 2000 port
Please help

I recommend that you look into using CocoaAsyncSocket which will ease some of it...
https://github.com/robbiehanson/CocoaAsyncSocket
https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro
When you have your socket setup you can send (writeData:) your data as a C struct, but you might need to have the right endianess for the Baudrate. Check that.

Related

Labview - Check if TCP read buffer contains more data

I've got a TCP Server that processes messages of the following structure:
[ Msg. Size (2 Byte) | Msg. Payload (N Byte) ]
The process is as follows:
Read 2 bytes from the TCP connection to identify payload size N.
Read N payload bytes and do something with it.
Close TCP connection.
To reduce networking overhead I'd like to piggyback multiple messages.
[ Msg. Size #1 | Msg. Payload #1 ][ Msg. Size #2 | Msg. Payload #2 ] ...
Obviously the processing loop must not close the TCP connection if the TCP read buffer contains more data (is not empty).
Is there any way to reliably check if more data is available in a TCP read buffer from within Labview 2013?
I could call read() again and check if it times out. But I'd like to avoid this solution since it introduces unwanted latencies.
In the processing loop described above standard Labview TCP VIs are used (e.g. TCP Wait On Listener, TCP Read, TCP Write, TCP Close Connection).
The client should shutdown the sending side of the connection as soon as it does not wish to send any more queries. The server should keep reading from the connection. If it detects that the other side has shut down the sending side, it can close the connection as soon as it has sent the final reply.
There is no need to wait for the read to timeout. A half-closed connection should be detected as soon as all data is read.
If for some reason you cannot support half-closed connections, you need some way to indicate the final request in the data that the server received. You can do this with a a special "I'm done" message. There are other ways.
By the way, you should not use the term "packet" to refer to application-level messages. You should use the term "message" to refer to an application-level unit of data that represents a single request or response.
You can wire a zero for the timeout time. Then you do not introduce unwarranted latencies.

how to reserve rtp port when making a sip INVITE request

I am Developing a VOIP softphone, I need to put RTP port number in SDP part in my INVITE Request. how can I find a free UDP port number to accept RTP packets.
I have found 2 solutions but don't know if they are correct way to do this.
Solution 1 : start from a UDP port number (say 7000) and see if its free , if not increase by 1 and continue until a free port is found. then open a UDP socket on that port , so that other calls can't choose my calls RTP port.
then send the request.
Solution 2 : start from a UDP port number (say 7000) and see if it's free, put it in SDP and send the request. but when I get OK response from other party (after a while), there is no guarantee that the port number I announced for RTP is still available. maybe other call has captured that.
I would like to know what is the best way to do this.
As AymericM suggested, you should stick to your solution 1.
You need to use the bind call to bind a socket to a port.
Additionally, the RTP specification states that the RTP port should typically be even, with the RTCP port being the rtp_port + 1.
For UDP and similar protocols,
RTP SHOULD use an even destination port number and the corresponding
RTCP stream SHOULD use the next higher (odd) destination port number.
Even in the case where you support RTP/RTCP multiplexing over a single port, the answerer might not, so it might be a good idea to bind both the RTP and RTCP ports when generating the offer.
So to summarise, try to bind two consecutive ports starting on an even number and once you've found two suitable ports, generate the offer/INVITE.
Solution 1 is the only way to reserve a port number within a specific range of port.
If you do not care about being close to a specific port number, just open a port with value 0 in order to get a random port which will of course be free. Then, retrieve the real opened port with socket's API and use it in your sdp!

how many tcp connection can be created

TCP uses port numbers to identify sending and receiving application end-points on a host, or Internet sockets. Each side of a TCP connection has an associated 16-bit unsigned port number (0-65535) reserved by the sending or receiving application
Now if we want to create tcp connection and keep it alive i cannot go more then 65535
What should be the best strategy to cross the limit 65k?
adding multiple interface can increase the possibility of creating more connection is there any other stategy
TCP requires that the tuple (server-ip, server-port, client-ip, client-port) is different for each connection. You can change any one of those to get a new connection. A different server-ip is OK, as is a different client-port. The two port ranges alone give you 2^16*2^16 ~ 4 billion connections.
Each side of a TCP connection has an associated 16-bit unsigned port number (0-65535) reserved by the sending or receiving application
No. (1) It is 1-65535, not 0-65535. (2) Client side ports are usually reserved by the operating system, not by the application. (3) There is no such thing as the 'sending or receiving application'. There are client and server applications.
If we want to create tcp connection and keep it alive i cannot go more then 65535
No again. If you want to create connections in a client you cannot create more than 65535 to the same target. if you want to accept connections in a server you can accept as many as you like, subject to the prior limit at the client end in each client.
What should be the best strategy to cross the limit 65k?
65535 is 64k-1, not 65k, and there really isn't any such limit except as above, which isn't any kind of limit in practice. You don't need 64k client connections to the same target.
Just because a port number is limited to 64K value does not mean you are limited to 64K connections maximum. You can connect to the same port on different servers (think of how many websites you visit at a time, they all listen on port 80 or 443), and you can reuse the same local port for multiple connections as long as they are connected to different servers. It is the combination of [LocalIP:LocalPort]+[RemoteIP:RemotePort] that uniquely identifies a TCP connection, so that gives you flexibility to tweak those values to allow more connections.

Does TCP keepalive refresh the timeout on a NAT?

I've read that NAT routers "assume a connection has been terminated if no data has been sent for a certain time period."
I've also read that TCP keepalive packets usually shouldn't contain any data.
So my questions are:
Are the above statements true?
Do NAT routers consider empty TCP keepalive packets when reordering/cleaning their tables?
I'm asking this because I need a reliable connection between two endpoints where both of them have to be able to detect and react to connection problems. I know that I might just implement a keepalive mechanism myself but I want to know whether the TCP implementation could be used for that.
I do believe the second statement refers to payload (The shortest possible TCP/IP packet is 40 bytes long - 20 bytes for TCP header + 20 bytes for IPv4 header).
Regarding the first, here's a quote from RFC 2663:
End of session for TCP, UDP and others
The end of a TCP session is detected when FIN is acknowledged by
both halves of the session or when either half receives a segment with
the RST bit in TCP flags field. However, because it is impossible for
a NAT device to know whether the packets it sees will actually be
delivered to the destination [...] the NAT device cannot safely assume
that the segments containing FINs or SYNs will be the last packets of
the session [...] Consequently, a session can be assumed to have been
terminated only after a period of 4 minutes subsequent to this
detection. The need for this extended wait period is described in RFC
793 [Ref 7], which suggests a TIME-WAIT duration of 2 * MSL (Maximum
Segment Lifetime) or 4 minutes.
Reference: https://www.rfc-editor.org/rfc/rfc2663
To my understanding, any packets that identifies a session would reset the TTL counter - but that depends heavily on implementation, since 'data' can be understood as 'packet' (minimum 40 bytes) or 'packet payload'. Nonetheless, #CodeCaster is spot-on; never assume that a connection is alive, make sure it is before sending (and, if possible and depending on criticality, acknowledge receipt.)

TCP-Connection Establishment = How to measure time based on Ping RRT?

I would be greatful for help, understanding how long it takes to establish a TCP connection when I have the Ping RoundTripTip:
According to Wikipedia a TCP Connection will be established in three steps:
1.SYN-SENT (=>CLIENT TO SERVER)
2.SYN/ACK-RECEIVED (=>SERVER TO CLIENT)
3.ACK-SENT (=>CLIENT TO SERVER)
My Questions:
Is it correct, that the third transmission (ACK-SENT) will not yet carry any payload (my data) but is only used for the connection establishement.(This leads to the conclusion, that the fourth packt will be the first packt to hold any payload....)
Is it correct to assume, that when my Ping RoundTripTime is 20 milliseconds, that in the example given above, the TCP Connection establishment would at least require 30 millisecons, before any data can be transmitted between the Client and Server?
Thank you very much
Tom
Those things are basically correct, though #2 assumes that the round-trip time is symmetric.
To measure this, called the "Time to Syn/ACK" (which is NOT the Time to Establish a connection - the connection is only half-open when in that state, you need the 3rd packet acknowledging the establishment to consider it established), you usually need professional tools that include their own TCP stack, enabling that kind of measurement. The most used one is called the Spirent Avalanche, but you also have Ixia's IxLoad or BreakingPoint Systems box (BPS has now been acquired by Ixia btw).
Note that, yes, 3rd packet won't have any data, and that is also true of the first two. They are only Syn and Syn+Ack flagged (those are TCP flags), and contain no application data. This initial exchange, called the Three-way Handshake therefore causes some overhead, which is why TCP is typically not used in real-time applications (voice, live video, etc..).
Also as stated, you can't assume that Latency = RTT/2. It is in fact very complicated to measure one-way latency above layer 3 (IP) - and you are already at layer 4 (TCP) here. This blog post covers in details the challenge of this: http://synsynack.wordpress.com/2012/04/09/realistic-latency-measurement-in-the-application-layers/