BeginAccept not calling back to end accept

So I’m trying to get a console app running where the server and client can message each other i had it working before but it was with blocking or synchronous lines like send or receive and i wanted to switch over to unblocking because either the client or server had to wait to receive a message to send one and it wasn’t practical so i started with BeginAccept to get a feel of how they work but my beginAccept isn’t calling back to the static void that has the endAccept for a proper connection

Any and all help is greatly appreciated

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

public class Server
{
    public static void Main()
    {
        //Set ipv4 address and socket to variables
        IPAddress ServerAddress = IPAddress.Parse("127.0.0.1");
        int ServerPort = 1234;
        bool connected = false;

        //Setup the new socket with the parameters of addressFamily.interNetwork (tells it to use a ipv4 address)
        //socketType.stream and protocolType.tcp (tells it to have a connection tye of tcp)
        Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        ServerSocket.Bind(new IPEndPoint(ServerAddress, ServerPort));

        //telling the socket to start listening and to only let 5 connections at one time
        ServerSocket.Listen(5);
        Console.WriteLine("Server is listening for connections");

        while (!connected)
        {
            ServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), ServerSocket);
            
            if (ServerSocket.Connected == true)
            {
                connected = true;
                Console.WriteLine("Client connected!");
            }
        }
        

        
        

        while (connected == true)
        {
            
            if (ServerSocket.Connected == true)
            {
                connected = true;
            }
            else
            {
                connected = false;
            }


            //setup a array called temp to store the clients data, set what you recieve from clientSocket to be put into an int called ClientBytes
            //convert the bytes from clientbytes to string to read it 
            byte[] temp = new byte[1024];
            int ClientBytes = ServerSocket.Receive(temp);

            if (ClientBytes <= 0 || temp == null)
            {
                return;
            }
            else
            {
                string ClientMessage = Encoding.ASCII.GetString(temp, 0, ClientBytes);
                Console.WriteLine("Recieved data from client: " + ClientMessage);
            }


            //Convert the message to bytes, store it in a array then send it down the client socket
            string ServerMessage = Console.ReadLine();
            if (ServerMessage == null || ServerMessage.Length == 0)
            {
                return;
            }
            else
            {
                byte[] temp2 = Encoding.ASCII.GetBytes(ServerMessage);
                ServerSocket.Send(temp2);
            }

            /*ClientSocket.Shutdown(SocketShutdown.Both);
            ClientSocket.Close();*/


        }
        if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.F10)
        {
            ServerSocket.Close();
        }
        

    }
    private static void AcceptCallback(IAsyncResult ar)
    {
        
        Socket ServerSocket = (Socket)ar.AsyncState;
        Socket Handler = ServerSocket.EndAccept(ar);
    }
}

I expected it to just connect but it didn’t. I haven’t tried much to be honest because i thought i just got a good understanding of it but i guess not

New contributor

Doblob83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật