C# Socket Programming - Multiple Clients

  Рет қаралды 360,571

Brian

Brian

11 жыл бұрын

This is the simplest way I could do it. Find the code here (it's a bit more organized):
github.com/AbleOpus/Networkin...
BTW: Use Buffer.BlockCopy() same syntax but faster

Пікірлер: 277
@fanuelinawgaw2065
@fanuelinawgaw2065 8 жыл бұрын
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!
@fletchercutting9440
@fletchercutting9440 9 жыл бұрын
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
@waszqba
@waszqba 3 жыл бұрын
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.
@alessandrogerelli
@alessandrogerelli 7 жыл бұрын
Great job. You just saved my life and teaches me something fantastic.
@harunmn
@harunmn 7 жыл бұрын
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....
@tahacyberman3640
@tahacyberman3640 9 жыл бұрын
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
@squirekev
@squirekev 3 жыл бұрын
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.
@MrHogarth45
@MrHogarth45 10 жыл бұрын
Hey thanks for the video, I've just decided to get into network programming and this is a great introduction.
@Krypton91
@Krypton91 3 жыл бұрын
Thank you for this Tutorial! Very usefull to understand how sockets work in C# :)
@tgifhounds
@tgifhounds 10 жыл бұрын
You are one of those guys people stand behind and watch code, great tutorial.
@jubin4joy
@jubin4joy 10 жыл бұрын
Thanks a lot dude, it was something new from my experiance.
@ajitkumar15
@ajitkumar15 10 жыл бұрын
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.
@stutitehri993
@stutitehri993 10 жыл бұрын
superb tutorial;used to be scared abt socket programming
@nickcsgo
@nickcsgo 10 жыл бұрын
YES YESS YESS TY TY THIS IS EXACLTY WHAT I WAS LOOKING FOR PLZ TY
@randomasshandle223
@randomasshandle223 10 жыл бұрын
Thanks a lot dude! Very easy to follow along, you spoke clearly, and explained what each method did. A++ would recommend.
@vukasinvucenovic9015
@vukasinvucenovic9015 10 жыл бұрын
Would be bad if you gave him C++. HAHAHA dem programmer jokes....
@LeftTechticle
@LeftTechticle 10 жыл бұрын
Vukasin Vucenovic lol
@bestefarm9966
@bestefarm9966 7 жыл бұрын
Thank you so much for this tutorial! Great work! :-)
@cristianokwiatkovsk9059
@cristianokwiatkovsk9059 4 жыл бұрын
very good job, i used this in my study
@Inzolity
@Inzolity 8 жыл бұрын
This is pretty neat, thanks!
@jeelpatel9818
@jeelpatel9818 4 жыл бұрын
thank you very much. Really nice explanation
@sayalisurve5801
@sayalisurve5801 3 жыл бұрын
You are amazing!! thank you so much
@Crni03
@Crni03 6 жыл бұрын
Great tutorial! Thanx a lot!
@LeftTechticle
@LeftTechticle 11 жыл бұрын
Ya, if your clients disconnect ungracefully, you can get exception in various points of the server. You need to handle these errors and remove the client form your client list when they occur for a specific client.
@user-ci9ny8vo7b
@user-ci9ny8vo7b 5 жыл бұрын
Thx bro I am Taiwanese love your video a lot
@AbbasKhan-qu7ib
@AbbasKhan-qu7ib 7 жыл бұрын
well it is such a great help thank-you and keep it up (Y).
@LeftTechticle
@LeftTechticle 11 жыл бұрын
Its in my best interest to disregard byte accumulation for this video. And I dont know what you mean with the callbacks. And by the way, I will have another vid on receive past buffer size which will be much more robust than the other one.
@b4ux1t3-tech
@b4ux1t3-tech 9 жыл бұрын
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.
@LeftTechticle
@LeftTechticle 9 жыл бұрын
Indeed, I am going to get around to the source and apply comments to it at least. I've been refactoring everything.
@__-qe7pl
@__-qe7pl 4 жыл бұрын
hello your great tutorial is so good. this video i can solve my problem. Thanks a lot!
@binishvbabu
@binishvbabu 3 жыл бұрын
Thanks for the video!
@chiennguyen-un5wx
@chiennguyen-un5wx 8 жыл бұрын
Thanks u so much! ^^ it usefull for me!
@giaple1123
@giaple1123 10 жыл бұрын
Hey! Just wanna say thank you very much! :)))
@EbbsClifton33
@EbbsClifton33 4 жыл бұрын
This was great, thanks
@chrisschuller7055
@chrisschuller7055 3 жыл бұрын
Excelent Tutorial!
@sadfjlkjsdivje
@sadfjlkjsdivje 7 жыл бұрын
Thanks for the video:)
@panagiotisgnafakis9851
@panagiotisgnafakis9851 9 жыл бұрын
Hey man, your tutorial is awesome and I did understand it. I have one problem though, if I try to send data from another computer in the network it doesn't work. I try to change the listening IP to a specific IP and it only accepts connections that come from my computer's IP. If I try to send anything through the network, i.e. from another computer either A) it does't see the data coming (when socket is bound to "IPAddress.Any") B) it crashes at server setup (when socket is bound to an external (LAN address)) Do you have any idea on what is wrong?
@Nz_Yhwach
@Nz_Yhwach 5 жыл бұрын
My god, u help me a lot
@Levonfly
@Levonfly 3 жыл бұрын
Thank you!
@jaycelee8453
@jaycelee8453 2 жыл бұрын
Thank you. :)
@borissman
@borissman 8 жыл бұрын
THANK YOU!!!!
@Tibiafy
@Tibiafy 4 жыл бұрын
Thx man! U saved my life haha
@RMDetho
@RMDetho 8 жыл бұрын
Greetings @LeftTechticle , I liked your video and I liked your example even more. I hope you don't mind me asking you this.. I'm working on a multiplayer card game (just to learn, no specific reasons) and I stumbled upon your video trying to find a solution to server/client connections. In your example, the client is just as is on the server, in other words, server doesn't care who the client is.. but is there a way to differentiate separate clients? or send sort of an object containing information about the specific client to the server?
@Gildo202
@Gildo202 10 жыл бұрын
Thank you very much.
@GuillinCorp
@GuillinCorp 8 жыл бұрын
Thanks fam appreciate
@LeftTechticle
@LeftTechticle 11 жыл бұрын
I learned from initially books and now c# articles.
@bluepole421
@bluepole421 6 жыл бұрын
can you do it also in C# windows server and client.?
@fletchercutting9440
@fletchercutting9440 9 жыл бұрын
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 = Encoding.ASCII.GetString(dataBuf); Console.WriteLine("Text received: " + text); if (text.ToLower() == "get time") { SendData(DateTime.Now.ToString(), socket); } else { SendData("invalid request", socket); } } catch { Console.WriteLine("client disconnected"); socket.Close(); socket.Dispose(); }
@bigbolo1986
@bigbolo1986 8 жыл бұрын
thanks for this!!!
@firstnamelastname7844
@firstnamelastname7844 6 жыл бұрын
What's the SendData function?
@SuperChristianMiiworld
@SuperChristianMiiworld 6 жыл бұрын
Where the f**k did that SendData function come from?
@dievel5256
@dievel5256 6 жыл бұрын
tank you ^^
@lucasgrizante1165
@lucasgrizante1165 6 жыл бұрын
If a client disconnect, should I remove him from _clientSockets list? also, if I'm creating a game (just for example) with a login screen and etc, I'd have to call the "loopconnect" at the login button? Also, how can I force a client to disconnect (and make him know that he has been disconnected)? I'm thinking about just calling something like _clientSockets[0].Close(), _clientSockets[0].Dispose(), _clientSockets.remove(0); but I don't know what would happen with the client at this point. Also, thanks for your fix!!
@AcEPFL
@AcEPFL 10 жыл бұрын
In order to stop it closing you have to add Console.Readline(); in the Main. Make sure it is your last code in the Main.
@LeftTechticle
@LeftTechticle 11 жыл бұрын
I know exactly what you mean, but I left them things out for the sake of the concept
@russiansoldiersniper4183
@russiansoldiersniper4183 6 жыл бұрын
i love
@norge_daniel
@norge_daniel 4 жыл бұрын
Hi Brian. Congratulations.Good tutorial !! Do you have some example from this point to implement WebSocket Server in C# using Socket ?
@varunjain0606
@varunjain0606 8 жыл бұрын
Hi, Nice tutorial. Learned a lot from the video. However I have a doubt as to how can we handle the request based on timer...means I want to disconnect the socket connection if i don't get response from any client. May be 5 seconds for instance and the time for each client starts from the moment they are connected and whenever they send response, the particular timer for the client resets to 5 secs. Thanks.
@LeftTechticle
@LeftTechticle 11 жыл бұрын
If you mean to type it out as the video goes then yes. I dont induce that
@penguthepenguinj
@penguthepenguinj 5 ай бұрын
Amazing
@LeftTechticle
@LeftTechticle 11 жыл бұрын
Its probably not a good idea to try to predict where threads are spawned in Async methods. Just know whether you on a UI thread or not (print out your current thread IDs or something to see). BTW this vid is not meant to explain the behavior of anything, it was just to present a simple design pattern for multiple clients.
@justchris846
@justchris846 5 жыл бұрын
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!
@ThePreceptIsInCSharp
@ThePreceptIsInCSharp 6 жыл бұрын
can it work like a public server I meant can it send a message to one computer from a remote computer?
@jayelliott1
@jayelliott1 6 жыл бұрын
Hello Brian. I'm interested in executing OS commands on multiple servers in parallel ..ie to stop and start applications on all servers at once. I'm using VS Studio WPF C#...also considering integrating python or windows powershell, but hope to use C# only...do you have any tips? Will the method of programming sockets in your video work? I'm new...please share tips?
@LeftTechticle
@LeftTechticle 11 жыл бұрын
Well the Speech namespace does not provide freeform dictation, but you can try to load an entire English dictionary as grammar. You would then perform tests on the results like anything else. This is a long shot though
@r.gianfrancocarbonells.9662
@r.gianfrancocarbonells.9662 5 жыл бұрын
Thanks!
@LeftTechticle
@LeftTechticle 10 жыл бұрын
I think using the Socket class would be the best. Theres abstractions like TcpClient and TcpListener that don't seem to make anything easier. I cant tell you if it would be appropriate to adapt the sample. The sample wont be able to retrieve large amounts of data. As well its better just to put all of this stuff in a class library if it were any more complex than this.
@WelshGuitarDude
@WelshGuitarDude 7 жыл бұрын
Would this stuff be used to make server FPS stuff or is this more for things that doesn't require fast updates?
@tomasmartins5009
@tomasmartins5009 7 жыл бұрын
How would you go about checking everyone who's connected to the server and sending messages from the server to any client at any given time, not just as a response to a request from a client. Sort of a push notification.
@usergroupX
@usergroupX 9 жыл бұрын
ah , no need to convert port's byte ordering like in c++ ...you know why I don't neeed glasses? because I can C # .. lol
@tyrellwreleck4226
@tyrellwreleck4226 6 жыл бұрын
i have no idea what you said about c++, im no c++ literate but i like your joke :)
@Chiramisudo
@Chiramisudo 6 жыл бұрын
Wait, does that mean I can't C# because I do need glasses?... but I can C#. It's a paradox!
@mreazl6227
@mreazl6227 4 жыл бұрын
Lmao
@LeftTechticle
@LeftTechticle 10 жыл бұрын
Are you looking for suggestions on creating a chat program?
@DaGoofSta
@DaGoofSta 6 жыл бұрын
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 socketList = new List();//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; }
@martinhschei6486
@martinhschei6486 6 жыл бұрын
Nice tutorial! But why make the buffer static? All recevied data from clients will then be put in the same buffer.
@HonnerApl
@HonnerApl 7 жыл бұрын
I have a doubt, running this with many clients, using the same static buffer won't make the requests get all mixed?
@AbbasKhan-qu7ib
@AbbasKhan-qu7ib 7 жыл бұрын
i have done the same code and it worked fine. i made some changes like from server side i changed responce on switch(text) and it worked well but when i tried to read() some responce from serverside, on run time it throws exception of socket , at int rec = _clientSocket.Receive(recievedBuf); if someone can help ???
@LeftTechticle
@LeftTechticle 10 жыл бұрын
There are a lot general things I could say but not too much about receive and store information. I can give you general advice.but only through voip, preferably skype
@kimy5480
@kimy5480 6 жыл бұрын
Hello! How can I allow connections from clients outside of my local network?
@rogernevez5187
@rogernevez5187 8 жыл бұрын
Excellent! Do you know if it is possible to do a raw socket in win7?
@nimda4047
@nimda4047 2 жыл бұрын
Nice video, now the following question occurs to me; if i want to send a message from one client to the other(s) via server (client1 -> server -> client2), do i just use a loop and count through all the connected client sockets like "clientSockets[i].Send(data); i++;"
@dorinelnicolaerus3533
@dorinelnicolaerus3533 10 жыл бұрын
The idea is that the boards read some sensors, after that the data is processed by the firmware within the micro-controller and than is sent using Ethernet protocol to the server. Another situation is when I send from the server a command and the firmware within the micro-controller will process it and do a different task. So my question is whether the video that you presented can be adapted to do this, because I think that I must to use the same Socket class.
@alpharomeo5123
@alpharomeo5123 6 жыл бұрын
Good!
@siavashmehmandoost2198
@siavashmehmandoost2198 3 жыл бұрын
Thanks for this amazing tutorial! Is this applicable on Unity? I set client code on unity, But when I ran server on pc it didn't work
@madalinmironica3626
@madalinmironica3626 4 жыл бұрын
sei un parolaio
@theldentity
@theldentity 7 жыл бұрын
My visual studio says that Socket does not contain definition for BeginAccept help please. Tried in windows forms and actually works
@JMemini8
@JMemini8 10 жыл бұрын
Great Tut. I Learned something. But sir, why my client side only can request one time only? an also I tried with my two laptops connected to one router but the client cannot connect to server? Please response as soon as you got time. Thanks
@sbasalan
@sbasalan 10 жыл бұрын
How about out of loopback IP usage of your project? Because I did a project but it can only run as loopback IP(127.0.0.1)
@FranciscoJBurtin
@FranciscoJBurtin 5 жыл бұрын
Hi, Why you didn't develop you server in WCF fullduplex service?
@dmccalldds
@dmccalldds 3 жыл бұрын
How did you add a second Client at 26:30?
@gottogetbig2900
@gottogetbig2900 7 жыл бұрын
What does the buffer do? What happens if you dont use a buffer?
@LeftTechticle
@LeftTechticle 11 жыл бұрын
Start by establishing whether or not the server is crashing or not.
@bulldoggin12
@bulldoggin12 10 жыл бұрын
I need to construct a server which can receive/store the information in a "friends list" from a client for a chat program. Do you have any suggestions?
@ninjasquidface
@ninjasquidface 9 жыл бұрын
How come mine stops receiving from a client when the receive callback is called? I mean to say it only works if I put the beginreceive command at the end of the receive callback, is this normal?
@nealdorelis5703
@nealdorelis5703 Жыл бұрын
Nice! Can you connect this short client/server app with a database ??
@Mabox-sc5iz
@Mabox-sc5iz Жыл бұрын
I don't know if you don't pay attention but is there something for when a socket is disconnected? Because when I close a client the server crashes
@tyrellwreleck4226
@tyrellwreleck4226 6 жыл бұрын
why didn't you make a separate class for the client? just wondering
@arthurmillerl
@arthurmillerl 10 жыл бұрын
DUDE, YOU ARE A LEGEND.
@Mystic0Dreamer
@Mystic0Dreamer 6 жыл бұрын
This works fine when I run the client and server on the same computer. But when I run them on two different computers they never connect. These are computers that are connected via WiFi in my home and can share files. So why isn't this working? Do I need to specify IP addresses?
@JulioPoloArias
@JulioPoloArias 10 жыл бұрын
Likes++;
@zeekloft
@zeekloft 11 жыл бұрын
Normally people use PascalCase for methods but camelCase for variables and such.
@DaminGamerMC
@DaminGamerMC 2 жыл бұрын
what resources could I use to learn about programming stuff like this?
@wolftech1389
@wolftech1389 9 жыл бұрын
nice
@agustawicaksono
@agustawicaksono 8 жыл бұрын
I tried this code, and it works... but somehow. the server stops listening and i got this. "The thread '' (0x1768) has exited with code 0 (0x0)." Any helps?
@newbienewbie4823
@newbienewbie4823 8 жыл бұрын
i get this error for int received = socket.EndReceive(AR); unhandled exception: system.nullreferenceexception:object reference not set to an instance of an object.
@sybangnguyen8582
@sybangnguyen8582 10 жыл бұрын
A++
@koblar_
@koblar_ 9 жыл бұрын
How to send message from server to client? I wanted to create server and client in one application (listening and writing), but it doesn't work. There must be something I'm doing wrong... can you tell me what I need to do to send message from server to client?
@JesusChrist-tt1sz
@JesusChrist-tt1sz 9 жыл бұрын
If you want to get rid of the error that occurs when the client gets closed you need to use a try catch around where you get the exception.
@LeftTechticle
@LeftTechticle 9 жыл бұрын
Jesus Christ Ye, gotta catch them all
@ellywithpluto125
@ellywithpluto125 7 жыл бұрын
This help me alot problem with TCP, Console.Write("Thank you !")
@JohnDoe-rw8he
@JohnDoe-rw8he 6 жыл бұрын
You forgot the semi-colon
@michaelepica3564
@michaelepica3564 4 жыл бұрын
*Console.WriteLine("Thank you!");
@TuNguyen-zw9dv
@TuNguyen-zw9dv 10 жыл бұрын
could you please send me the code of these solutions? i can't find them in your attached link. thanks a lot
@LeftTechticle
@LeftTechticle 10 жыл бұрын
I can help with specific things for sure, but if your boards do not run .NET, then I would have little idea how to process the request.
C# Async Sockets Part 1: Basics
22:15
Brian
Рет қаралды 48 М.
Tutorial 22.2 - Network Programming in C#, TcpClient
4:30
Jeff Chastine
Рет қаралды 64 М.
THE POLICE TAKES ME! feat @PANDAGIRLOFFICIAL #shorts
00:31
PANDA BOI
Рет қаралды 25 МЛН
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 99 МЛН
마시멜로우로 체감되는 요즘 물가
00:20
진영민yeongmin
Рет қаралды 25 МЛН
C# Async Await Mistakes | Part 1
10:19
Amichai Mantinband
Рет қаралды 31 М.
C# multithreading 🧶
6:59
Bro Code
Рет қаралды 147 М.
Bob Nystrom - Is There More to Game Architecture than ECS?
23:06
Roguelike Celebration
Рет қаралды 194 М.
The New Constructor Type Coming in C# 12 is Weird
9:19
Nick Chapsas
Рет қаралды 65 М.
C# - where constraints
4:11
Brian
Рет қаралды 11 М.
TCP Socket Tutorial (C#, Visual Studio, Networked Console Apps)
16:24
wolfs cry games
Рет қаралды 56 М.
Tutorial 22.3 - Network Programming in C#, TcpListener
7:51
Jeff Chastine
Рет қаралды 65 М.
THE POLICE TAKES ME! feat @PANDAGIRLOFFICIAL #shorts
00:31
PANDA BOI
Рет қаралды 25 МЛН