Y
Y
Yuri Vetrov2021-10-22 19:22:34
C++ / C#
Yuri Vetrov, 2021-10-22 19:22:34

Why does the c# socket send message loop work differently?

Why, when the client is first launched, the cycle in the server for sending a message is triggered 1 time (displays the number1), when the client is restarted, it fires completely (displays 1 2 3 4)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ServerTest
{
class Program
{

static byte[] buffer = new byte[2024];

static void Main(string[] args)
{

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0 .1"), 45008));

socket.Listen(10);
socket.BeginAccept(new AsyncCallback(ClientAccepted), socket); //принять попытку подключения
Console.WriteLine("Server is ready!");

Console.Read();
}

public static void ClientAccepted(IAsyncResult ar)
{
Console.WriteLine("client connect");
var socket = ar.AsyncState as Socket;
var client = socket.EndAccept(ar);

for (int i = 1; i < 5; i++)
{

client.Send(Encoding.UTF8.GetBytes( i.ToString()));

}

client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), client);
socket.BeginAccept(new AsyncCallback(ClientAccepted), socket);
}


public static void ReceiveMessage(IAsyncResult ar)
{
try
{
var socket = ar.AsyncState as Socket;
var length = socket.EndReceive(ar);
var message = Encoding.Unicode.GetString(buffer, 0, length);
Console.WriteLine(message);

socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}


Client code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ClientTest
{
class Program
{
static void Main(string[] args)
{

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socket.Connect("localhost", 45008);
Console.WriteLine("connect to the server");

socket.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReceiveMessage), socket);

var message = "Message from client : " + Console.ReadLine();
var outputBuffer = Encoding.Unicode.GetBytes(message);

for (int i = 0; i < 5000; i++)
{
socket.BeginSend(outputBuffer, 0, outputBuffer.Length, SocketFlags.None, null, null);
}


Console.ReadLine();

}

static byte[] buffer = new byte[1024];

public static void ReceiveMessage(IAsyncResult ar)
{

try
{
var socket = ar.AsyncState as Socket;
var length = socket.EndReceive(ar);
Console.WriteLine(length);

var message = Encoding.UTF8.GetString(buffer, 0, length);

Console.WriteLine(message);

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question