Hack Forums
[C] TCP server - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] TCP server (/showthread.php?tid=6106011)



TCP server - nullbyte - Aug 29, 2020

Code
#include <iostream>
#include <WS2tcpip.h>
#pragma comment (lib, "ws2_32.lib")

using namespace std;

int main()
{
    //initialize winsock
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);

    int wsOk = WSAStartup(ver, &wsData);
    if (wsOk != 0)
    {
        cerr << "Can't Initialize Winsock, quitting" << end1;
        return;
    }

    //create socket
    SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
    if (listening == INVALID_SOCKET)
    {
        cerr << "can't create socket, bitch" << end1;
        return;
    }
    //bind socket to ipaddr and port
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(4448);
    hint.sin_addr.S_un.S_addr = INADDR_ANY;

    bind(listening, (sockaddr*)&hint, sizeof(hint));

    //tell winsock the socket is listening
    listen(listening, SOMAXCONN);

    //wait for connection
    sockaddr_in client;
    int clientSize = sizeof(client);

    SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);

    char host[NI_MAXHOST];      //client remote name
    char service[NI_MAXHOST];   //service/port

    ZeroMemory(host, NI_MAXHOST); //memset
    ZeroMemory(service, NI_MAXHOST);

    if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
    {
        cout << host << " connected on port " << service << end1;
    }
    else
    {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        cout << host << " connected on port " <<
            ntohs(client.sin_port) << end1;
    }

    //close listening socket
    closesocket(listening);

    //while loop; accept and echo to client
    char buf[4096];

    while (true)
    {
        ZeroMemory(buf, 4096);

        //wait for client to send data
        int bytesReceived = recv(clientSocket, buf, 4096, 0);
        if (bytesReceived == SOCKET_ERROR)
        {
            cerr << "Error in recv(). quitting" << end1;
            break;
        }
        
        if (bytesReceived == 0)
        {
            cout << "client disconnected " << end1;
            break;
        }

        //echo message

        send(clientSocket, buf, bytesReceived + 1, 0);
    }

    //close socket
    closesocket(clientSocket);


    //shutdown winsock
    WSACleanup();
};

For some reason all my end1; lines are causing my program to not compile. I'm not sure why it's an undeclared identifier.

Any assistance would be greatly appreciated. It's probably a stupid mistake, but I'm not seeing it.


RE: TCP server - Poet - Aug 29, 2020

Pretty sure you're using an integer 1 > end1
Try to use letter l > endl
as in end Line


RE: TCP server - nullbyte - Aug 29, 2020

(Aug 29, 2020 11:57 AM)0xBlack Wrote:  Pretty sure you're using an integer 1 > end1
Try to use letter l > endl
as in end Line

I'm facepalming so hard right now. Thank you.


RE: TCP server - Dexie - Aug 29, 2020

Thanks for the code, and also thank you for commenting it as well, I'm sure it will help some people.


RE: TCP server - nullbyte - Aug 29, 2020

(Aug 29, 2020 05:52 PM)Dexie Wrote:  Thanks for the code, and also thank you for commenting it as well, I'm sure it will help some people.

No problem! I'm in the middle of building a client that you can send certain commands to as well.


RE: TCP server - 4Txe4y33C9zvNhMj - Aug 30, 2020

(Aug 29, 2020 05:52 PM)Dexie Wrote:  Thanks for the code, and also thank you for commenting it as well, I'm sure it will help some people.

Pretty sure it's based on this: https://bitbucket.org/sloankelly/youtube-source-repository/src/8a4847e8ca5f9f039a7246a1172aa87f64ef55c5/cpp/networking/BarebonesClient/Barebones_Client/main.cpp?at=master

The guy has some decent tutorials on networking in C++: https://www.youtube.com/user/sloankelly/videos