Network communication and server-side technologies


Events are actions that can be detected by your script. They are typically triggered by user interaction, such as clicking a button or moving the mouse, but they can also be triggered by other events, such as page loading or receiving data from a server.


To handle events in JavaScript, you can use event listeners. An event listener is a function that is executed in response to a specific event. For example, you can use an event listener to respond to a user clicking a button by displaying an alert message or by performing some other action.

Bridges


Bridges are an essential part of the history of communication systems. In the early days of Ethernet networks, computers were connected via a coaxial cable that allowed all computers on the local area network (LAN) to communicate with each other. Each computer had to listen to the cable to determine if it was free to send a message to all computers on the LAN. This allowed the message to reach not only the intended recipient, but all computers on the LAN. To determine the recipient of a message, the destination MAC address was used, which was unique on each network card.



However, connecting all computers via a single cable led to a major problem: collisions. If two or more computers were transmitting on the LAN at the same time, a collision would occur and none of the packets would reach the intended recipient. To get around this problem, bridges were introduced. Bridges segmented and divided the network into multiple network segments. They could tell which network segment the recipient of a message was on by the destination MAC address, and forward the message only to that segment.



The operation of a bridge was accomplished by building and maintaining a forwarding address table. This table contained the information about the sender addresses and the associated source ports. When a bridge received a message, it entered the sender address and source port into the table to facilitate the forwarding of a possible response. When an address stopped sending packets for a period of time, it was deleted from the table to ensure that the list was up-to-date.


The use of bridges effectively avoided collisions by forwarding messages only to the relevant network segments. Bridges formed an important basis for the development of modern network infrastructures. Today, however, switches are used as an evolution of bridges because they provide the ability to connect more than two LANs and enable micro-segmentation to avoid collisions and their effects. Nevertheless, the role of bridges in the history of communication systems remains as an important milestone in network technology.



Switches


Switches are multiport bridges that filter and forward frames based on their MAC addresses. They enable efficient communication in networks by eliminating collision domains through micro-segmentation. Switches form a broadcast domain together with the end systems and increase throughput by passing multiple frames in parallel.



Characteristics of switches are as follows:




Communication modell


Client-server communication involves the exchange of messages between two computers or a (web) server. To describe the network connection, a 5-tuple is used. This contains the protocol used (TCP), the IP addresses of the local host and the foreign host, and the ports of the local and foreign process.



The IP address identifies the respective host, while the port refers to the associated application. In addition to communication, database queries and application systems can be used on the server side. On the client side, other technologies are often used, such as JavaScript and frameworks like jQuery. Furthermore, Java applets or native apps in Java or SWIFT for Android/Google can be used. There are also hybrid and web apps that are programmed on the server side and can load native code on the device as well as generate server-side JavaScript code for the client browser.


Java Applets


Java applets are a technology that creates interactive graphical user interfaces (GUI) on the client side of web pages. They are based on the Java API and provide a platform-independent solution for developing web applications. However, Java applets are becoming less popular today due to security concerns and potential risks associated with insecure code and access to the user's file system. Modern web technologies have largely replaced the use of Java applets and offer more secure alternatives for creating interactive content on the web.





Risks of Java applets are the following:



Server-sided communication methods


Server-side technologies delegate tasks to external components and enable communication between servers and clients in web applications. By forwarding an HTTP request based on the URL, they generate a response that is sent to the client. This type of task distribution plays an important role in the development of efficient and flexible web applications.





Examples of this are:



Common Gateway Interface


As a Server-sided communication model the CGI (Common Gateway Interface) was developed in the mid-1990s to go beyond simple retrieval of static documents. It enabled the connection of databases and the exchange of data, forms and images through the use of programs. In this process, the web server forwarded the request to the corresponding CGI program and received a response back. Data was exchanged using pipes and environment variables. Although CGI offered the advantage that applications could be created with arbitrary programming languages, it had limitations such as the need to start a new process instance for each call. It also had a high security risk due to the possibility that anonymous users could execute programs on the server. For these reasons, CGI is no longer used today due to security and performance issues.



PHP


PHP stands for "Hypertext Preprocessor" and is a widely used scripting language for developing dynamic websites. This website we are on right now is also built with PHP. Now follows the information about PHP and its advantages and disadvantages.



PHP is a scripting language that can be embedded directly into HTML. It is supported by most WWW servers and is available on both Windows and Linux. With PHP, developers have access to wide availability, a relatively easy-to-learn scripting language, and a rich feature set, including access to databases.



The way a PHP request works begins with the user requesting a web page through the browser. If the file ends in .php, .php3 or .phtml, the request is forwarded to the PHP server. This reads the required data from the MySQL server and packages it into HTML, which is then returned to the browser via the web server. With the help of the PHP function phpinfo(), all environment variables can be displayed, some of which are particularly important.



Advantages:




Disadvantages:


  1. Interpretation leads to slower execution for large server-side computations (JIT compilation reduces this drawback)


The PHP function phpinfo() displays all important environment variables:



Java Servlets & JSP


Java servlets are a further development of CGI and are based on the Java programming language. They allow the creation of stateless Java classes that are addressed via URLs and instantiated and executed by the web server. The servlets return the content to the web browser that made the request. In addition, Java Server Pages (JSP) are extensions to servlets that allow the creation of web pages that can mix both static and dynamic content.



Saving Objects


To store created objects or newly created states of existing objects for future access, various storage options are available. The two most common options are serialization and storage in text files. Both serialization and storage in text files provide the ability to store and later retrieve the state of objects. This allows effective reuse and re-access to important data and information.



