Creating a TCP Server in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ต.ค. 2024

ความคิดเห็น • 548

  • @RaZziaN1
    @RaZziaN1 5 ปีที่แล้ว +156

    For people with error "invalid padding length" instead of using SSH in puty, use RAW.

    • @Gavy093
      @Gavy093 5 ปีที่แล้ว +1

      If I may ask ...How did u figure that out?

    • @sirgouki6207
      @sirgouki6207 5 ปีที่แล้ว +4

      @@Gavy093 I figured it out before I saw this comment, just by random testing. Fun fact: both PuTTY telnet and Windows telnet will *immediately* send your keys to the server, which will *immediately* send them back, which looks like your telnet client is messing up.

    • @ericc5652
      @ericc5652 5 ปีที่แล้ว +1

      WOW...Thank you very much!

    • @alexanderwilson5659
      @alexanderwilson5659 4 ปีที่แล้ว

      it works!!

    • @gangelo777
      @gangelo777 4 ปีที่แล้ว

      Humm. This isn't really a "fix" if you want to maintain a secure connection. I wonder how to actually fix this.

  • @kidkyle712
    @kidkyle712 7 หลายเดือนก่อน +8

    7 year old video and still one of the best resources on this topic I could find. Amazing.

    • @sloankelly
      @sloankelly  7 หลายเดือนก่อน +3

      Glad it was helpful!

    • @heavymetalmixer91
      @heavymetalmixer91 4 หลายเดือนก่อน

      Why do you think it's the best? I'm just starting with programming in general and C++ specifically so I don't know anything about other "better options".

    • @kidkyle712
      @kidkyle712 4 หลายเดือนก่อน +3

      @@heavymetalmixer91 I just think the structure of the video is well done. Instead of jumping right into coding he first lays the steps out, otherwise you’d be enticed to just type over without thinking of the ‘bigger picture’ of what is actually being achieved. He also does this in the code with comments instead of a decoupled slideshow for example. That way you won’t have to go back to check the steps as they are there plus it chains the ‘theory’ and practice together.

  • @agentk3984
    @agentk3984 5 ปีที่แล้ว +43

    Comment the entire thing, THEN code it. That is the definition of professional sir, a true professional expert...

    • @choosetolivefree
      @choosetolivefree 3 ปีที่แล้ว +1

      I'm not a professional in that I am self taught and work for my self and not some big company.. but I'm pretty sure actual professionals use flow charts to outline their applications. Not saying the comment method isn't good, but flowcharts are definitely superior.

    • @choosetolivefree
      @choosetolivefree 3 ปีที่แล้ว

      @ASRDGEEK I mean, that's how I operate, I just step through each step of what I want the app to do, and then code it.. but I code solo. What about teams of developers? How does anyone know what each other are doing without some sort of outline?

  • @DeniseJamestoo
    @DeniseJamestoo 6 ปีที่แล้ว +16

    Great video and the comment on setting putty to raw. I got this working in one hour. This is my first ever socket application.

    • @darkchocolate3390
      @darkchocolate3390 5 ปีที่แล้ว

      How did you connect to Putty? I put my ip address I found in cmd but it didn't work.

  • @benficapedro2
    @benficapedro2 5 ปีที่แล้ว +16

    OMG been seeing so many tutorials and so many hours spent on this, and ONLY AFTER WATCHING THIS VIDEO, IT'S WORKING!!!!!!!!
    Thank you, you really helped me :D

  • @stevenhine8269
    @stevenhine8269 4 ปีที่แล้ว +2

    Thank you very much for explaining this!
    For those getting the error Can't connect to server code 10061. The repository link above is to the client. You'll have to find the correct bare bones server project.

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      You might get away with just changing the port number to something else. Another service might be using 54000.

  • @wamxfl1p653
    @wamxfl1p653 6 ปีที่แล้ว +58

    "I won't cover any error handling from now on. This program won't fail. I think." Lmfaooo

  • @wak6880
    @wak6880 6 ปีที่แล้ว +193

    i just went from hello world to TCP server

    • @radon1221
      @radon1221 6 ปีที่แล้ว +8

      congrats

    • @Coranthh
      @Coranthh 6 ปีที่แล้ว +38

      TCP server where you type hello world

    • @DeltaSigma16
      @DeltaSigma16 5 ปีที่แล้ว +3

      Congrégations dude , you are genius :)

    • @BizzaroBrainBoi
      @BizzaroBrainBoi 4 ปีที่แล้ว +2

      lol

    • @suhailahmed2310
      @suhailahmed2310 4 ปีที่แล้ว +1

      Aye congratulations its a celebration!

  • @jackbailey049
    @jackbailey049 3 ปีที่แล้ว +1

    The amounts of stuff you can learn through youtube! Thank you

  • @garyrogers378
    @garyrogers378 5 ปีที่แล้ว +1

    Others posting "tutorials" on TH-cam should look at this before posting some of the garbage that's on here. This is exemplary work, so well explained and most importantly, Mr.Sloan does not have marbles in his mouth as so many of these sound like!
    Thanks for taking the time to do a twisted topic a lot of good! Many thumbs up!!!

  • @PM-et6wz
    @PM-et6wz 7 ปีที่แล้ว

    Always wanted to do something like this in c++ but had no idea where to start. So thanks a lot and awesome video.

  • @delysid604
    @delysid604 2 ปีที่แล้ว +1

    Thank you mate. Clear explanation all ur steps. Good vid

  • @TheClonerx
    @TheClonerx 7 ปีที่แล้ว +163

    using namespace std; void main()
    my god, you are crazy

    • @kebluetugirafee7558
      @kebluetugirafee7558 6 ปีที่แล้ว +5

      why crazy?This very short code block never use another namespace which may cause confused with "std".emmm...whatever,you are right ,but that's not the key point for this video.

    • @janmerta8651
      @janmerta8651 6 ปีที่แล้ว +11

      void?

    • @blackgarfieldyt
      @blackgarfieldyt 6 ปีที่แล้ว +22

      The C++ standard requires main() to return int. Also, its bad convention to use "using namespace".

    • @NoSoupForYouu
      @NoSoupForYouu 6 ปีที่แล้ว +7

      Why is it bad convention? genuinely asking. Why would I want to constantly state std:: every time?

    • @Spirrwell
      @Spirrwell 6 ปีที่แล้ว +7

      For here I'd argue using namespace std; is not a bad convention. Where it can be bad is doing it in a header file. That would mean any file that includes that header would also use the namespace, and that might not be what you want. You could have functions, classes, etc. that have the same names and run into conflicts.
      For example, using min() and max(), they're in the std namespace, but you could have those same names elsewhere.

  • @alexanderhulton2748
    @alexanderhulton2748 4 ปีที่แล้ว

    I have a question, Sloan.
    Why didn't you use the addrinfo struct members, like ai_flag, or ai_protocall?
    And would you recommend the method I'm using?

  • @theshawnz
    @theshawnz 4 ปีที่แล้ว +1

    Sloan, thank you for the video , extremely helpful for a project I have been working on.

  • @licknelon3076
    @licknelon3076 6 ปีที่แล้ว +1

    Nice, a fellow Scotsman. Thanks for these videos!

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว +2

      I still have my accent! \\:D//

  • @bahnny
    @bahnny 7 ปีที่แล้ว +5

    just wanted to say great video, I'm going to play with this a bit. I wanted to add th reason you had trouble with putty at the end is you connected using ssh and not telnet. completely different protocol :)
    cheers

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      I always forget to hit that setting. I also miss ;'s and assign fields in the inspector :)

  • @masterpc-9845x
    @masterpc-9845x 6 ปีที่แล้ว

    Hi Sloan.
    I've only lived with a Linux VM Workstation for those socket programs. When I tried them on Windows, I have a few errors and thus can't compile them. Did I miss any important file or setting?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      What errors did you get? And what compiler/IDE are you using?

  • @dinoconsabor
    @dinoconsabor 3 ปีที่แล้ว

    This was a great introduction to sockets programming for me.

    • @TheTheThewillow
      @TheTheThewillow 3 ปีที่แล้ว

      I would recommend checking a video in C and not C++. This is just not detailed and more object oriented

    • @dinoconsabor
      @dinoconsabor 3 ปีที่แล้ว

      @@TheTheThewillow at the time they were looking into C++, but I agree.

  • @ridwaanbham1779
    @ridwaanbham1779 6 ปีที่แล้ว +2

    Thank you for simplifying Modbus TCP communication. The fact that you went through it step by step, illustrating the thought process really helps. Out of curiosity if one wanted to receive more than one value from the communication what would be the simplest way to do this?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      Not sure what you mean by more than one value, but the socket is always listening and when recv() is called anything in the buffer will be read in.

  • @emperorj4783
    @emperorj4783 3 ปีที่แล้ว

    Thank you. This really helped me! You are the only one with a good tutorial

  • @gayashanrathnavibhushana2615
    @gayashanrathnavibhushana2615 5 ปีที่แล้ว +2

    Thank you for this video series. It helped me a lot..

  • @0xb15h4l
    @0xb15h4l 3 ปีที่แล้ว +1

    a nice approach to explain and it is methodological! Impressive!

  • @saikirangattu2924
    @saikirangattu2924 6 ปีที่แล้ว +3

    Hi Sloan, best video ever on TCP IP. It worked for me when I built it couple months ago. Now it does not. I get an exception in server solution when I call "accept()" function. I have a feeling "sockaddr_in client;" object is not right. I tried using try catch but it does not handle the exception.
    //Wait for communication
    sockaddr_in client;
    int clientSize = sizeof(client);
    SOCKET clientSocket;
    try
    {
    sockaddr * p = (sockaddr*)&client;
    clientSocket = accept(listening_socket, (sockaddr*)&client, &clientSize);//*Exception here*
    }
    catch (...)
    {
    clientSocket = NULL;// just clearing ...
    clientSocket = accept(listening_socket, (sockaddr*)&client, &clientSize);
    }
    I also get an exception in "connect()" function in client.
    //Connect to server
    int ConnResult = 0;
    try
    {
    ConnResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
    }
    catch (...)
    {
    ConnResult = 0;
    ConnResult = connect(sock, (sockaddr*)&hint, sizeof(hint));// dont know what to do trying connect again
    }

  • @catalinstefanteodorescu2996
    @catalinstefanteodorescu2996 6 ปีที่แล้ว +1

    Great! It compiled and worked on my VS2015.
    A question please: is this code indeed C++ (as mentioned in the title) or just C ?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว +1

      +Stefan T. It is, in that it won't compile using a C compiler.

    • @catalinstefanteodorescu2996
      @catalinstefanteodorescu2996 6 ปีที่แล้ว +1

      Thank you for your answer. Another question please. For one message sent from putty, I checked in debugging mode that the while-loop inside the server runs 2 iterations, instead of one as I would have expected. First iteration it receives the message part (ex. "Hello world!" but without '\0' at the end) and sends it back to putty; the second iteration it receives a 2-bytes message consisting in '
      ' which is also sent back to putty. For this reason we get an extra blank line in putty between messages. Any understanding why this happens, and how to avoid the second iteration? Thank you.

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      AFAIK this is a putty issue. Shouldn't be an issue with other clients.

  • @nguyenthaile2024
    @nguyenthaile2024 2 ปีที่แล้ว

    OMG!!!!! THANK YOU SIR, YOU SAVE my LIFE!!!!!!!!! GOD BLESS YOU!!!!!

  • @cherifaly6757
    @cherifaly6757 ปีที่แล้ว

    Thank you for your amazing tutorial!
    I have one question please, what if I want to receive and send more than 4kb? What you recommend doing in this case?

    • @sloankelly
      @sloankelly  ปีที่แล้ว +1

      Thanks :)
      You can prefix your message with a header that will have the size of the payload. The receiving program then reads the bytes received and loads in the header. The header is _usually but not always_ a fixed size. Assuming it is and it contains four bytes that represent the size of the payload.
      I've written a _terrible_ pseudo code, but hopefully it illustrates what I mean. gist.github.com/codehoose/9be1ffa71353e2ec83f1823225467a00

  • @cangorkemgunes
    @cangorkemgunes 2 ปีที่แล้ว

    Hi sir, i just wanted to ask you some questions. Firstly, i have a microcontroller which is client for a socket server. Now, i would like to develop a socket server and want to deploy it my domain/hosting. How can i write it, do you have knowledge about it. I mean if i just write and example server like your codes does it work on a domain. What should i do?(My hosting is a windows server)

  • @rabit1998g
    @rabit1998g 5 ปีที่แล้ว

    is there way to send data to the server not via the same computer where the server is running, but from another device?

  • @usama57926
    @usama57926 6 ปีที่แล้ว

    Hi Sloan Kelly, your explanation is nice

  • @TheVestinius
    @TheVestinius 6 ปีที่แล้ว +8

    God this is so useful. Thank you so much!

  • @dennisschnapka9148
    @dennisschnapka9148 4 ปีที่แล้ว

    On line 86. I want to implement if I write e.g. 1 that the proagam startr google with the ShellExecuteA function. How can I put it in a If statement?

  • @brocksprogramming
    @brocksprogramming 7 ปีที่แล้ว +3

    I got it working, great video sir.

  • @TeddieMovie
    @TeddieMovie 7 ปีที่แล้ว +11

    Hello Sloan,
    i tried your code but i get an error from Putty saying:
    " Invalid padding length on recieced packet".
    Im farely new to C++ so i dont really know how to resolve this issue.
    your explanation was very good though :)

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว +13

      Thanks :) Did you set PuTTY to 'Raw' ?

    • @TeddieMovie
      @TeddieMovie 7 ปีที่แล้ว

      with your "custom" tcp client everything worked so i think it is fixed.

    • @TeddieMovie
      @TeddieMovie 7 ปีที่แล้ว

      Yeah tried that aswell but didnt work either.

    • @joachimkaddah6040
      @joachimkaddah6040 7 ปีที่แล้ว

      I had the same error and I did same as Mr.Kellz said and it work it.

    • @manual720
      @manual720 6 ปีที่แล้ว

      Set to Raw and it worked, Thank you!

  • @LachyBalboa1
    @LachyBalboa1 6 ปีที่แล้ว +1

    Hey Sloan Kelly or anyone else. Do you have any ideas about what we could do to make this program vulnerable and how might be exploited? E.g. modifying the buffer size?

  • @omarl7333
    @omarl7333 7 ปีที่แล้ว

    i using the raw option in putty, work perfect! ty great tutorial

  • @ThaiPham-bm9si
    @ThaiPham-bm9si 4 ปีที่แล้ว +1

    hello. i have problem!
    when i click Local Windows Debugger, it start 2s then close.thanks

    • @virozz1024
      @virozz1024 4 ปีที่แล้ว

      are u kidding men ?

  • @brocksprogramming
    @brocksprogramming 7 ปีที่แล้ว +1

    I know you used Visual Studio for this. Is there any way to do this using a free compiler like codeblocks? The problem I ran into is that the pragma isn't implemented. The only two errors are with getnameinfo() and inet_ntop. I'm guessing that they're found inside of the pragma library included.

  • @LaGordita732
    @LaGordita732 2 ปีที่แล้ว +1

    Hello, I have encountered problem with ntons, as the compiler says there is an error with that (supposedly, it's not defined)

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      What IDE / compiler are you using?

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      If you’re trying to get Winsock to work with anything other than Visual Studio, this video might help. th-cam.com/video/6qD3LYH-YnM/w-d-xo.html

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      It's htons too btw.

  • @vishwanathpai16
    @vishwanathpai16 4 ปีที่แล้ว

    Hi Sloan Kelly. Thanks for the tutorial you provided. I do have some questions though regarding the echo part. How does the client side interpret the bytes which are echoed to it? Is there an inbuilt function which readily converts the bytes of data to a string?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว +1

      Sort of. On the client side you can create a std::string with a byte array.

  • @matruprasadgantayat5160
    @matruprasadgantayat5160 2 ปีที่แล้ว

    Hey man, it shows cannot opensource file"sys/socket.h" ,same with "netinet/in h>.Any idea why is it happening?

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      What dev environment are you using? What IDE and compiler? My guess is that it’s not VS Studio/msbuild?

  • @dimitarvel1441
    @dimitarvel1441 5 ปีที่แล้ว

    What kind of keyboard have you got? Sounds very good.

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว

      Razer Blackwidow Stealth. 2014 I think?

  • @alexandrujugureanu3752
    @alexandrujugureanu3752 2 ปีที่แล้ว

    you re top, for real, lot of thanks for those videos, help a lot at school, love you for this, saves a lot of time for me

  • @megasalexander579
    @megasalexander579 4 ปีที่แล้ว +1

    If I transfer the compiled program to another PC, when I run it I get an error saying me that vcruntime140d.dll is missing. How can I do? (I'd prefer not to have to install Visual C++ or other stuff on that PC). Thanks

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      Make a release build of your application and copy it to the other machine. The dll that is missing is the debug runtime. The C++ runtime is probably on the target pc. If not, you will need to install that. I think this is the right version for vs2015? www.microsoft.com/en-us/download/details.aspx?id=52685

  • @ChungusTheLarge
    @ChungusTheLarge 4 ปีที่แล้ว

    Okay I'm curious. Why did you do your design in comments instead of a pure virtual class, or both?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      I wanted to concentrate on just barebones networking without adding complexity. It’s a tough subject to get into if you’re just starting, so barebones was the way I wanted to go.
      The comments were something I’ve done before. It “keeps me honest” in that it is easy to see what steps to code and in what order the operations should be performed.

  • @dheerajsharma5492
    @dheerajsharma5492 5 ปีที่แล้ว +1

    thanks for neat and clean explanation

  • @LusidDreaming
    @LusidDreaming 4 ปีที่แล้ว

    Why is clientSize a pointer? Is it possibly modified in any way when the connection is accepted?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว +1

      It can be, yes. It depends on the size of the sockaddr struct that is returned by the function.

  • @brendanlydon5272
    @brendanlydon5272 3 ปีที่แล้ว

    Does it matter what port number you input into the htons() around 11:40? He does not specify where this value comes from and I am wondering if it is arbitrary. Spent many hours before trying to build a server using ASIO and I think the port number matter? With ASIO I tried using port numbers attached to my local ip address through the command prompt and it did not seem to run implement or connect to the server. I probably did something wrong, but hey. Thanks in advanced

    • @sloankelly
      @sloankelly  3 ปีที่แล้ว

      It’s doesn’t matter so long as the client connects to that port number. I’d recommend using a port number between 1024 and 65535 that isn’t used by your local machine (the server)

  • @BluecoreG
    @BluecoreG 5 ปีที่แล้ว +1

    Avast and all anti virus programs are scams! Nothing beats Windows Defender and smart internet browsing.
    Great tutorial by the way!!

    • @sirgouki6207
      @sirgouki6207 5 ปีที่แล้ว

      Windows Defender *is* an antivirus... Those other programs existed before Windows Defender, and it borrows a lot from them.

  •  2 ปีที่แล้ว

    Thaks, great intro. I learned it.

  • @adriel8140
    @adriel8140 4 ปีที่แล้ว +1

    It worked! Thank you!

  • @jwk7317
    @jwk7317 4 ปีที่แล้ว

    sorry im korean so im not used to be using english
    i just start socket programming
    so am i create two project server project and client project?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      You can use PuTTY to connect to the server, or if you want you can use this one: th-cam.com/video/0Zr_0Jy8mWE/w-d-xo.html

    • @jwk7317
      @jwk7317 4 ปีที่แล้ว

      @@sloankelly thank you so much

  • @BubLblckZ
    @BubLblckZ 5 ปีที่แล้ว

    What do I have to do to make this work on 2 different computers with 2 different ip’s?

    • @DaCurse0
      @DaCurse0 5 ปีที่แล้ว

      Port forwarding

    • @BubLblckZ
      @BubLblckZ 5 ปีที่แล้ว

      DaCurse0 yes but dont i have to change the code a single bit?

  • @kulaningobeni20
    @kulaningobeni20 4 ปีที่แล้ว

    Thanks for the time and effort put in this video. It is very informative.

  • @francescobucciol6381
    @francescobucciol6381 5 ปีที่แล้ว +1

    Very nice tutorial! Thank you

  • @marcovalentinoalvarado3290
    @marcovalentinoalvarado3290 5 ปีที่แล้ว +5

    Sloan, im needing help, im trying to deal with this error: "Invalid padding length on received packet"

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว +7

      Are you using PUTtY? If so, make sure it’s set to “raw”.

    • @marcovalentinoalvarado3290
      @marcovalentinoalvarado3290 5 ปีที่แล้ว +6

      Sloan Kelly Sir, I really appreciate your help, I gave you a Like and I'm already subscribed, you've done an excellent job with these tutorials, keep teaching, you're amazing :)

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว +9

      Thank you!

    • @mrcollie7210
      @mrcollie7210 3 ปีที่แล้ว

      @@sloankelly I like your video very much, i recommended it to friends and give you a like!

  • @yuvaraj9268
    @yuvaraj9268 4 ปีที่แล้ว

    nice illustration, thank you

  • @Metruzero
    @Metruzero 4 ปีที่แล้ว +1

    Great resource, except for changing putty from ssh to raw, everything worked great! Thanks so much for the video.

  • @harkibains8787
    @harkibains8787 4 ปีที่แล้ว

    Great video, but I ran into the following problem.
    SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
    Immediately returns INVALID_SOCKET . From what I've read accept() should wait for a client connection and then return.
    Running without modification results in;
    204.204.204.204 connected to port 52428
    Error in recv(). Quitting
    I have tried changing the port number and "sockaddr_in client = {};"
    Any suggestions would help.

  • @phudinhtruong
    @phudinhtruong 4 ปีที่แล้ว +1

    Thank god it work

  • @wojciechbroka5989
    @wojciechbroka5989 4 ปีที่แล้ว

    Sloan your application is great! How can I change the mode from blocking to non-blocking socket?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      Thanks :) check out the ioctlsocket function docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-ioctlsocket

  • @Auron380
    @Auron380 4 ปีที่แล้ว

    I am getting errors: getnameinfo and inet_ntop is undefined?? Why is this? I am importing ws2tcpip.h. Is it because they are obsolete now?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      What IDE are you using on what platform?

    • @Auron380
      @Auron380 4 ปีที่แล้ว

      @@sloankelly I was using visual studio code with mingw.
      I think it had something to do with the library that was included in the package, I migrated everything over to visual studio community and the problem disappeared. I now have a problem with connecting to localhost on putty, but I think that is some native to Windows or my firewall.. if you have some tips about this, let me know! I am using your tutorial for an upcoming interview assignment, and it is very useful!
      Thanks for replying!

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      If you’re using mingw it will require some extra steps. I did this video for CodeBlocks, it might work for mingw too. th-cam.com/video/6qD3LYH-YnM/w-d-xo.html

  • @dasmaffin1633
    @dasmaffin1633 4 ปีที่แล้ว

    What if I want to send object data instead of a char?
    In most applications I just send an object like for example in a game an enemyVO that then tells me the location, current sprite, animation,... all that stuff that can't be contained in char data

    • @sloankelly
      @sloankelly  3 ปีที่แล้ว

      A char is a byte. I’m afk just now, but iirc you pass in the address of a struct that contains your data and the sizeof(my struct) as the length and you should be good to go.

    • @dasmaffin1633
      @dasmaffin1633 3 ปีที่แล้ว +2

      @@sloankelly Thanks, figured it out!
      For anyone else that is wondering about how to receive the data then (I for sure did) you just change the type of your buf variable to MyStruct (or whatever your object is called) and in the recv() you put (char*)MyStruct and you get your object.
      Thanks for the help

  • @ramasif9983
    @ramasif9983 6 ปีที่แล้ว

    I am new to coding and am trying to establish a TCP written in C++ for linux - I was wondering if this tutorial can be used there too? Saw some other tutorials for linux, where they do not use e.g. WSA-libraries? Thanks

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      WS is WinSock which is short for Windows Sockets Layer, which you won't have available to you because you're using Linux. You will need to include the correct headers for Linux and ignore any WSA....() functions.

    • @ramasif9983
      @ramasif9983 6 ปีที่แล้ว

      Thank you very much for the answer. And for the video of course.

  • @shashipreethampreetham316r
    @shashipreethampreetham316r 4 ปีที่แล้ว

    I am getting an error of "invalid padding length on received packets" in putty while opening the client with 127.0.0.1 and 54000

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      Set Putty to use RAW instead of SSH.

  • @ubiquitous9173
    @ubiquitous9173 4 ปีที่แล้ว

    'inet_ntop' was not declared in this scope? How do I fix that? I am using Code::Blocks MinGW64 Windows 7.

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      This video will help you! I was asked before about how to get this to work with CodeBlocks th-cam.com/video/6qD3LYH-YnM/w-d-xo.html

    • @ubiquitous9173
      @ubiquitous9173 4 ปีที่แล้ว

      @@sloankelly Oh ok thanks!

  • @iamklevy
    @iamklevy 2 ปีที่แล้ว

    thank you sir we appreciate that❤❤

  • @sirgouki6207
    @sirgouki6207 5 ปีที่แล้ว

    My version has an interesting problem that I can not pin down: If I send the message back to the client, it adds another line end (can't tell why because I can't figure out how to print the exact byte contents of the buffer). I can send any cstring to the client (const char *... like "Test") and it gets the exact message without adding an extra line end.
    I typed the code while following the video, and aside from a function I'm using as a shortcut to std::cout

  • @israjacome6771
    @israjacome6771 6 ปีที่แล้ว +1

    excellent explanation, please can you help me instead of sending the customer a greeting or string, I need to send vectors with integer values ​​as I do that?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว +1

      I'm AFK at the moment, but it'd be something like this:
      struct MyStruct
      {
      int x;
      int y;
      int z;
      }
      MyStruct s; // initialize struct with values
      char* ptr2Struct = (char*)&s;
      int sent = send(socket, ptr2Struct, sizeof(MyStruct), 0);
      On the receiving end:
      int read = recv(socket, buf, bufsize, 0);
      MyStruct* s = (MyStruct*)buf;
      You'd do something similar for other value types like int, float etc.

  • @usama57926
    @usama57926 6 ปีที่แล้ว

    can you help me in a problem that how to add graphics in visual studio

  • @GiuseppeSensolini
    @GiuseppeSensolini 6 ปีที่แล้ว

    Thanks a lot! very useful and well explained

  • @swiftsifu
    @swiftsifu 6 ปีที่แล้ว +2

    Great tutorial, thank you.

  • @thecolgatemann
    @thecolgatemann 2 ปีที่แล้ว

    Is it possible to make it work on WAN? like i could make a messaging app out of it?

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      Absolutely. Use IP ANY or 0.0.0.0 for the address. Any computer on your LAN will then be able to communicate with the server. For WAN it’s a bit more complex because you’ll probably need a static IP or other network configuration to get it to work on the net. Way beyond my expertise though. And it would depend on your Internet provider and other networking considerations

  • @awaixchodry
    @awaixchodry 4 ปีที่แล้ว

    hello sir.. can you please tell me how to implement linked list code to server .. socket programming?

  • @garimamishra8934
    @garimamishra8934 3 ปีที่แล้ว

    Hello sir,
    I have tried this code.
    And through putty when I am connecting local host afterthat I am not able to type something in putty
    What I can do for it ?

    • @sloankelly
      @sloankelly  3 ปีที่แล้ว

      Is Putty in raw mode?

  • @andsparky
    @andsparky 6 ปีที่แล้ว +1

    Very helpful! Thanks!

  • @0x_Anakin
    @0x_Anakin 5 ปีที่แล้ว +1

    best tutorial around!

  • @gaiabellovi7560
    @gaiabellovi7560 5 ปีที่แล้ว +1

    Really useful and interesting!

  • @Catrabela
    @Catrabela 7 ปีที่แล้ว

    Doing this in VS Community 2017 doesn't work...
    It's missing an include file in Winsock2.h, specifically qos.h
    Got it working in VS Community 2015 but my putty telnet connection echoes instantly rather than waiting for 'enter' to be pressed. I'm assuming something to do with Putty there.

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      +Draco T Bastard is putty set to "raw"? I haven't checked it in 2017, just installed it. I'll try tonight. Thanks for the heads up

    • @Catrabela
      @Catrabela 7 ปีที่แล้ว +1

      I've had putty set to Telnet. Just tried it with Raw and it worked thanx.

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      +Sloan Kelly just tried it and it works, but you have to install the C++ package for VS2017 if you got it from the Unity installer.

    • @Catrabela
      @Catrabela 7 ปีที่แล้ว

      I didn't get it from Unity but I may have had Unity installed prior to VS2017

    • @Catrabela
      @Catrabela 7 ปีที่แล้ว +1

      Ended up having to do a reinstall of VS2017. Worked after that.

  • @alexmao8805
    @alexmao8805 3 ปีที่แล้ว

    I always receive 204.204.204.204, which seems error in recv() function. The first time I try, it was successful, but now it always shows I have connected in 204.204.204.204.what can I do?

    • @alexmao8805
      @alexmao8805 3 ปีที่แล้ว

      this is my error:"204.204.204.204connected on port52428"

    • @alexmao8805
      @alexmao8805 3 ปีที่แล้ว

      I changed my port from 54000 to 54001, It worked, but why? it seems port 54000 can't be used.

    • @alexmao8805
      @alexmao8805 3 ปีที่แล้ว +1

      I fixed it by release port 54000, the port is disabled. Thank you for the tutorial!

  • @dennisschnapka9148
    @dennisschnapka9148 4 ปีที่แล้ว

    now it is compiling but he cannot find ... .obj, what I should do?

  • @kennethhudgins1369
    @kennethhudgins1369 7 ปีที่แล้ว

    Towards the end when you connect with your local 54000 I get an error...I obviously don't have that but I don't know how to movie forward.

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      Try another port #, like 54010. Make sure the client uses the same port. What's the error ..?

    • @moazzamali3025
      @moazzamali3025 4 ปีที่แล้ว

      @@sloankelly, "Putty Fatal Error: Network Error, Connection Refused"

  • @tunahansert9573
    @tunahansert9573 6 ปีที่แล้ว

    Hello Sloan,
    When i compile the code, i get 10038 error code with WSAGetlastError(). This 10038 error code is WSAENOTSOCK(Socket operation on nonsocket).
    Any thoughts on fixing this? thanks for help.

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      Can you post up the code you have on gist? Thanks

    • @tunahansert9573
      @tunahansert9573 6 ปีที่แล้ว

      It's basicly a shortened copy of yours and i get the this on screen : " Error in recv(): 10038 "
      #include
      #include
      #pragma comment (lib, "ws2_32.lib")
      void main()
      {
      WSADATA data;
      WORD vers = MAKEWORD(2, 2);
      int initChecker = WSAStartup(vers, &data);
      if (initChecker != 0)
      {
      std::cerr

    • @tunahansert9573
      @tunahansert9573 6 ปีที่แล้ว +1

      I changed the port number from 49153 to 54000 then it started working without the problem.

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      Awesome! Glad you got it working

  • @ochenhoroshy6104
    @ochenhoroshy6104 6 ปีที่แล้ว +1

    I am new to C++. Does this tutorial fit for Unix users?

    • @jflopezfernandez
      @jflopezfernandez 5 ปีที่แล้ว

      *nix systems use actual Berkley socket implementation code, which is slightly different. The Windows version is really similar but there are some differences that would cause compiler errors, such as the WSA prefix. WinSock, specifically WinSock2, which is the version of WinSock in modern systems, is a port of the original Berkeley sockets for Windows. So this exact code won't run on *nix systems, but the correct code is very similar and you should be able to find it either online or through books like Unix network programming. If you're on a Unix-like system, anything by Richard Stevens is worth its weight in both gold and silver

  • @Buruhtania
    @Buruhtania 2 ปีที่แล้ว

    26:35 How to properly detect the client was disconnected without any errors? It's always returns the SOCKET_ERROR code after closing the client socket.

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      That's what happens when a client disconnects. Check the recv() result, if it's < 1 then the client pulled the plug.

    • @Buruhtania
      @Buruhtania 2 ปีที่แล้ว

      @@sloankelly No, i mean how to detect disconnection reason? The client was disconnected by itself or the connection was unexpectedly interrupted.

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      That’s not possible and I’m not sure it matters anyway? The connection dropped and that’s enough of a signal to let the program know to clean up anything that uses it.

    • @Buruhtania
      @Buruhtania 2 ปีที่แล้ว

      @@sloankelly Ok, i understand. Thanx. I liked this video. I understand socket principal.
      I think you need to write a simple client-server game. Something like pong.

  • @B1TCH35K1LL3R
    @B1TCH35K1LL3R 7 ปีที่แล้ว

    why does it need the pragma declaration on the main.cpp file?

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      To include the Winsock library. The alternative is to go into project settings and add it there.

  • @jaideepshekhar4621
    @jaideepshekhar4621 4 ปีที่แล้ว

    22:18 I get "inet_ntop()" is not defined! What to do? I am using Windows 10.

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      What IDE are you using?

    • @jaideepshekhar4621
      @jaideepshekhar4621 4 ปีที่แล้ว

      @@sloankelly VSCode 1.41.1. inet_ntoa and inet_addr are shown, but not inet_ntop.

  • @anlakkaya5511
    @anlakkaya5511 5 ปีที่แล้ว

    thanks, this was helped me a lot last year, and allowed me to code a big program to achieve my dreams.

  • @emilovici8688
    @emilovici8688 7 ปีที่แล้ว

    When I try to connect with Putty I get an error: "Network error: Connection refused." Should I keep the same IP address and Port as in the video?

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      +Emilovici OfAlk try another port, e.g. 49153

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      +Emilovici OfAlk are you using Raw for putty?

    • @emilovici8688
      @emilovici8688 6 ปีที่แล้ว

      I tried a lot of different number ports but I still get that error.
      I don't know what does Raw mean but I'll do some search to find out.

  • @geckoo9190
    @geckoo9190 6 ปีที่แล้ว

    Ho men i have being trying to make a socket program work like for three days, its a real problem with the libraries, that you have to use this or that you have to use that, at the end all those libraries are full of holes.

  • @NiceBot724
    @NiceBot724 6 ปีที่แล้ว

    I wrote the exact same code in VS2017 but once i hit "local windows debugger" a prompt flashes and goes away.. when i tried to run the "bearboneserver.exe" file in my debug folder through the command prompt i get "cannot start winsock" message. I don't know where i am going wrong. Please help me out

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      +Gourish Biradar write out the error code returned from WSAStartup() and look the value up here: msdn.microsoft.com/en-us/library/windows/desktop/ms742213(v=vs.85).aspx

  • @colepeterson5392
    @colepeterson5392 7 ปีที่แล้ว +1

    I dont get any errors, but typing into putty results in multiple characters being pushed for instance 'a' types as 'aaaa'. Pressing enter does not echo the message back either. I thought maybe its constantly echoing without ending line but I followed the code exactly. Any ideas? Also, Awesome video.

    • @colepeterson5392
      @colepeterson5392 7 ปีที่แล้ว +1

      Nevermind! If anyone has this problem, I set the connection type to Raw and it worked!

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      +Cole Peterson thanks! Is putty in raw mode? If not, try that.

    • @sloankelly
      @sloankelly  7 ปีที่แล้ว

      +Cole Peterson thanks for posting the solution!

  • @user-dz4qq7gv1w
    @user-dz4qq7gv1w 7 ปีที่แล้ว

    how to send file or chat in between computer in ftp ?

  • @vaishnavinarayan5418
    @vaishnavinarayan5418 6 ปีที่แล้ว

    Hi Sloan will this work without internet? like only with LAN?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      Yes, it'll work just fine.

    • @vaishnavinarayan5418
      @vaishnavinarayan5418 6 ปีที่แล้ว

      Thanks, Sloan. I am really new to socket programming. I also want to know how to execute these client and server programs without using putty. If you can tell me how,it would be of great help.

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      You need a client of some kind to talk to the server. Doesn't have to be putty, but you'll need something. Luckily there's a video for that :) th-cam.com/video/0Zr_0Jy8mWE/w-d-xo.html

    • @vaishnavinarayan5418
      @vaishnavinarayan5418 6 ปีที่แล้ว

      Sloan Kelly Sloan Kelly that's great... Many thanks, Sloan.. Also can u guide me how to make this work as p2p chat? Like what should be added or changed? Or can you tell me as to how to go abt it?

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      Have you seen my multiple chat client one thread server video?

  • @awaismushtaq5719
    @awaismushtaq5719 2 ปีที่แล้ว

    Excuse me Mr. Kelly, you must also switch theme from dark to light for better viewing because in this theme every thing looks dark so so dark and puts strain on viewers' eyes!

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      An epic debate to be sure. What do other people think? Do you use dark or light mode? Which one would you prefer for these videos?

    • @awaismushtaq5719
      @awaismushtaq5719 2 ปีที่แล้ว +1

      @@sloankelly I would say light theme. Black text against white background puts less strain on eyes and is ergonomically compliant. Choice is yours however!

    • @sloankelly
      @sloankelly  2 ปีที่แล้ว

      I created a poll in the channel. It’s an interesting question.

  • @sonaion5668
    @sonaion5668 5 ปีที่แล้ว

    first of all thank you.
    but how can i connect 2 different devices via the internet using tcp/ip server, i got it by using hamachi to get 2 devices to the same network, but how can i do it without?

    • @csteam7287
      @csteam7287 5 ปีที่แล้ว

      You can open a port on your rooter and use that port in your program and then use your rooter's public ip

  • @TripedalTroductions
    @TripedalTroductions 5 ปีที่แล้ว

    I'm having trouble understanding why you chose 127.0.0.1

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว

      127.0.0.1 is the loop back address for the networking stack. The data doesn’t go out to the network and stays on the same machine.

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว

      See en.wikipedia.org/wiki/Localhost

  • @abindieflasche100
    @abindieflasche100 5 ปีที่แล้ว

    i executed your code and i get the socket error 10061 wich means something is blocking creating the socket pls help

    • @sloankelly
      @sloankelly  5 ปีที่แล้ว +1

      Try using a different port instead of the one in the example (54000)

  • @eduardoalarcon239
    @eduardoalarcon239 4 ปีที่แล้ว

    Hi Sloan thank you for this video. I have an issue with wsOk value , can you help me please?

    • @sloankelly
      @sloankelly  4 ปีที่แล้ว

      How can I help you?

    • @eduardoalarcon239
      @eduardoalarcon239 4 ปีที่แล้ว

      @@sloankelly in run time the wsOk receive the value -89756311 or something like this.

  • @braceleerohith
    @braceleerohith 6 ปีที่แล้ว

    please suggest me some books on network programming in c++ ( i have solid understanding in protocals)

    • @sloankelly
      @sloankelly  6 ปีที่แล้ว

      That's easy! :) beej.us/guide/bgnet/