C# Socket Programming - Multiple Clients

preview_player
Показать описание
This is the simplest way I could do it. Find the code here (it's a bit more organized):

BTW: Use Buffer.BlockCopy() same syntax but faster
Рекомендации по теме
Комментарии
Автор

Your source helped me a lot with my uni project, made me kinda understand how to use C# implementation of sockets and how to serialize data for that. Not all heroes wear capes, thank you.

waszqba
Автор

Thanks for the awesome tutorial. I'm currently designing a multiplayer hacking simulator which I will now be able to program thanks to this tutorial :) It's basically just going to be command based where the client types in commands and the commands, clients and other data is dealt with in the server. Thanks a heap :D

fletchercutting
Автор

i was trying to learn about socket programming and this video is awesome, just watched once then i wrote my own server and client.

thanks for sharing....

harunmn
Автор

That was awesome! Not only did you help me get a better understanding but you also made me go further, so thank you very much!

fanuelinawgaw
Автор

4:30am testing, ah the exciting life of a programmer. few minutes off from when I'm watching to attempt a similar objective.
Nice tutorial. Verbalizing as you worked away was very helpful in understanding the reasoning for your choices.

squirekev
Автор

You are one of those guys people stand behind and watch code, great tutorial.

tgifhounds
Автор

Great job. You just saved my life and teaches me something fantastic.

alessandrogerelli
Автор

Excellent job teaching Sockets. Though, I would have preferred if you took time to comment your code. Not because it necessarily needs comments (though they might help explain things a bit better) but more because otherwise you go very fast! I had to pause and go back several times.

Still, learned more about working with sockets tonight than I ever did trying to read the docs.

buxt-tech
Автор

Thank you for this Tutorial! Very usefull to understand how sockets work in C# :)

Krypton
Автор

THAT was AWESOME (Y) really thank you man, you are very pro brother, thank you, that was what I was searching for it for about 2 weeks (Y) THANK YOU

tahacyberman
Автор

Thanks a lot dude! Very easy to follow along, you spoke clearly, and explained what each method did.

A++ would recommend.

randomasshandle
Автор

Oh thank god. This is so much nicer than the TcpListener/TcpClient api, which is all I've seen in google results. I've been using sockets directly like this for Udp traffic but wasn't sure that I could do the same for Tcp.

stefdevs
Автор

Great Video.. you  are a great programmer .I am big FAN of yours. Thanks for this video...it helped me to understand our company project which is also using socket programming.

ajitkumar
Автор

superb tutorial;used to be scared abt socket programming

stutitehri
Автор

Hey thanks for the video, I've just decided to get into network programming and this is a great introduction.

MrHogarth
Автор

Thanks a lot dude, it was something new from my experiance.

jubinjoy
Автор

very good job, i used this in my study

cristianokwiatkovsk
Автор

if the ports specified in code don't work, then set the port's number higher than 1024, I forget if port 100 is used by the system or not, but there are a LOT of open ports available, excellent tutorial tho!

justchris
Автор

Everyone who wants to send the time to all clients at once you would need to loop through the socket list sending it to all at once for instance

List<Socket> socketList = new List<Socket>();//Will be full of connected clients

foreach(Socket s in socketList)
{
byte[] data = sendString("time string here");
s.Send(data);
}


public byte[] sendString(string msg)//method for converting bytes into strings
{
byte[] data = Encoding.ASCII.GetBytes(msg);
return data;
}

DaGoofSta
Автор

If you want to fix the error when the client disconnects, replace the code in the ReceiveCallback function with this:

Socket socket = (Socket)ar.AsyncState;
            
            try
            {
                int received = socket.EndReceive(AR);
                byte[] dataBuf = new byte[received];
                Array.Copy(_buffer, dataBuf, received);
            
                string text =
received: " + text);
            
                if (text.ToLower() == "get time")
                {
                    SendData(DateTime.Now.ToString(), socket);
                }
                else
                {
request", socket);
                }
            }
            catch
            {
disconnected");


            }

fletchercutting
visit shbcf.ru