An Event-Driven Socket Library
The Importance of Computer and Communication Technologies
The exchange of data between computer systems plays an extremely important role in our interconnected world. Therefore we often speak of the combined computer- and communication technologies that should be mastered. In this chapter you learn how to handle the data exchange between two computer systems using the TCP/IP protocol which is used in all Internet connections, for example the Web and all streaming services (data clouds, voice, music, video streaming).
The socket programming is based on the client-server model, which has already been described in some details on the TcpJLib and BtLib sites. Most important and somewhat unexpected is the fact that the communication partners, the server and the client, are not completely symmetrical. Rather, first the server program must be started before the client program can engage a connection to it. In order to identify the two computers on the Internet, its IP address is used. In addition, server and client specify one of 65536 communication channels (IP ports), that is selected with a number in the range 0..65535.
When the server starts, it creates a server socket (like a electrical plug) that uses a particular port and goes in a wait state. We say that the server is "listening" for an incoming client, so the server is in the LISTENING state. The client creates a client socket (the plug counterpart) and tries to establish a communication link to the server using its IP address and port number.
The Easy Way of Socket Programming Using Objects and Events
The library TCPCom simplifies the socket programming essentially, since it describes the current state of the server and the client with state variables and considers the change of the variables to be caused by events. This programming model corresponds to the natural feeling of many people to describe the communication between two human partners. In the free download you find three versions of the library for Java SE, Android and Python and many demonstration examples. The Java SE and Python versions may also be used on the Raspberry Pi and the Java SE version on the Lego EV3 robot running leJOS.
As usual in a event-driven model, a callback function, here called stateChanged(state, msg) is invoked by the system, when an event is triggered. The Python library module is integrated into TigerJython, but can also be downloaded from this site to be used in a pure Python environment. The Java library module is distributed as JAR archive.
When the server is started, it creates a TCPServer object specifying the ports and the callback function onStateChanged() and embarks in the LISTENING state.
The client starts with the creation of a TCPClient object specifying the IP address of the server, the port and the callback function onStateChanged(). By invoking connect() it starts a connection trial.
The call to connect() is blocking, which means that the function returns with True / true once the connection has succeeded, or with False / false after a certain timeout period (approximately 10 seconds) if the connection fails. The information about the success or failure of the connection can also be detected via the callback.
You can try out the client-server programs on the same PC by starting two TigerJython windows. In this case you choose the host address localhost. Using two different computers for the client and the server is more close to reality. They must be connected with a network cable or via wireless LAN and the link must be open for TCP/IP communication with the selected port. If the connection fails with your normal hotspot (WLAN access point), this is mostly due to firewall restrictions. In this case you can use your own router or start a mobile host spot app on your smartphone. Access of the mobile phone to the Internet is not necessary.
A First Glance: The Time-Server/Time-Client Scenario
To demonstrate how simple it is to write a client-server application using tcpcom, your first programming duty is to create a server that provides a time service. When a client logs on, it sends the current time (with date) back to the client. There are numerous such time servers on the Internet and you can be proud that you are already in a position to code such a professional server application.
In order to turn off the time server, we use a well-known trick: The main program "hangs" in a modal message dialog opened by the blocking function. When the function returns by pressing the OK or clicking the close button, the server is stopped by calling terminate(). (In Python we use the easygui module that is much simpler than Tkinter. It can be downloaded from the Internet.)
In Java we use JOptionPane to create a modal message dialog. Unfortunately it is not so easy to catch or inhibit the close button click. So do not use it.
Execute the program locally using WebStart
Execute the program locally using WebStart