• WinSock

    From Jon Justvig@VERT/STEPPING to All on Sat Jun 9 20:29:33 2018
    Hi all...

    I am looking for examples of how to create a WinSock server. I would like for a telnet client to connect to the server. The server first displays text and provides a prompt for user input from the client. Can someone guide me in the right direction or provide a piece of code I can work with? I've googled until my guts fall out. I am building on a Win32 platform using Visual C++ Studio Community 2017.

    Thanks in advance.

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:8085

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Digital Man@VERT to Jon Justvig on Sun Jun 10 03:08:31 2018
    Re: WinSock
    By: Jon Justvig to All on Sat Jun 09 2018 01:29 pm

    Hi all...

    I am looking for examples of how to create a WinSock server. I would like for a telnet client to connect to the server. The server first displays text and provides a prompt for user input from the client. Can someone guide me in the right direction or provide a piece of code I can work with? I've googled until my guts fall out. I am building on a Win32 platform using Visual C++ Studio Community 2017.

    Here's a sample:

    https://www.tutorialspoint.com/unix_sockets/socket_server_example.htm

    It's generic socket example, but it's mostly relevant to WinSock. With Windows (and WinSock) you can't treat socket handles as file descriptors, so you'll have to replace the call to read() with recv() and replace write() with send().

    There's a similar WinSock-specific example on this page from Microsoft: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx

    digital man

    This Is Spinal Tap quote #40:
    Morty the Mime: Come on, don't talk back, mime is money, come on, move it. Norco, CA WX: 73.2øF, 52.0% humidity, 10 mph ENE wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Jon Justvig@VERT/STEPPING to Digital Man on Mon Jun 11 08:33:56 2018
    Re: WinSock
    By: Digital Man to Jon Justvig on Sat Jun 09 2018 08:08 pm

    Here's my code, trying to output using a socket:

    char c;
    std::fstream myStream("welcome.txt", std::fstream::in);
    while (myStream.get(c))
    {

    send(clientSocket, (const char*)c, size_t(0), 0);
    std::cout << c;

    Of course it outputs fine on the console, yet, it doesn't start on the second line on column 1, it starts the second line on the line above and on the column if left on...

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Nightfox@VERT/DIGDIST to Jon Justvig on Mon Jun 11 17:00:12 2018
    Re: WinSock
    By: Jon Justvig to Digital Man on Mon Jun 11 2018 01:33 am

    Here's my code, trying to output using a socket:

    char c;
    std::fstream myStream("welcome.txt", std::fstream::in);
    while (myStream.get(c))
    {

    send(clientSocket, (const char*)c, size_t(0), 0);
    std::cout << c;

    Where/how is clientSocket declared? And has it been set up with a valid socket handle? That isn't shown in your code snippet. It looks like this code is reading from a text file and trying to send each character from the file over a socket?

    Also, did you intend for the "std::cout << c" to be indented? It doesn't need to be, as it's not in a separate block of code.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Jon Justvig@VERT/STEPPING to Nightfox on Tue Jun 12 00:56:10 2018
    Re: WinSock
    By: Nightfox to Jon Justvig on Mon Jun 11 2018 10:00 am

    Here's my code, trying to output using a socket:

    char c;
    std::fstream myStream("welcome.txt", std::fstream::in);
    while (myStream.get(c))
    {

    send(clientSocket, (const char*)c, size_t(0), 0);
    std::cout << c;

    Where/how is clientSocket declared? And has it been set up with a valid socket handle? That isn't shown in your code snippet. It looks like this code is reading from a text file and trying to send each character from the file over a socket?

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

    I intend to read a text file and have it sent to the client through the socket.

    Also, did you intend for the "std::cout << c" to be indented? It doesn't need to be, as it's not in a separate block of code.

    Indentations has no meaning in source code except cosmetically.

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Jon Justvig@VERT/STEPPING to Digital Man on Tue Jun 12 08:13:59 2018
    Re: WinSock
    By: Digital Man to Jon Justvig on Sat Jun 09 2018 08:08 pm

    DM,

    Okay, I figured out send() just fine. I'm having major issues with recv(). Right now, I can only get it to input one character, even with char arrays or strings. Here's a sample:

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Nightfox@VERT/DIGDIST to Jon Justvig on Tue Jun 12 16:26:35 2018
    Re: WinSock
    By: Jon Justvig to Nightfox on Mon Jun 11 2018 05:56 pm

    Indentations has no meaning in source code except cosmetically.

    Yes, but indentations in C/C++ are typically done for separate code blocks. If you indent code randomly, then it is more difficult to read.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Digital Man@VERT to Jon Justvig on Tue Jun 12 18:32:06 2018
    Re: WinSock
    By: Jon Justvig to Digital Man on Tue Jun 12 2018 01:13 am

    Re: WinSock
    By: Digital Man to Jon Justvig on Sat Jun 09 2018 08:08 pm

    DM,

    Okay, I figured out send() just fine. I'm having major issues with recv(). Right now, I can only get it to input one character, even with char arrays or strings. Here's a sample:

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    Where is the definition of 'buffer'?

    digital man

    Synchronet "Real Fact" #75:
    Rob's alias "digital man" was inspired by a song on Rush's 1982 "Signals" album.
    Norco, CA WX: 82.9øF, 38.0% humidity, 8 mph NNE wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Jon Justvig@VERT/STEPPING to Digital Man on Tue Jun 12 23:34:12 2018
    Re: WinSock
    By: Digital Man to Jon Justvig on Tue Jun 12 2018 11:32 am

    Okay, I figured out send() just fine. I'm having major issues with
    recv(). Right now, I can only get it to input one character, even with
    char arrays or strings. Here's a sample:

    char buffer[20];

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    just above int result; ...

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Digital Man@VERT to Jon Justvig on Wed Jun 13 00:29:46 2018
    Re: WinSock
    By: Jon Justvig to Digital Man on Tue Jun 12 2018 04:34 pm

    Re: WinSock
    By: Digital Man to Jon Justvig on Tue Jun 12 2018 11:32 am

    Okay, I figured out send() just fine. I'm having major issues with
    recv(). Right now, I can only get it to input one character, even with
    char arrays or strings. Here's a sample:

    char buffer[20];

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    just above int result; ...

    So.. if you only receive 2 bytes (result == 2), you're still going to send 20 (sizeof buffer)? That's probably not what you want.

    digital man

    This Is Spinal Tap quote #16:
    David St. Hubbins: I believe virtually everything I read...
    Norco, CA WX: 83.2øF, 43.0% humidity, 9 mph ENE wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Jon Justvig@VERT/STEPPING to Nightfox on Wed Jun 13 01:23:33 2018
    Re: WinSock
    By: Nightfox to Jon Justvig on Tue Jun 12 2018 09:26 am

    Indentations has no meaning in source code except cosmetically.

    Yes, but indentations in C/C++ are typically done for separate code blocks. If you indent code randomly, then it is more difficult to read.

    Normally, I use good indentations for functions and code inside like while or do loops, etc... The code I posted isn't difficult to read. I understand if it were more complex then I could understand you pointing it out. Anyway, I'm more interested in getting the code working... :|

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From MRO@VERT/BBSESINF to Nightfox on Wed Jun 13 04:33:48 2018
    Re: WinSock
    By: Nightfox to Jon Justvig on Tue Jun 12 2018 09:26 am

    Re: WinSock
    By: Jon Justvig to Nightfox on Mon Jun 11 2018 05:56 pm

    Indentations has no meaning in source code except cosmetically.

    Yes, but indentations in C/C++ are typically done for separate code blocks. If you indent code randomly, then it is more difficult to read.

    Nightfox


    INDENTATIONS ARE FOR POOFS
    CARRIAGE RETURNS ARE WHERE IT'S AT
    ---
    þ Synchronet þ ::: BBSES.info - free BBS services :::
  • From Jon Justvig@VERT/STEPPING to Digital Man on Wed Jun 13 11:29:22 2018
    Re: WinSock
    By: Digital Man to Jon Justvig on Tue Jun 12 2018 05:29 pm

    char buffer[20];

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv
    cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    just above int result; ...

    So.. if you only receive 2 bytes (result == 2), you're still going to send 20 (sizeof buffer)? That's probably not what you want.

    How might I receive between 2 and 12 bytes?

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Digital Man@VERT to Jon Justvig on Wed Jun 13 19:24:03 2018
    Re: WinSock
    By: Jon Justvig to Digital Man on Wed Jun 13 2018 04:29 am

    Re: WinSock
    By: Digital Man to Jon Justvig on Tue Jun 12 2018 05:29 pm

    char buffer[20];

    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, buffer, sizeof(buffer), 0); // recv
    cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (const char*)buffer, sizeof(buffer), 0);
    }

    just above int result; ...

    So.. if you only receive 2 bytes (result == 2), you're still going to send 20 (sizeof buffer)? That's probably not what you want.

    How might I receive between 2 and 12 bytes?

    You're calling recv() with a max-length of 20 (sizeof buffer). That doesn't mean you'll necessarily receive exactly 20 bytes. You could receive anywhere between 0 and 20 bytes - the return value of recv() tells you how many bytes you received, if any.

    digital man

    This Is Spinal Tap quote #7:
    Nigel Tufnel: That's just nitpicking, isn't it?
    Norco, CA WX: 87.0øF, 41.0% humidity, 5 mph NE wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net
  • From Jon Justvig@VERT/STEPPING to Digital Man on Thu Jun 14 02:34:16 2018
    Re: WinSock
    By: Digital Man to Jon Justvig on Wed Jun 13 2018 12:24 pm

    SOCKET current_client = (SOCKET)lpParam;

    // buffer to hold our recived data
    char buffer;
    // buffer to hold our sent data
    char sendData[20];
    // for error checking
    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, (char*)&buffer, 1, 0); // recv cmds

    Sleep(10);

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (char*)&buffer, 1, 0);
    }
    }
    }

    Right now, it works, however, on the client side it only shows one character at at time with Message from client: (character the remote end sent) ...I tried using a char array, however, it was just showing back garbage. How might I get input up to about 12 characters until the "\r" (carriage return) is received to the server from the remote client?

    Sincerely,
    Jon Justvig
    Stepping Stone BBS
    telnet://vintagebbsing.com:2325
    http://vintagebbsing.com:81

    ---
    þ Synchronet þ Stepping Stone BBS - vintagebbsing.com:2325
  • From Digital Man@VERT to Jon Justvig on Thu Jun 14 03:50:06 2018
    Re: WinSock
    By: Jon Justvig to Digital Man on Wed Jun 13 2018 07:34 pm

    Re: WinSock
    By: Digital Man to Jon Justvig on Wed Jun 13 2018 12:24 pm

    SOCKET current_client = (SOCKET)lpParam;

    // buffer to hold our recived data
    char buffer;
    // buffer to hold our sent data
    char sendData[20];
    // for error checking
    int result;

    // our recv loop
    while (true)
    {

    result = recv(current_client, (char*)&buffer, 1, 0); // recv cmds

    Sleep(10);

    That Sleep() call shouldn't be necessary - just for debugging use?

    if (result > 0)
    {
    cout << "\n\tMessage from client: " << buffer;
    send(current_client, (char*)&buffer, 1, 0);
    }
    }
    }

    Right now, it works, however, on the client side it only shows one character at at time with Message from client: (character the remote end sent) ...I tried using a char array, however, it was just showing back garbage. How might I get input up to about 12 characters until the "\r" (carriage return) is received to the server from the remote client?

    See sockreadline() in http://cvs.synchro.net/cgi-bin/viewcvs.cgi/src/sbbs3/mailsrvr.c?revision=1.621

    It's recv()'s one byte at a time a builds up the string until a \n is received. You could easily adapt that to terminate on \r instead.

    I purposely pointed you to an old revision that doesn't support TLS, because the implementation is more simple.

    digital man

    This Is Spinal Tap quote #7:
    Nigel Tufnel: That's just nitpicking, isn't it?
    Norco, CA WX: 74.6øF, 51.0% humidity, 0 mph S wind, 0.00 inches rain/24hrs

    ---
    þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net