Multiple Chat Clients: One Thread (in C++)
Vložit
- čas přidán 18. 06. 2017
- #networking #sockets #multipleclients #nothread
This is an introduction to network programming by building a server in C++ that echos the client's messages back. It allows multiple clients to connect using the fd_set structure, the select() function and three macros: FD_CLR, FD_SET and FD_ZERO
Introduction to Networking, sockets and ports: • Introduction to Networ...
The original video (C++) allowing one client to connect: • Creating a TCP Server ...
Source code: github.com/codehoose/cpp-netw...
You'll need PuTTY to test out the server. It can be downloaded from here: www.chiark.greenend.org.uk/~sg...
Well you managed to do a great job explaining select and how to handle multiple sockets on a server. I learned way more in 20 minutes from this video than from a few hours online. Thanks a ton
Incredible stuff.Your explanation is so easy to understand.Great tutorial.Maybe the best winsock tutorial on CZcams till now.Thx again.
Excellent tutorial! Thanks for your time :)
Hi. Thanks for this video. You did a really good job of explaining fd_set. I've been learning sockets so I can try to fix a racing game from the 1990s. These videos have been crucial to getting the server side rewritten. I'm moving it from udp connections between every single client in a race to a central tcp server so that users don't have to have port forwarding/holepunching or directly connect to all the other users in the race.
I hope you know how much I appreciate you. I'm self taught developer learned C++ and now this opens so many doors for me, I owe you one man
Very nice explication, im glad to find a video like that ! (u saved my project for my technical degree)
You are natural at teaching, thanks for these videos man they are of high quality.
Oh God what a simple way of explanation.
You are awesome man!!!
Absolutely great tutorial, also very helpful that you subtly encouraged me to the debugging process. I recommend anyone to just hit a breakpoint and see what the 'select' function actually does each time you (disconnect, reconnect or send messages) from one of the clients.
For me, the confusion cleared up when I saw 'select' blocking the while loop for any activity and modifying the 'copy' fd set and the socketCount, to only include active sockets.
Thank you so much for this.
Easily the best video on this topic, explained so well
You have no clue how happy this video made me. Thank you for taking the time to show this - really helped me understand things.
+Higher Ed John thanks!
Thanks for doing this video ! Awesome stuff. Appreciate it.
Great tutorial, man! I'm currently implementing a easy database like redis, and the communication between multiple clients and single server in windows is tough for me. This video really help me a lot. : )🥳
you really explain perfectly what the book didnt explained well you did it!
watching your videos I feel like I've taken a full year of school in just 10 minutes .#stupied_school_system
I needed this. Thanks for explaining!
Awesomeness! Always wondered what the FD stands for :-)
Will have to play with the select() function in order to see how it can handle client specific data! Thank you for the help!
File Descriptor.
Mazda FD is a car
Thank you for posting this. I'm currently working on an assignment similar to this and you have helped me so much and explained everything so well!
You're so welcome!
That was just wonderful. Thank you very much.
Excelent and practical video, thank you Mister.
Hello sir, I'm a relatively experienced programmer, and your networking series is great for me to get into C++ network programming. Thank you very much!
Thanks!
Thank you !! good tutorial and it was hard to find another comprehensive tutorial for multiple clients ...
Glad it was helpful!
thanks man, we apreciate your work
Loved the vid, very well explained !
God bless you. it's the best tutorial channel that i ever saw in entire internet. you explain better than courses that i pay for.
Thank you!!
Thanks man, you are awesome at teaching :)
Just finished the video it was very informative your a Legend, thank you.
Thanks a lot for the video and the time you invest in it, i hope you're going to continue this serie on c++ and others aswell, imo very interesting content, deep but understandable explanation and you're good at teaching
He is awesome! :-)
Thank you soo much this helped me a lot
23:01
can confirm, this is an accurate interaction
This tutorial is definitely helpful for C++ networking and one of the better-explained ones too - thank you!
Learning new stuff! :DD
you are my way to mmo game 😃 great lessons. thank you! keep doing videos, they are very detailed and also simply explained, good job
Just amazing! I could finally finish my "daemon" - THANKS Much!!!
good job! really liked your tutorial, please keep it up ! :)
You are a savior man.. truly amazing.. thanks
Thank you so much. Hello from Belarus )
Awesome job man
Great job
thank you for this video!
Good video many thanks.
thank you alot for your tutorial.
You are THE BEST!!!!!! Thank you so much!!!
+Николай Лебедев thank you :)
Best channel! No Water, really good
Glad you enjoy it!
I definitely liked the video
great vid! thanks!
Thank you sir
Thank you very much, excellent video! Now I just hope my project don't end up needing multi threading.... 🙂
Awesome vid, haha looks like someones been working with threads
Awesome content
this is amazing, thanks :DDDD)))
finally i get it! Thank you!
Thank you!
Excellent video sir :)
+Ns Girish thanks!
This is video is really nice. Will it be possible for you to create a video on "multiple clients using windows IOCP"? I am trying to learn this but having a lot difficulties to understand the concept.
very nice
nice videos man
Very helpful! Ty. Also - are you Bahamian? And/or Canadian? Noticed the bill on the board and "CRA, Eh"
Canadian :) We were in the Bahamas on holiday and I got the bill as part of my change. The CRA, Eh? thing is a reference to the Family Guy episode when they pass though Canada to get to the North Pole.
thank you bro
Exellent! Very cool lesson.But I want more theory, because you vary interesting explain. Please, make more tutorials about server and client.
+Николай Городецкий thank you :) feel free to check out the other videos in this playlist. Networking in C++: czcams.com/play/PLZo2FfoMkJeEogzRXEJeTb3xpA2RAzwCZ.html
You're the boss.
Nice Vid ;
Learning new= stuff;
Good Job
YOU NUMBER 1!
Sloan your tutorials are excellent. Please do a multi client asynchronous server.
Thanks :) I'm working on more networking tutorial ideas, but I don't have an ETA when I'll start recording them.
Thank you for your tutorial. Really useful and well described. By the way. You made a little mistake when you added a new bonus function \quit in your shared code. When you type \quit in putty, it returns "\quit
" instead of "\quit". That's why you would never killed your server :) I have been investigating your code with debugger about hour and a half )) Sorry for my english. Thank you again. Nice job!
D'oh! Thanks for debugging and posting what you found!
Hello @Sloan, your video is very awesome but i have a question for you. you use FD function to connect the clients to server, i think it is seem to array which have listen and clients. But if 2 or more client send message together, how to it work? I used C# make the multiple chat app, and it use "thread", it divide some threads which handle clients at the same time. If you don't use "thread", maybe it happens lag, lead to "Not Responding" to server. Thank you!
When data is received it fills up a buffer inside WinSock. The recv() function is used to read that buffer and place the contents in your program's local memory (that 'buf' variable). If there's nothing in the buffer, recv() blocks, that's why we use those FD_XXX macros, to test if there is data waiting. If there's no data, recv() isn't called and the program doesn't block. All of this means that you can have multiple clients connected to a server running a single thread.
Now, you may want to NOT have a single process for your server, for whatever reason, but this example shows that for quick message passing this may be a viable solution for your problem without resorting to multiple threads / processes / servers etc.
Any recommendations for learning about multithreaded chat server?
well, good job, when next part?
Excellent video!!!
can you please make a video if there was a client.cpp sending a dynamic data to the server.
how should i handle the hand, data(dynamic) and tail
entire package struct to the server
I dont know if the select function change but now if you want the select function to dont block the thread you need to create a timeval variable fill it in by the time you want him to block and put it at the last parameter of the select function ortherwise if you put null or 0 it will block the thread.
Great tutorial! I tried to make reading only server switch between multiple nonblocking udp sockets but in my case i want to bind udp-es on one port only. Which approach works for udp sockets , because they can'be bind to one port and select function does not take sockaddr_in argument to switch between users IP-es?
Thank you! You can bind UDP sockets. I updated the code to use UDP instead of TCP:
github.com/codehoose/cpp-networking/blob/e803aa73141947951ef88e27d208a348d9d4f78a/MultipleClientsBarebonesServerUDP/MultipleClientsBarebonesServer/main.cpp#L24 Creates the UDP socket and github.com/codehoose/cpp-networking/blob/e803aa73141947951ef88e27d208a348d9d4f78a/MultipleClientsBarebonesServerUDP/MultipleClientsBarebonesServer/main.cpp#L37 binds it to the port #
This section of code uses recvfrom() to read the incoming data from the UDP client:
github.com/codehoose/cpp-networking/blob/e803aa73141947951ef88e27d208a348d9d4f78a/MultipleClientsBarebonesServerUDP/MultipleClientsBarebonesServer/main.cpp#L84-L88
amazing
Hello, program works fine, but when my friend tries to connect to the server it does not work for him. Although when I open multiple instances of PuTTY it works fine for me. Any idea why my friend does not see the chat or the welcoming message?
Hello, I am coding under macOS so I can't use the library WinSock... I replaced SOCKET by int but I have an issue with .fd_array and .fd_count
Here is the problem "No member named 'fd_array' in 'fd_set'" and "No member named 'fd_count' in 'fd_set'"
Do you have a solution?
Thank you very much
Can you make this work with the client c++ program you've written?
I want to ask that how to open a new xterminal using C++ command in coding section?
sir can you provide a way to connect multiple clients to a single server without using putty, I mean just by making clients in visual studio itself
whitch is best for server .windows or linux? and thx
Great tutorial , thanks ! I was wondering if you could show us how to implement a vector so that as soon as you run the program, names would appear instead of Socket numbers . Thank you
Sounds like a good idea. Ok!
Hi, how could I make the barebones clients from your other video receive and send to the server at the same time, I've trying for a while but I just can't get it right.
Me too, the BareBone Client doesnt work at all
@@GhostJerry did u guys find a solution?
How can I make a code for client what changes Do I have to make in the code that you teach in A video named Creating TCP client in C++
i copied the code but Putty keep exiting saying erro and i don't know why it work for the originally tcp server but not working for this one
Genius
Hi. Thank you for your videos. It's appreciated. But I wonder if Putty uses send() twice when I press enter? Because I need to use recv() twice to get all bytes. (Once for the message I've written in putty, and once for 2 bytes of nothing. Maybe it's a newline?)
I tested this by sending a message from Putty BEFORE the server had reached the function recv(). This time, it was enough to use recv () only once. I suppose everything from Putty had time to come in this time before I used recv ()
PuTTY seems to send the
separetly, and I don't know why. You can use the barebones client to send messages though. Video is here: czcams.com/video/0Zr_0Jy8mWE/video.html
Hello Mr. Kelly,
I have watched multiple of your videos, two on creating a TCP server (one winsock and one linux) and now this one on creating chat clients.
I am doing my own practice using vscode on a mac. I was wondering, are you familiar with Boost asio library? It seems, from the information i've gathered, this library is quite common in c++ networking. Does the Boost/asio library effectively replace those libraries chosen by you in this video and the TCP server video (Sockets libraries, etc.)
I’m aware that it exists, but I haven’t used it. I was teaching a college course on socket programming and I decided to make videos around winsock.
There’s definitely a good reason to use things like the Boost library, but I wanted to let people understand what’s under the hood.
@@sloankelly Absolutely, it definitely helped me understand all the steps it takes to create a socket and establish a connection.
Thanks for responding and making such informative videos!
Your videos are very good, I learn a lot. if you allow me, do you have some kind of video that teaches you to create a service in the WCF, REST, WebAPI?
+Julio Castro I don't at the moment. But accessing RESTful services is something I have in my idea hopper.
MASTER!
Copy!
@@johnmccourt000 ?
hi, thank u for the video, it was really usefull, but I' ve a problem:
if I do not want to use PuTTY, how can i create the client?
I have a video for that! czcams.com/video/0Zr_0Jy8mWE/video.html
It's a basic into and it does have a drawback that is mentioned in the comments section of this video. It doesn't auto update messages from other clients. However, someone did post a solution below!
why do we loop on return value of select? shouldn't we run on
copy.fd_count?
You could.
Can you please do the exact thing for linux too?
Aside bitbucket where else can I get the code. I am having difficulties downloading it from there
Really should have moved these from bitbucket long ago. Done now! Full source is at github.com/codehoose/cpp-networking/tree/main/MultipleClientsBarebonesServer.
Thank you for your videos. I am new to networking and have found your videos to be very useful. Is there a book you can recommend for me to study from for networking in C++
Yes! Beej’s networking guide beej.us/guide/bgnet/
@@sloankelly Thank you !
I wonder how looping after select works, if I have 3 connection and loops now works on connection #2 then I got message from client #1 at the same time as loop state is on connection #2. Did I still got message from connection #1?
The messages are received into a buffer and read as if they were a stream and so any messages that you 'miss' will be available the next time you call recv() on that socket.
So I guess what you mean is no threading on the server right? Because when I use 2 PCs I obviously have 2 separate threads so that wouldnt make sense to me
That’s right, yes. A single thread on the server. Multi-threaded tends to mean multiple threads on the same machine.
hi, I've been having a bit of a problem, because in order to call the fd_sets, etc, I've had to make them all pointers, and now it isn't letting me declare SOCKET Sock = copy.fd_array[i]
really great tutorial tho, very easy to follow
never mind all fixed :)
I used the same idea to send to every client that connected to the server a Msg . but when I use putty from another device to test if it work on internet . it doesn't connect to the server
You might need to open the port on your firewall. Are you connecting to the server over the internet, or just your local network? If you're trying to connect over the internet, it probably won't work because your internet provider won't allow you to do so.
Please demonstrate on how to use putty.
hi sir, nice video, learn a lot from here , but can i ask you a question about how do you make client 1 chat and then client 1 can chat client 2 again without waiting client 2 to response just like every chatting app do ?
The server gets data sent to it from each client. This data is held in a buffer until the server calls the recv() function.
By using the select() function we can wait until a message has been placed in the buffer before calling recv().
When a message has been received it is broadcast to every other client, except the one that sent the original message.
The clients use something similar in their code. This allows them to receive and respond at the same time.
I see, thanks sir !
no reason for me to not like this video. thumb up!
i get this error when i make ‘struct fd_set’ has no member named ‘fd_array’ at the line copy.fd_array[i]; does anyone know how to fix this
Are you trying to run this under UN*X?
Hey how can I reach you ? Id like to know how to connect using a specific IP address instead of the loopback IP
Use 0.0.0.0 on the server and it will choose the default (iirc) card. You can use ipconfig on the command line to find out the ip of your PC. Use that IP to connect to the server from the client.
@@sloankelly Thank You.