Text files: Data for access by other programs must be written to a text file with field separators. Different techniques are possible, regardless of whether the data is stored as bytes or in normal word form.


Serialization


Serialization is the process of converting an object into a byte stream. This makes it possible to store the object or transfer it to a database or file. Storing objects is used to keep created objects or states of existing objects for later access. A common method for this is to use text files with field separators. This stores the data in a structured form so that it can be easily read and processed by other programs.




There are several techniques for storing objects in text files. One can either represent the data in the form of bytes or as normal words. This flexibility makes it possible to choose the appropriate method depending on the requirements of the project.



Writing the object data to a text file with field separators allows access from other programs. This allows the stored data to be retrieved and used later. This method is a common practice in data processing and provides an easy way to share information between different applications.



The choice of the appropriate technique for storing objects in text files depends on several factors, such as the type of data, the size of the data set, and compatibility with other systems. It is important to carefully analyze the requirements and select the appropriate method accordingly to ensure efficient and smooth data processing.


Deserialization


To read a serialized object again, the process of deserialization is used. This involves five steps in which a file is read to create a new object. The goal here is for the newly created object to have the same state as the original serialized object at the time of serialization. However, there is a difference for transient variables, which are returned with either null (for object references) or default values (for primitive data types).




For deserialization, a FileInputStream and an ObjectInputStream are needed to read the serialized data. The process begins by reading the object from the stream and then classifying it. It is important to note that exceptions may be thrown if the Java Virtual Machine cannot find or load the class. Similarly, an exception is thrown if an attempt is made to read more objects than are present in the stream. To handle such problems, a try-catch block is often used.



When reading out multiple objects, the "first in - first out" principle is followed. This means that the objects are read in the order in which they are present in the stream. Serialized objects can be successfully recovered by deserialization.


Write a string to a text file


When writing a string, a similar approach is used as when writing an object. This allows data to be exchanged between a Java program and a non-Java program. Instead of the ObjectOutputStream, a FileWriter is used. By calling the write() method on the FileWriter object, the data can be written to a specified text file. A concrete example would be to use the fileWriter.write("Text to be in the text file") command. This allows the desired text content to be written to the file, allowing communication between different programs.



Java.io


The java.io.File class allows you to work with files on the disk. It represents a file and provides functions for creating, checking and deleting files. However, the class itself does not contain the actual contents of the file, but is mainly used to manage file paths and properties.o



The File-class can be used to perform file operations, such as creating new files, verifying the existence of a file, or deleting files. It provides a variety of methods to perform these tasks and manage file state.



The File class itself is not used to read or write file contents. It is used for managing files and directories on the file system.


Buffer


Using a buffer when writing strings to a file avoids bottlenecks because the strings are collected in the buffer before being passed to the FileWriter. The flush() method allows data to be sent from the buffer ahead of time, if necessary. This allows efficient and uninterrupted data transfer.



Here is how a buffer is implemented:




Read from text files


To read text from a file, one uses a FileReader and a BufferedReader. To output the read text, a while loop is used that terminates when the end of the text is reached. In each iteration of the loop, one line is read into a string and compared to zero. If the line is not zero, it is output. If the line is null, that is empty, the while loop is terminated.



Reader and Writer



Input via character streams from the class:




Output via character streams in the Writer class:




These are just a few examples of commonly used commands.



For more information on the Reader and Writer classes, and the buffered variants of them, here's a link to the Scalingbits website, from our old lecturer in Java programming:



http://www.scalingbits.com/java/javakurs2/programmieren2/streams/readerwriter


Network programming via sockets


TCP socket communication allows you to connect to the outside world. It abstracts away low-level network details, allowing developers to focus on communication. Architecture of a chat program where a server and multiple clients interact with each other can thus be created. By sending and receiving messages, they can communicate with each other. An important technique used in this process is threads, which work in parallel and enable efficient communication. In this process, each client and the server can work in a separate thread. By using sockets, threads and the Java.net library, developers have powerful tools at their disposal to develop network applications and realize a wide range of communication possibilities over the network.



Socket connections


To get a client running, three basic functions will be built:





The challenge is to do two things at the same time, i.e. send and receive at the same time.




To establish the connection between two machines that know how to communicate with each other, sockets are used. The prerequisite for a successful connection is that both pieces of software know each other and know how to communicate with each other. So both machines have information about each other. These are the Ip-address in the network and the -TCP port. The IP address determines the receiver (here server) and the port number represents a logical connection with a specific software on the server. Caution, the TCP port numbers from 0-1023 are reserved! (http://www.iana.org/assignments/port-numbers)



The TCP ports are used to let the servers know which one the client wants to connect to, so they can access the desired protocols and read the messages.



To send messages to the server you also use a socket. With the help of the PrintWriter class you can write something string by string. For this you use the two methods print() and println().



Buffered readers are used to read the I/O streams.



It doesn't matter if the underlying connection stream is connected to a file or a socket.



Finally, a socket connection to the server is created by using the IP address and the port number of a socket connection. The BufferedReader is used for reading.



Only two sockets are needed to write a server application. The server socket waits for client requests. And another socket that is used for communication with the client.


Writing a Chat Client


Basically, a chat system consists of server-side and client-side code.



The technical procedure is as follows:






In addition, a text field is needed that displays the chat history, i.e. the received and sent messages, and a button that can be used to send the messages.



A button listener is also needed for this, through which the message is sent.



Threads are needed so that the message can be read as soon as it is sent, and they do not depend on a repeated query. This way the end result is more interactive.