#networking #sockets #tcpip #cplusplus

That is an introduction to community programming by constructing a server that echos the shopper’s messages again once more. The server is penned in C++.

Get it functioning in LINUX: https://www.youtube.com/watch?v=cNdlrbZSkyQ&attribute=youtu.be

In the event you is perhaps wanting for a backgrounder on networking, sockets, ports and lots of others. Study out the companion video clip beneath: https://youtu.be/TE1XQ8U_JNA

It’s your decision PuTTY to check out the server. It may be downloaded from right here: http://www.chiark.greenend.org.united kingdom/~sgtatham/putty/most present.html

Full useful resource will be downloaded right here: https://bitbucket.org/sloankelly/youtube-source-repository/src/learn/cpp/networking/BarebonesClient/


46 thoughts on “Making a TCP Server in C++”

  1. I'm having issues with getting the server to work.
    I copied the code from the bitbucket and upon running it I received error 10061 "WSAECONNREFUSED".
    Then I tried it with hint.sin_addr.S_un.S_addr = INADDR_ANY; instead of inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr); and this time I received error 10049 "WSAEADDRNOTAVAIL".
    I also tried changing the ipAddress to "" and got error 10061 again.
    I'm new to network programming and it's probably a mistake on my part. Please help.

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

  3. If you're using a different ide (Im using vscode) and you're getting an error about "inet_ntop was not declared" you may need to define _WIN32_WINNT so the compiler knows what system you're compiling for.
    I'm on windows 10 so adding "#define _WIN32_WINNT _WIN32_WINNT_WIN10" above all my code fixed the problem for me. More info here: https://docs.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers?redirectedfrom=MSDN#setting-winver-or-_win32_winnt

    If using gcc you may also need the flag "g++ server.c -o server -lws2_32"

  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

  5. 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?

  6. 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?

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

Leave a Reply

Your email address will not be published. Required fields are marked *