/Apartment-Communication

Apartment communication with tcp/ip

Primary LanguageC#

Apartment-Communication

1-Introduction

The project is an apartment management system that allows residents to communicate with each other through a chat function and access real-time information about exchange rates and weather. The system is implemented using a client-server architecture, where each resident's computer runs the client program and connects to a central server. The server is responsible for managing the chat communication between clients and for retrieving and broadcasting exchange rate and weather data to all connected clients. The client program provides a user-friendly interface for residents to view the chat messages, exchange rate and weather data, and also includes a feature for sending messages to other residents. The system is designed to be efficient, reliable, and easy to use, making it a valuable tool for enhancing the communication and information-sharing among the members of the apartment complex.

2-Technical Details of the Project

The project is developed using C# programming language and makes use of several libraries such as .NET Framework, System.Net, System.Text and Newtonsoft.Json. The client-server architecture is implemented using the TcpListener and TcpClient classes from the System.Net.Sockets namespace, which provide the necessary functionality for creating and managing socket connections. The communication between the client and server is based on the Transmission Control Protocol (TCP) which ensures that data is transmitted reliably and in order. In terms of the threading model, the server uses multiple threads to handle incoming client connections and to broadcast data to all connected clients. The timer class is used to periodically retrieve weather and currency data from the respective APIs and broadcast the data to all connected clients. The data exchange between the client and server is done by sending and receiving data in the form of byte arrays, which are then converted to and from strings using the ASCII encoding. The weather and currency data are retrieved from the APIs in the form of JSON strings and parsed using the Newtonsoft.Json library and JObject class. The user interface of the client program is implemented using Windows Forms, which allows for the creation of a graphical user interface with various controls such as text boxes, labels, buttons, and list boxes. The interface is designed to be simple and easy to use, providing clear and intuitive navigation for the residents.

3-Networking

The project utilizes networking concepts to enable communication between the client and server. The client-server architecture is based on the Transmission Control Protocol (TCP), which is a reliable and widely used protocol for network communication. The TcpListener and TcpClient classes from the System.Net.Sockets namespace are used to create and manage socket connections between the client and server. The server uses the TcpListener class to listen for incoming client connections on a specific port and IP address. Once a client connects, the server uses the TcpClient class to create a new socket for that connection and assigns a new thread to handle the communication with that client. The client uses the TcpClient class to connect to the server on a specific IP address and port. Once the connection is established, the client can use the socket to send and receive data to and from the server. In addition, the project uses asynchronous programming to handle the communication between the client and server. This allows the program to continue executing other tasks while waiting for data to be received or sent, which improves the responsiveness of the system. Overall, the networking concepts used in the project enable the clients to connect to the server and exchange data in real-time, making it possible for the residents to communicate with each other and access the exchange rate and weather data through the chat program.

4-Data Exchange

The project utilizes data exchange to enable communication between the client and server, as well as to transmit exchange rate and weather data to clients. The data is exchanged in the form of byte arrays, which are converted to and from strings using the ASCII encoding. When a client connects to the server, it sends a "Welcome" message to the client, which is encoded to a byte array and sent through the socket connection. The client receives the byte array, decodes it back to a string, and displays the message in the user interface. When a client sends a message through the chat function, the message is encoded to a byte array and sent to the server. The server then broadcasts the message to all connected clients, who receive the byte array, decode it back to a string, and display the message in their user interface. In addition, the project uses HTTP GET requests to retrieve exchange rate and weather data from the APIs in the form of JSON strings. The server parses the JSON strings using the Newtonsoft.Json library and JObject class and then encodes the data to byte arrays and broadcast it to all connected clients. The clients receive the byte arrays, decode it back to the original JSON strings, and parse it to display the exchange rate and weather data in their user interface. Overall, the data exchange enables the communication and data transfer between the client and server, making it possible for the residents to communicate with each other and access the exchange rate and weather data through the chat program.

5-Threading

The project utilizes threading to handle multiple client connections and to broadcast data to all connected clients simultaneously. The threading model used in the project is based on the Thread class and ThreadStart delegate from the System.Threading namespace. When a client connects to the server, a new thread is created and assigned to handle the communication with that client. The thread is created using the Thread class and initialized with a ThreadStart delegate that points to the HandleConnection method of the ClientClass class. This method is responsible for managing the socket connection and for handling the communication with the client, including receiving and sending data through the socket. The server uses a while loop to continuously listen for new client connections and create new threads for each incoming connection. This allows the server to handle multiple client connections simultaneously and improves the scalability of the system. In addition, the project uses Timer class to periodically retrieve weather and currency data from the respective APIs and broadcast the data to all connected clients. The Timer class is initialized with a callback method that retrieves the weather and currency data and broadcasts it to all connected clients. The Timer class also allows to schedule the time intervals to execute the callback method. Overall, the threading model used in the project allows the server to handle multiple client connections simultaneously and improves the performance and responsiveness of the system by allowing multiple tasks to be executed concurrently.

6-Wheather and Currency Data

