• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Good TCP/IP resources

tatteredpotato

Diamond Member
I've been tasked with implementing Ethernet on a project and I'm looking for some good TCP/IP resources out there. Most of what I've found insofar is a very vague overview, but I need some more details on implementation.
 
TCP/IP and Ethernet are two different layers, are you implementing both?

I'm supposed to implement the Modbus TCP/IP protocol which sits at the application layer, so I need Ethernet/IPv4/TCP for this application.

I also need to find a good hardware Ethernet controller... the demo board I'm using has a Davicom DM9000A which I would like to stick with for the production board since I can test it with a working reference design.
 
So you are implementing your own version of the Modbus protocol then? Or is there an API that you are restricted to?

Are you writing this in C or in Java?
 
http://lwip.wikia.com/wiki/LwIP_Wiki

I've used lightweight IP to provide TCP/IP support to small operating systems. It's self-contained, you just need to write an ethernet card driver, and on the other end there is a nice API for writing network applications. If you don't have or want to use threading, then you just need to call certain timer functions at defined intervals. There's examples on the wiki.
 
Yea... It's really quite the jump from RS232, but I'll probably give the library dinkumthinkum linked to a shot as long as I can do so (legally speaking).

If I'm understanding how this library works, then I don't need a RTOS to run it. That would be good for the current system, however I might look into loading some OS in the future anyways.
 
Are you implementing Modbus TCP/IP over Ethernet, or using Modbus TCP/IP over Ethernet?

Current play is implementing Modbus TCP/IP.... When I say implement I mean I am going to write the C code that handles the raw bytes of a Modbus message and constructs the raw bytes that is the response. The actual TCP/IP I can go with a library for, but I've done Modbus RTU/ASCII already, so if I have the whole IP stack done implementing Modbus TCP/IP won't be too hard IMO.

Also the main point of this thread was concerning the TCP/IP implementation. Assuming I can legally use an already available library like the one previously mentioned, I shouldn't have too much trouble with the Modbus side of things.
 
Last edited:
Also good call on LWIP, it seems to be the network stack used for the demo projects on this board. Essentially meaning it's definitely compatible and works with my hardware.
 
I've been reading up on the different protocols involved, and I've become curious about what mechanic determines what application gets the traffic? Lets say it's HTML data over HTTP; using TCP port 80 ensures that the traffic is interpreted as HTTP, but lets say you have two different web browsers open, both communicating over port 80. How is it determined which browsers get the packets?
 
I've been reading up on the different protocols involved, and I've become curious about what mechanic determines what application gets the traffic? Lets say it's HTML data over HTTP; using TCP port 80 ensures that the traffic is interpreted as HTTP, but lets say you have two different web browsers open, both communicating over port 80. How is it determined which browsers get the packets?

Well each browser connecting to a page would open its own connection and, as such, have its own file descriptor that the client/server write to.

Additionally, each packet's sequence number will be virtually unique at any given time. The sequence number is determined by a combination of a rand() function, the time sent, and some other variables.

-Kevin
 
I've been reading up on the different protocols involved, and I've become curious about what mechanic determines what application gets the traffic? Lets say it's HTML data over HTTP; using TCP port 80 ensures that the traffic is interpreted as HTTP, but lets say you have two different web browsers open, both communicating over port 80. How is it determined which browsers get the packets?

Browsers don't use 80. Let me explain in degibson-detail.

Only one process can bind to a port. That port then belongs to that one process until the process unbinds or dies. There are a set of well-known ports that certain services (e.g., web servers, ssh servers, smtp servers, etc.) will almost always use (e.g., ports 80, 22, and 25, respectively). This is a convention in place so it becomes easy to 'look for' those services, given an IP. E.g., on IP 1.2.3.4, the web server on that node is probably bound to 80.

TCP connections have two endpoints. Each endpoint is an IP/port pair. As we've just established, servers have known IPs and, usually, known ports. 1.2.3.4:80 in our previous example. Clients on the other hand, don't need to have known ports, because no other entity is usually trying to connect to a client (usually, clients connect to servers).

So, when a browser starts, it generally doesn't care which port it uses. It is customary for client applications to use a port >1024, and its generally wise to avoid ports that belong to some other service (IANA has a list here http://www.iana.org/assignments/port-numbers).

A browser will typically bind to the client IP at some high-numbered port. E.g., my Chrome session to look up the IANA port numbers can be seen in 'netstat':

Code:
C:\>netstat

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    hms-clover:1041        localhost:27015        ESTABLISHED
  TCP    hms-clover:27015       localhost:1041         ESTABLISHED
 ...
  [b]TCP    hms-clover:1146        www.iana.org:http      ESTABLISHED[/b]

In the case above, that Chrome process (consisting of one tab in chrome) used port 1146. It connected to www.iana.org:http -- netstat opportunistically does reverse-DNS (hence why it shows iana.org instead of the IP) and routinely translates real port numbers to service names (i.e., www.iana.org:http = 192.0.32.8:80) Most browsers pick ports to use opportunistically -- i.e., they just find any old available port and use it for the connection.

The server, upon seeing a connect request from a client, knows how to respond to that client, because the TCP header encode the port number the client is using.

One last thing: port numbers are by convention only. Its possible to be nefarious and send non-HTTP traffic on 80, or non-ssh traffic on 22.
 
Last edited:
Thanks degibson... I did pretty much the same thing you did (opened iana.org in chrome), however I get 6 connections to the iana.org server reported via netstat. Are these for different web services provided by the site?
 
Thanks degibson... I did pretty much the same thing you did (opened iana.org in chrome), however I get 6 connections to the iana.org server reported via netstat. Are these for different web services provided by the site?

It is customary for high-performance browsers to use multiple connections for multiple page elements. E.g., one connection is used to get the HTML itself, another used for each image, etc. -- the idea is to do as many transfers as possible in parallel. HTML transfer requires that each of those be different HTML transactions... so if you want to do them concurrently (for performance) you need multiple connections.

The reason you only saw one connection on my netstat dump is because I first hit google, then went straight to the list (skipping the www.iana.org frontpage, and therefore bypassing the images, etc.).
 
It is customary for high-performance browsers to use multiple connections for multiple page elements. E.g., one connection is used to get the HTML itself, another used for each image, etc. -- the idea is to do as many transfers as possible in parallel. HTML transfer requires that each of those be different HTML transactions... so if you want to do them concurrently (for performance) you need multiple connections.

The reason you only saw one connection on my netstat dump is because I first hit google, then went straight to the list (skipping the www.iana.org frontpage, and therefore bypassing the images, etc.).

Interesting... thanks again, that was a lot better explanation/simpler than I was expecting.
 
It is important to note that with the use of NAT and Firewalls there are times when a reverse connection is necessary (Unless using Hole-Punching or another method) in which the server connects to the client (ie: P2P Traffic).

IPv6 is supposed to solve much of this, though I doubt people get rid of NAT even when IPv6 becomes widespread.
 
Back
Top