Establishing Communicate Between Executable Files in TCP IP

Establishing communication between executable files using TCP/IP involves creating a client-server architecture where one executable acts as the server and listens for incoming connections, while the other acts as the client and initiates a connection to the server. Here’s a basic example in C#:

Server (Receiver)

The server executable will wait for incoming connections and receive data from the client.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class TCPServer
{
static void Main(string[] args)
{

// Specify the IP address and port to listen on
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
int port = 8888;

// Create a TCP listener
TcpListener listener = new TcpListener(ipAddress, port);

// Start listening for incoming connection requests
listener.Start();
Console.WriteLine("Server started. Waiting for connections...");

// Accept the client connection
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client connected.");

// Get the client's network stream for reading and writing
NetworkStream stream = client.GetStream();

// Receive data from the client
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine("Data received from client: " + dataReceived);

// Close the connection
client.Close();
listener.Stop();
}
}

Client (Sender)

The client executable will establish a connection to the server and send data.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class TCPClient
{
static void Main(string[] args)
{

// Specify the server's IP address and port
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int serverPort = 8888;

// Create a TCP client
TcpClient client = new TcpClient();

// Connect to the server
client.Connect(serverIP, serverPort);
Console.WriteLine("Connected to server.");

// Get the client's network stream for reading and writing
NetworkStream stream = client.GetStream();

// Send data to the server
string message = "Hello from client!";
byte[] data = Encoding.UTF8.GetBytes(message);
stream.Write(data, 0, data.Length);
Console.WriteLine("Data sent to server: " + message);

// Close the connection
client.Close();
}
}

Running the Example

1. Compile and run the server executable.
2. Compile and run the client executable.

You should see the server output indicating that it received data from the client.

This example demonstrates a basic TCP/IP communication setup between two executable files written in C#. You can extend this example by implementing more complex communication protocols, error handling, and data serialization as needed for your specific application.