Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

TCP server

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.
Pretty sure you're using an integer 1 > end1
Try to use letter l > endl
as in end Line
(Aug 29, 2020 11:57 AM)Poet 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.
Thanks for the code, and also thank you for commenting it as well, I'm sure it will help some people.
(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.
(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...?at=master

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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by gurlhackerclubAug 31, 2020 09:34 PM 0 185 Aug 31, 2020 09:34 PM
Last Post: gurlhackerclub
[C] by ʇsıʎqqoɥJan 2, 2020 05:33 PM 5 559 Jan 2, 2020 05:33 PM
Last Post: akali
[.NET] by HardStoNDec 25, 2018 01:17 AM 7 294 Dec 25, 2018 01:17 AM
Last Post: Nick (Name)