To accomplish this task, you will need to use the Socket class in C#. The Socket class provides a way to create, connect, send, and receive data over a network.
Here are the steps to create a simple Chat Application:
Step 1: Create two Windows Forms
Create two Windows Forms; one form is for the sender and the other is for the receiver.
Step 2: Design the Forms
Design the forms by adding controls such as text boxes, buttons, and labels to enable users to enter their messages and send them.
Step 3: Add Code to the Forms
Add code to the forms to create a socket, connect to the server, and send and receive messages. Here's an example of how to create a socket and connect to a server:
using System.Net.Sockets;
using System.Text;
// Create a socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the server
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint endPoint = new IPEndPoint(ipAddress, 1234);
socket.Connect(endPoint);
Step 4: Send and Receive Messages
Once the socket is connected, you can send and receive messages. Here's an example of how to send a message:
string message = "Hello, world!";
byte[] messageBuffer = Encoding.ASCII.GetBytes(message);
socket.Send(messageBuffer);
To receive a message, you can use the Receive method:
byte[] buffer = new byte[1024];
int bytesRead = socket.Receive(buffer);
string message = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Step 5: Test the Application
Run the sender and receiver forms and test the application by sending messages from one form to the other.
Note: In order to make the application work in an offline environment where users can send and receive messages simultaneously, you will need to use a peer-to-peer network architecture. This is beyond the scope of this tutorial, but there are many resources available online to help you get started with peer-to-peer networking in C#.