transfer the data from one system to another through ip address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 6302768590
    New Member
    • May 2024
    • 1

    transfer the data from one system to another through ip address

    Hai team
    i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
  • Percepticon77
    New Member
    • May 2024
    • 4

    #2
    Here's a simple TCP, JSON example that sends data every 5 minutes.

    GetUpdatedMetho d() will fetch the latest data. Timer is set to trigger at every 5 minutes. When the timer ticks, connect to the other system via TCP, serialize the updated data and send it. Wrap the transmission code in a try-catch to catch errors.


    Here is sample code:
    Code:
    using System;
    using System.Timers;
    using System.Net.Sockets;
    using System.Text.Json;
    
    // ... (Your GetUpdatedData() method would be here)
    
    Timer timer = new Timer(300000); // 5 minutes in milliseconds
    timer.Elapsed += OnTimedEvent;
    timer.AutoReset = true;
    timer.Enabled = true;
    
    void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        string updatedData = GetUpdatedData(); // Get your updated data
    
        try
        {
            using TcpClient client = new TcpClient("OtherSystemIPAddress", 12345); // Replace with actual IP and port
            using NetworkStream stream = client.GetStream();
    
            string jsonData = JsonSerializer.Serialize(updatedData);
            byte[] data = System.Text.Encoding.ASCII.GetBytes(jsonData);
            stream.Write(data, 0, data.Length);
        }
        catch (Exception ex)
        {
            // Handle exceptions (log, retry, etc.)
            Console.WriteLine("Error sending data: " + ex.Message);
        }
    }

    Comment

    Working...