RESPONSE-CHAT.GPT/DEEP.SEEK
RESPONSE-CHAT.GPT/DEEP.SEEK

REQUEST)
거래소 주식 TCP 데이타는 회원사들이 100% 일방적으로 수신만 합니다. 거래소는 일방적으로 송신만 합니다. 그럼 회원사들이 사용하는 수신TCP프로그램을 작성하고 싶습니다. 데이타의 구분자는 3개의 딜리미터로 구분됩니다. 0x0a,0a0d,0xff입니다. c#으로 회원사 수신프로그램을 작성해주세요

RESPONSE-CHAT.GPT)

using System;
using System.Net;
u

sing System.Net.Sockets;

using System.Text;
using System.Threading;

class TcpServer
{
    private const int Port = 12345; // 서버 포트
    private TcpListener tcpListener;
    private bool isRunning;

    public TcpServer()
    {
        tcpListener = new TcpListener(IPAddress.Any, Port);
        isRunning = true;
    }

    public void Start()
    {
        tcpListener.Start();
        Console.WriteLine("TCP 서버가 시작되었습니다.");

        while (isRunning)
        {
            try
            {
                // 클라이언트 연결 대기
                var tcpClient = tcpListener.AcceptTcpClient();
                Console.WriteLine("클라이언트 연결됨.");

                // 클라이언트에서 데이터를 수신하는 작업 수행
                Thread clientThread = new Thread(() => HandleClient(tcpClient));
                clientThread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"오류: {ex.Message}");
            }
        }
    }

    private void HandleClient(TcpClient tcpClient)
    {
        NetworkStream stream = tcpClient.GetStream();
        byte[] buffer = new byte[1024];
        StringBuilder sb = new StringBuilder();

        while (true)
        {
            try
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    break; // 연결 종료
                }

                // 받은 데이터를 문자열로 변환
                sb.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));

                // 딜리미터에 맞춰 데이터 분리
                string data = sb.ToString();
                string[] messages = SplitByDelimiters(data);

                foreach (string message in messages)
                {
                    if (!string.IsNullOrEmpty(message))
                    {
                        Console.WriteLine($"수신된 메시지: {message}");
                    }
                }

                // 남은 데이터를 다시 설정
                sb.Clear();
                sb.Append(data.Substring(data.LastIndexOf(messages[messages.Length - 1]) + messages[messages.Length - 1].Length));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"오류: {ex.Message}");
                break;
            }
        }

        tcpClient.Close();
        Console.WriteLine("클라이언트 연결 종료.");
    }

    private string[] SplitByDelimiters(string input)
    {
        // 3개의 딜리미터에 따라 데이터를 분리합니다.
        var delimiters = new string[] { "\x0A", "\x0D\x0A", "\xFF" };
        var messages = input;

        // 딜리미터를 기준으로 데이터를 분리
        foreach (var delimiter in delimiters)
        {
            messages = messages.Replace(delimiter, "\n"); // 구분자를 줄바꿈 문자로 바꿔서 Split
        }

        return messages.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public void Stop()
    {
        isRunning = false;
        tcpListener.Stop();
    }

    static void Main(string[] args)
    {
        TcpServer server = new TcpServer();
        server.Start();
    }
}

 


REQUEST)
거래소 주식 TCP 데이타는 회원사들이 100% 일방적으로 수신만 합니다. 거래소는 일방적으로 송신만 합니다. 그럼 회원사들이 사용하는 수신TCP프로그램을 작성하고 싶습니다. 데이타의 구분자는 3개의 딜리미터로 구분됩니다. 0x0a,0a0d,0xff입니다. c#으로 회원사 수신프로그램을 작성해주세요

RESPONSE-DEEP.SEEK)

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

class Program
{
    static void Main(string[] args)
    {
        string serverIP = "거래소서버IP"; // 거래소 서버 IP 주소
        int port = 12345; // 거래소 서버 포트 번호

        TcpClient client = new TcpClient(serverIP, port);
        NetworkStream stream = client.GetStream();

        byte[] buffer = new byte[1024];
        StringBuilder dataReceived = new StringBuilder();

        while (true)
        {
            int bytesRead = stream.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
            {
                // 서버와의 연결이 끊어졌을 경우
                break;
            }

            string chunk = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            dataReceived.Append(chunk);

            ProcessReceivedData(dataReceived);
        }

        stream.Close();
        client.Close();
    }

    static void ProcessReceivedData(StringBuilder dataReceived)
    {
        string data = dataReceived.ToString();
        string[] delimiters = { "\x0a", "\x0d", "\xff" };

        // 데이터를 구분자로 분리
        string[] messages = data.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

        foreach (string message in messages)
        {
            Console.WriteLine("Received: " + message);
        }

        // 처리된 데이터는 제거
        dataReceived.Clear();
    }
}

 

+ Recent posts