Multiple Chat Clients: One Thread (in C++)

Sdílet
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...

Komentáře • 278

  • @tmy6294
    @tmy6294 Před 5 lety +13

    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

  • @nrncproductions
    @nrncproductions Před 6 lety

    Incredible stuff.Your explanation is so easy to understand.Great tutorial.Maybe the best winsock tutorial on CZcams till now.Thx again.

  • @EDemircivi
    @EDemircivi Před 7 lety +28

    Excellent tutorial! Thanks for your time :)

  • @johnmccourt000
    @johnmccourt000 Před 2 lety

    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.

  • @jonforhan9196
    @jonforhan9196 Před rokem

    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

  • @beauvisagecolin3567
    @beauvisagecolin3567 Před 5 lety +7

    Very nice explication, im glad to find a video like that ! (u saved my project for my technical degree)

  • @nipunramani
    @nipunramani Před 7 lety

    You are natural at teaching, thanks for these videos man they are of high quality.

  • @harshitmittal1217
    @harshitmittal1217 Před 4 lety

    Oh God what a simple way of explanation.
    You are awesome man!!!

  • @hemanta212
    @hemanta212 Před 2 lety +1

    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.

  • @doylechrsitopherpete1323
    @doylechrsitopherpete1323 Před 6 lety +1

    Easily the best video on this topic, explained so well

  • @higheredjohn8316
    @higheredjohn8316 Před 6 lety +2

    You have no clue how happy this video made me. Thank you for taking the time to show this - really helped me understand things.

  • @xetop
    @xetop Před 6 lety +2

    Thanks for doing this video ! Awesome stuff. Appreciate it.

  • @tiandizhong6495
    @tiandizhong6495 Před 2 lety +1

    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. : )🥳

  • @ColdBits
    @ColdBits Před 4 lety +6

    you really explain perfectly what the book didnt explained well you did it!

  • @medjassertoubib4467
    @medjassertoubib4467 Před 6 lety +32

    watching your videos I feel like I've taken a full year of school in just 10 minutes .#stupied_school_system

  • @_xentropy
    @_xentropy Před 5 lety +1

    I needed this. Thanks for explaining!

  • @raynopowell
    @raynopowell Před 6 lety +3

    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!

  • @alexwilson8269
    @alexwilson8269 Před 4 lety +2

    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!

  • @QouteOfTheDay
    @QouteOfTheDay Před 5 lety +2

    That was just wonderful. Thank you very much.

  • @gastongp2011
    @gastongp2011 Před 4 lety

    Excelent and practical video, thank you Mister.

  • @AvivCMusic
    @AvivCMusic Před 4 lety +3

    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!

  • @JonoJamesJonoJames
    @JonoJamesJonoJames Před 4 lety

    Thank you !! good tutorial and it was hard to find another comprehensive tutorial for multiple clients ...

  • @iamklevy
    @iamklevy Před 2 lety +1

    thanks man, we apreciate your work

  • @mytyang1248
    @mytyang1248 Před 3 měsíci

    Loved the vid, very well explained !

  • @user-po4nt6pf7l
    @user-po4nt6pf7l Před 21 dnem

    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.

  • @anlakkaya5511
    @anlakkaya5511 Před 6 lety +2

    Thanks man, you are awesome at teaching :)

  • @simbarasheteveredzi9024

    Just finished the video it was very informative your a Legend, thank you.

  • @tehviganotti
    @tehviganotti Před 7 lety +5

    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

  • @madhusudhanreddy4932
    @madhusudhanreddy4932 Před 5 lety +2

    Thank you soo much this helped me a lot

  • @DogeCharger
    @DogeCharger Před 3 lety +5

    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!

  • @IgorAherne
    @IgorAherne Před 7 lety +4

    Learning new stuff! :DD

  • @tsraikage
    @tsraikage Před 5 lety +5

    you are my way to mmo game 😃 great lessons. thank you! keep doing videos, they are very detailed and also simply explained, good job

  • @KONTROLTECNOLOGIA
    @KONTROLTECNOLOGIA Před 3 lety

    Just amazing! I could finally finish my "daemon" - THANKS Much!!!

  • @edenr1988
    @edenr1988 Před 5 lety

    good job! really liked your tutorial, please keep it up ! :)

  • @jonf9989
    @jonf9989 Před 4 lety

    You are a savior man.. truly amazing.. thanks

  • @sonar_devices
    @sonar_devices Před 4 lety

    Thank you so much. Hello from Belarus )

  • @MudHoleCreation
    @MudHoleCreation Před 6 lety +1

    Awesome job man

  • @andresrobertotulcan6935
    @andresrobertotulcan6935 Před 2 lety +1

    Great job

  • @quanphamanh957
    @quanphamanh957 Před 2 lety

    thank you for this video!

  • @jhonycaicedomora3534
    @jhonycaicedomora3534 Před 6 lety +1

    Good video many thanks.

  • @machrvon
    @machrvon Před 5 lety +1

    thank you alot for your tutorial.

  • @user-co6te9dz4l
    @user-co6te9dz4l Před 6 lety +1

    You are THE BEST!!!!!! Thank you so much!!!

    • @sloankelly
      @sloankelly  Před 6 lety

      +Николай Лебедев thank you :)

  • @patrickhawking4655
    @patrickhawking4655 Před 2 lety

    Best channel! No Water, really good

  • @zhivkonushev6165
    @zhivkonushev6165 Před 3 lety

    I definitely liked the video

  • @krizanfil
    @krizanfil Před 3 lety +1

    great vid! thanks!

  • @hichamwarryor3547
    @hichamwarryor3547 Před 2 lety +1

    Thank you sir

  • @tobiaslarsson8206
    @tobiaslarsson8206 Před rokem

    Thank you very much, excellent video! Now I just hope my project don't end up needing multi threading.... 🙂

  • @ed_iz_ed
    @ed_iz_ed Před 5 lety

    Awesome vid, haha looks like someones been working with threads

  • @alexanderbalasky6174
    @alexanderbalasky6174 Před 3 lety

    Awesome content

  • @manjoos4906
    @manjoos4906 Před 3 lety +1

    this is amazing, thanks :DDDD)))

  • @ivanypinheiro6410
    @ivanypinheiro6410 Před 2 měsíci

    finally i get it! Thank you!

  • @nsgirish
    @nsgirish Před 6 lety

    Excellent video sir :)

  • @SisirKumarPathy
    @SisirKumarPathy Před 4 lety

    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.

  • @huy9251
    @huy9251 Před 3 lety

    very nice

  • @WeskerPower
    @WeskerPower Před 4 lety +1

    nice videos man

  • @caseyli5580
    @caseyli5580 Před 6 lety +1

    Very helpful! Ty. Also - are you Bahamian? And/or Canadian? Noticed the bill on the board and "CRA, Eh"

    • @sloankelly
      @sloankelly  Před 6 lety +2

      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.

  • @bobjime510
    @bobjime510 Před 5 lety +1

    thank you bro

  • @kolyan199816
    @kolyan199816 Před 6 lety

    Exellent! Very cool lesson.But I want more theory, because you vary interesting explain. Please, make more tutorials about server and client.

    • @sloankelly
      @sloankelly  Před 6 lety

      +Николай Городецкий thank you :) feel free to check out the other videos in this playlist. Networking in C++: czcams.com/play/PLZo2FfoMkJeEogzRXEJeTb3xpA2RAzwCZ.html

  • @nitroneonicman
    @nitroneonicman Před 5 lety

    You're the boss.

  • @fastermanable
    @fastermanable Před 6 lety

    Nice Vid ;
    Learning new= stuff;

  • @quanghong3922
    @quanghong3922 Před 5 lety

    Good Job

  • @ButIfWeSurvive-WeHereInTheEnd

    YOU NUMBER 1!

  • @jamesmorrison4713
    @jamesmorrison4713 Před 6 lety

    Sloan your tutorials are excellent. Please do a multi client asynchronous server.

    • @sloankelly
      @sloankelly  Před 6 lety +1

      Thanks :) I'm working on more networking tutorial ideas, but I don't have an ETA when I'll start recording them.

  • @slimbdeful
    @slimbdeful Před 6 lety +2

    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!

    • @sloankelly
      @sloankelly  Před 6 lety

      D'oh! Thanks for debugging and posting what you found!

  • @maivutuyet3101
    @maivutuyet3101 Před 6 lety

    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!

    • @sloankelly
      @sloankelly  Před 6 lety

      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.

  • @Bakuta1103
    @Bakuta1103 Před 5 lety

    Any recommendations for learning about multithreaded chat server?

  • @ganaa72001
    @ganaa72001 Před 7 lety +1

    well, good job, when next part?

  • @kamc6322
    @kamc6322 Před 5 lety

    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

  • @deadoptick01
    @deadoptick01 Před 6 lety

    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.

  • @user-hg1mn3qo8x
    @user-hg1mn3qo8x Před 2 lety

    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?

    • @sloankelly
      @sloankelly  Před 2 lety

      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

  • @user-vu2vz5pk5m
    @user-vu2vz5pk5m Před 4 lety +1

    amazing

  • @gelding
    @gelding Před 4 lety

    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?

  • @Nicolas-ch6ft
    @Nicolas-ch6ft Před 5 lety

    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

  • @cmdrgarbage1895
    @cmdrgarbage1895 Před 4 lety

    Can you make this work with the client c++ program you've written?

  • @aqibj76
    @aqibj76 Před 6 lety

    I want to ask that how to open a new xterminal using C++ command in coding section?

  • @dharamgyaani
    @dharamgyaani Před 4 lety +1

    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

  • @medjassertoubib4467
    @medjassertoubib4467 Před 6 lety

    whitch is best for server .windows or linux? and thx

  • @DobyS100
    @DobyS100 Před 5 lety +1

    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

  • @elisal8693
    @elisal8693 Před 4 lety +2

    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.

    • @GhostJerry
      @GhostJerry Před 4 lety

      Me too, the BareBone Client doesnt work at all

    • @talhazaigham3575
      @talhazaigham3575 Před 2 lety

      @@GhostJerry did u guys find a solution?

  • @sourav9135
    @sourav9135 Před 2 lety

    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++

  • @robertjarrell3105
    @robertjarrell3105 Před 3 lety

    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

  • @rambergush1569
    @rambergush1569 Před 3 lety +1

    Genius

  • @armanaghilipour
    @armanaghilipour Před 5 lety

    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 ()

    • @sloankelly
      @sloankelly  Před 5 lety

      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

  • @gnewb9658
    @gnewb9658 Před 3 lety

    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.)

    • @sloankelly
      @sloankelly  Před 3 lety

      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.

    • @gnewb9658
      @gnewb9658 Před 3 lety +1

      @@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!

  • @jscientista
    @jscientista Před 6 lety

    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?

    • @sloankelly
      @sloankelly  Před 6 lety

      +Julio Castro I don't at the moment. But accessing RESTful services is something I have in my idea hopper.

  • @raspberrypi1848
    @raspberrypi1848 Před 2 lety

    MASTER!

  • @samuelesarti3271
    @samuelesarti3271 Před 6 lety

    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?

    • @sloankelly
      @sloankelly  Před 6 lety

      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!

  • @naoryehuda6830
    @naoryehuda6830 Před 3 lety

    why do we loop on return value of select? shouldn't we run on
    copy.fd_count?

  • @tedp9146
    @tedp9146 Před 4 lety

    Can you please do the exact thing for linux too?

  • @easynavigation847
    @easynavigation847 Před 2 lety

    Aside bitbucket where else can I get the code. I am having difficulties downloading it from there

    • @sloankelly
      @sloankelly  Před 2 lety

      Really should have moved these from bitbucket long ago. Done now! Full source is at github.com/codehoose/cpp-networking/tree/main/MultipleClientsBarebonesServer.

  • @tanayabhave859
    @tanayabhave859 Před 3 lety

    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++

  • @bamsgian9759
    @bamsgian9759 Před 3 lety

    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?

    • @sloankelly
      @sloankelly  Před 3 lety +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.

  • @dasmaffin1633
    @dasmaffin1633 Před 3 lety

    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

    • @sloankelly
      @sloankelly  Před 3 lety

      That’s right, yes. A single thread on the server. Multi-threaded tends to mean multiple threads on the same machine.

  • @Mrboxdude123
    @Mrboxdude123 Před 7 lety

    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]

  • @elkaseralkobra294
    @elkaseralkobra294 Před 6 lety

    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

    • @sloankelly
      @sloankelly  Před 6 lety

      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.

  • @rishabhmalhotra2225
    @rishabhmalhotra2225 Před 4 lety

    Please demonstrate on how to use putty.

  • @JovianCasius
    @JovianCasius Před 4 lety +1

    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 ?

    • @sloankelly
      @sloankelly  Před 4 lety +1

      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.

    • @JovianCasius
      @JovianCasius Před 4 lety

      I see, thanks sir !

  • @mucomplex9115
    @mucomplex9115 Před 3 lety +1

    no reason for me to not like this video. thumb up!

  • @robvanspaandonk
    @robvanspaandonk Před 5 lety

    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

    • @sloankelly
      @sloankelly  Před 5 lety

      Are you trying to run this under UN*X?

  • @techferv
    @techferv Před 5 lety

    Hey how can I reach you ? Id like to know how to connect using a specific IP address instead of the loopback IP

    • @sloankelly
      @sloankelly  Před 5 lety

      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.

    • @techferv
      @techferv Před 5 lety

      @@sloankelly Thank You.