Согласно этому коду, когда я передаю пакет на сервер, сервер закрывает соединение и не возвращает ответ, потому что я установил пакет неправильно.
Рис. 1.

Рис. 2.
[img]https://i.sstatic. net/D8xxBO4E.png[/img]
Некоторые исправленные значения:
- Токен заказа: 101
- Идентификатор книги заказов: 3437892
- Боковой гирин (B/S/T): B
- Количество гиринов: 1
- Количество гиринов: 1
- li>
Цена (десятичный формат): 2,9 - Цена клиентского аккаунта: DE-102001
Код: Выделить всё
private static void SendOrder()
{
try
{
Console.WriteLine("Order Token girin:");
string orderToken = Console.ReadLine().PadRight(14);
Console.WriteLine("Order book ID girin:");
int orderBookId = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Side girin (B/S/T):");
string side = Console.ReadLine();
Console.WriteLine("Quantity girin:");
int quantity = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Price girin (Decimal formatında):");
decimal price = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Client-Account girin:");
string clientAccount = Console.ReadLine().PadRight(16);
// Diğer sabit alanlar
string customerInfo = "".PadRight(15);
string exchangeInfo = "".PadRight(32);
byte openClose = 0; // 0 = Default for the account
// Veriyi baytlara dönüştürme
byte[] orderTokenBytes = Encoding.ASCII.GetBytes(orderToken);
byte[] orderBookIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(orderBookId));
byte[] quantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(quantity));
byte[] priceBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(price * 10000))); // Fiyatı 4 bayt olarak ayarlama
byte[] displayQuantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(0L)); // 8 baytlı ve 0 olarak ayarlama
// Paket oluşturma
byte[] orderPacketBytes = new byte[114];
orderPacketBytes[0] = (byte)'O';
orderTokenBytes.CopyTo(orderPacketBytes, 1);
Array.Copy(orderBookIdBytes, 0, orderPacketBytes, 15, 4); // 4 baytlık Order book ID
orderPacketBytes[19] = (byte)side[0];
Array.Copy(quantityBytes, 0, orderPacketBytes, 20, 4); // 8 baytlık Quantity
Array.Copy(priceBytes, 0, orderPacketBytes, 28, 4); // 4 baytlık Price
orderPacketBytes[32] = (byte)'0'; // Time In Force (0 = Day)
orderPacketBytes[33] = openClose; // Open Close (0 = Default)
Encoding.ASCII.GetBytes(clientAccount).CopyTo(orderPacketBytes, 34);
Encoding.ASCII.GetBytes(customerInfo).CopyTo(orderPacketBytes, 50);
Encoding.ASCII.GetBytes(exchangeInfo).CopyTo(orderPacketBytes, 65);
displayQuantityBytes.CopyTo(orderPacketBytes, 97); // Display Quantity
orderPacketBytes[105] = (byte)'1'; // Client Category (1 = Client)
orderPacketBytes[106] = (byte)'0'; // OffHours (0 = Normal hours)
Encoding.ASCII.GetBytes("".PadRight(7)).CopyTo(orderPacketBytes, 107); // Reserved
byte[] packet = CreateOrderPacket(orderPacketBytes);
_networkStream.Write(packet, 0, packet.Length);
LogMessage("Sipariş girme isteği gönderildi.");
LogMessage("Giden: " + BitConverter.ToString(packet));
LogMessage("Giden (Metin): " + Encoding.ASCII.GetString(packet));
Task.Run(() => BeginReadingOrderResponse(_cancellationTokenSource.Token));
}
catch (Exception ex)
{
LogMessage("Sipariş girme isteği gönderilirken hata: " + ex.Message);
}
}
private static byte[] CreateOrderPacket(byte[] messageBytes)
{
byte[] lengthBytes = BitConverter.GetBytes((ushort)(messageBytes.Length + 1));
if (BitConverter.IsLittleEndian)
Array.Reverse(lengthBytes);
byte[] packet = new byte[lengthBytes.Length + messageBytes.Length + 1]; // Length + Type + Payload
lengthBytes.CopyTo(packet, 0);
messageBytes.CopyTo(packet, lengthBytes.Length);
packet[packet.Length - 1] = (byte)'\n'; // Terminating newline for SoupBinTCP
return packet;
}
private static async Task BeginReadingOrderResponse(CancellationToken token)
{
try
{
byte[] buffer = new byte[4096];
while (_isConnected && !token.IsCancellationRequested)
{
int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length, token);
if (bytesRead == 0)
{
LogMessage("Sunucu bağlantıyı kesti.");
Disconnect();
break;
}
ProcessOrderPacket(buffer, bytesRead);
}
}
catch (OperationCanceledException)
{
LogMessage("Okuma işlemi iptal edildi.");
}
catch (Exception ex)
{
LogMessage("Yanıt okunurken hata: " + ex.Message);
Disconnect();
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... y-encoding