The project utilizes weather and currency data to provide real-time information to the residents of the apartment complex. The data is retrieved from two external APIs, the Weather API and the Free Currency API, using HTTP GET requests. The Weather API provides weather data such as temperature, humidity, and wind speed for a specific location. The project uses the WeatherAPI class, which takes the API key as an input and has a method called GetWeatherAsync() that sends a GET request to the Weather API and retrieves the weather data in the form of a JSON string. The JSON string is then parsed using the JsonSerializer class from the System.Text.Json namespace and the data is stored in a WeatherData object. The Free Currency API provides exchange rate data for different currencies. The project uses the HttpClient class from the System.Net.Http namespace to send a GET request to the Free Currency API and retrieve the exchange rate data in the form of a JSON string. The JSON string is then parsed using the JObject class from the Newtonsoft.Json.Linq namespace and the data is stored in a ExchangeRateData object. Both weather and currency data are broadcasted to all connected clients through the server. The clients receive the data in the form of byte arrays, decode them back to the original JSON strings, and parse them to display the weather and currency data in their user interface. Overall, the weather and currency data provide valuable information to the residents of the apartment complex, which can be used to plan their daily activities and make informed decisions.

7-User Interface

The project utilizes a user interface to provide a simple and intuitive way for the residents of the apartment complex to interact with the program. The user interface is based on the Windows Forms framework, which is a part of the Microsoft .NET Framework and provides a rich set of controls and visual elements for building graphical user interfaces. The user interface consists of several elements, including text boxes, labels, buttons, and list boxes. These elements are used to display information, such as the connection status, weather data, and exchange rate data, as well as to provide input controls, such as text boxes for entering messages and buttons for sending messages and connecting to the server. The user interface is designed to be simple and easy to use, with a clear layout and consistent visual style. The controls are positioned and sized to provide a clear and logical flow of information, and the font and colors are chosen to be easy to read and visually pleasing. Additionally, the project uses the Timer class to periodically refresh the weather and exchange rate data and refresh them on the user interface. The Timer class is initialized with a callback method that retrieves the weather and currency data and updates the respective elements on the user interface. Overall, the user interface is an important aspect of the project, as it provides a simple and intuitive way for the residents of the apartment complex to interact with the program and access the weather and exchange rate data.

8-Testing and Debugging

The project includes several steps for testing and debugging to ensure that the program is functioning correctly and to identify and fix any issues that may arise. Unit testing is a technique that is used to test individual units of code, such as methods or classes, in isolation from the rest of the program. This can be done using a testing framework such as NUnit, which provides a set of attributes and assertion methods to define and run tests. Unit tests can be used to test the individual methods of the classes in the project, such as the methods that handle the communication with the server and the methods that parse the weather and currency data. Debugging is the process of identifying and resolving errors or bugs in the program. This can be done using the built-in debugging tools provided by the development environment, such as Visual Studio's debugging tools, which include features such as breakpoints, watch windows, and the ability to step through the code. Debugging can also be done by adding logging statements in the code, which can be used to print the values of variables and the flow of the program during the execution. Manual testing is also an important step to validate that the application is working as expected, this can be done by a human tester by manually interacting with the program and testing different scenarios, such as sending messages, connecting to the server, and checking the weather and currency data. Overall, testing and debugging are important steps in the development process, as they help to ensure that the program is functioning correctly and to identify and fix any issues that may arise.

9-Conclusion

In conclusion, the apartment management system is a robust and feature-rich program that provides a simple and intuitive way for the residents of the apartment complex to interact with the program. The program is built using the C# programming language and the .NET Framework, and utilizes various features such as threading, networking, data exchange and user interface. The system uses weather and exchange rate data to provide real-time information to the residents, this data is retrieved from external APIs using HTTP GET requests and broadcasted to the clients. The program is designed to be easy to use and understand, with a clear layout and consistent visual style. The project includes steps for testing and debugging to ensure that the program is functioning correctly and to identify and fix any issues that may arise. Overall, this apartment management system is a powerful tool that will help the residents to stay informed, connected and engaged with the community.

10-References

• The .NET Framework, which is a software framework developed by Microsoft for building Windows applications. It includes a large library of pre-built classes and functionality that can be used to develop a wide range of applications, including those that use threading, networking, and user interface • The System.Net.Sockets namespace, which provides a managed implementation of the Berkeley Sockets interface for developers who need to create distributed applications. It allows the program to create and use TCP and UDP sockets. • The System.Net.Http namespace, which provides a programming interface for modern HTTP applications. It allows the program to send HTTP requests and receive responses, this is used to retrieve weather and exchange rate data from the external APIs. • The System.Text.Json namespace, which provides high-performance, low-allocating, and standards-compliant JSON APIs. It is used to parse and serialize the weather data from JSON to a WeatherData object and vice versa. • The Newtonsoft.Json.Linq namespace, which provides a way to work with JSON objects using LINQ to JSON. It is used to parse and access the exchange rate data from the JSON object. • The Windows Forms framework, which is a part of the Microsoft .NET Framework and provides a rich set of controls and visual elements for building graphical user interfaces. It is used to create the user interface of the program.