So this is the code for the client and server

This is a discussion on So this is the code for the client and server within the C++ Programming forums, part of the General Programming Boards category; Code: ***************************server******************** //*************************************************************** // From the book "Win32 System Services: The Heart of Windows 95 // and Windows NT" // ...

  1. #1
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14

    So this is the code for the client and server

    Code:
    ***************************server********************
    
    //***************************************************************
    // From the book "Win32 System Services: The Heart of Windows 95
    // and Windows NT"
    // by Marshall Brain
    // Published by Prentice Hall
    //
    // Copyright 1995, by Prentice Hall.
    //
    // This code implements a TCP server.
    //***************************************************************
    
    // msipserv.cpp
    
    #include <windows.h>
    #include <iostream.h>
    #include <winsock.h>
    #include <process.h>
    
    #define NO_FLAGS_SET 0
    
    #define PORT (u_short) 44965
    #define MAXBUFLEN 256
    
    VOID talkToClient(VOID *cs)
    {
      char buffer[MAXBUFLEN];
      int status;
      int numsnt;
      int numrcv;
      SOCKET clientSocket=(SOCKET)cs;
    
      while(1)
      {
        numrcv=recv(clientSocket, buffer, 
          MAXBUFLEN, NO_FLAGS_SET);
        if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
        {
          cout << "Connection terminated" << endl;
          break;
        }
    
        _strupr(buffer);
    
        numsnt=send(clientSocket, buffer, 
          strlen(buffer) + 1, NO_FLAGS_SET);
        if (numsnt != (int)strlen(buffer) + 1)
        {
          cout << "Connection terminated." << endl;
          break;
        }
    
      } /* while */
    
      /* terminate the connection with the client
         (disable sending/receiving) */
      status=shutdown(clientSocket, 2);
      if (status == SOCKET_ERROR)
        cerr << "ERROR: shutdown unsuccessful" << endl;
    
      /* close the socket */
      status=closesocket(clientSocket);
      if (status == SOCKET_ERROR)
        cerr << "ERROR: closesocket unsuccessful"
          << endl;
    }
    
    INT main(VOID)
    {
      WSADATA Data;
      SOCKADDR_IN serverSockAddr;
      SOCKADDR_IN clientSockAddr;
      SOCKET serverSocket;
      SOCKET clientSocket;
      int addrLen=sizeof(SOCKADDR_IN);
      int status;
      DWORD threadID;
    
      /* initialize the Windows Socket DLL */
      status=WSAStartup(MAKEWORD(1, 1), &Data);
      if (status != 0)
      {
        cerr << "ERROR: WSAStartup unsuccessful" 
          << endl;
        return(1);
      }
    
      /* zero the sockaddr_in structure */
      memset(&serverSockAddr, 0,
        sizeof(serverSockAddr));
      /* specify the port portion of the address */
      serverSockAddr.sin_port=htons(PORT);
      /* specify the address family as Internet */
      serverSockAddr.sin_family=AF_INET;
      /* specify that the address does not matter */
      serverSockAddr.sin_addr.s_addr=htonl(INADDR_ANY);
    
      /* create a socket */
      serverSocket=socket(AF_INET, SOCK_STREAM, 0);
      if (serverSocket == INVALID_SOCKET)
      {
        cerr << "ERROR: socket unsuccessful"
          << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
          cerr << "ERROR: WSACleanup unsuccessful" 
            << endl;
        return(1);
      }
    
      /* associate the socket with the address */
      status=bind(serverSocket, 
        (LPSOCKADDR) &serverSockAddr,
        sizeof(serverSockAddr));
      if (status == SOCKET_ERROR)
        cerr << "ERROR: bind unsuccessful" << endl;
    
      /* allow the socket to take connections */
      status=listen(serverSocket, 1);
      if (status == SOCKET_ERROR)
        cerr << "ERROR: listen unsuccessful" << endl;
    
      while(1)
      {
        /* accept the connection request when 
           one is received */
        clientSocket=accept(serverSocket,
          (LPSOCKADDR) &clientSockAddr,
          &addrLen);
        if (clientSocket == INVALID_SOCKET)
        {
          cerr << "ERROR: Unable to accept connection."
            << endl;
          return(1);
        }
    
        threadID=_beginthread(talkToClient,
          0, (VOID *)clientSocket);
        if (threadID == -1)
        {
          cerr << "ERROR: Unable to create thread"
            << endl;
          /* close the socket */
          status=closesocket(clientSocket);
          if (status == SOCKET_ERROR)
            cerr << "ERROR: closesocket unsuccessful" 
              << endl;
        }
    
      } /* while */
    
    }
    
    ********************client****************************
    
    
    //***************************************************************
    // From the book "Win32 System Services: The Heart of Windows 95
    // and Windows NT"
    // by Marshall Brain
    // Published by Prentice Hall
    //
    // Copyright 1995, by Prentice Hall.
    //
    // This code implements a TCP client.
    //***************************************************************
    
    // msipclnt.cpp
    
    #include <windows.h>
    #include <iostream.h>
    #include <winsock.h>
    
    #define NO_FLAGS_SET 0
    
    #define PORT (u_short) 44965
    #define DEST_IP_ADDR "127.0.0.1"
    #define MAXBUFLEN 256
    
    INT main(VOID)
    {
      WSADATA Data;
      SOCKADDR_IN destSockAddr;
      SOCKET destSocket;
      unsigned long destAddr;
      int status;
      int numsnt;
      int numrcv;
      char sendText[MAXBUFLEN];
      char recvText[MAXBUFLEN];
    
      /* initialize the Windows Socket DLL */
      status=WSAStartup(MAKEWORD(1, 1), &Data);
      if (status != 0)
        cerr << "ERROR: WSAStartup unsuccessful"
          << endl;
    
      /* convert IP address into in_addr form*/
      destAddr=inet_addr(DEST_IP_ADDR);
      /* copy destAddr into sockaddr_in structure */
      memcpy(&destSockAddr.sin_addr, &destAddr,
        sizeof(destAddr));
      /* specify the port portion of the address */
      destSockAddr.sin_port=htons(PORT);
      /* specify the address family as Internet */
      destSockAddr.sin_family=AF_INET;
    
      /* create a socket */
      destSocket=socket(AF_INET, SOCK_STREAM, 0);
      if (destSocket == INVALID_SOCKET)
      {
        cerr << "ERROR: socket unsuccessful" << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
          cerr << "ERROR: WSACleanup unsuccessful" 
            << endl;
        return(1);
      }
    
      cout << "Trying to connect to IP Address: " 
        << DEST_IP_ADDR << endl;
    
      /* connect to the server */
      status=connect(destSocket, 
        (LPSOCKADDR) &destSockAddr,
        sizeof(destSockAddr));
      if (status == SOCKET_ERROR)
      {
        cerr << "ERROR: connect unsuccessful"
          << endl;
        status=closesocket(destSocket);
        if (status == SOCKET_ERROR)
          cerr << "ERROR: closesocket unsuccessful"
            << endl;
        status=WSACleanup();
        if (status == SOCKET_ERROR)
          cerr << "ERROR: WSACleanup unsuccessful" 
            << endl;
        return(1);
      }
    
      cout << "Connected..." << endl;
    
      while(1)
      {
        cout << "Type text to send: ";
        cin.getline(sendText, MAXBUFLEN);
    
        /* Send the message to the server */
        numsnt=send(destSocket, sendText, 
          strlen(sendText) + 1, NO_FLAGS_SET);
        if (numsnt != (int)strlen(sendText) + 1)
        {
          cout << "Connection terminated." << endl;
          status=closesocket(destSocket);
          if (status == SOCKET_ERROR)
            cerr << "ERROR: closesocket unsuccessful"
              << endl;
          status=WSACleanup();
          if (status == SOCKET_ERROR)
            cerr << "ERROR: WSACleanup unsuccessful"
              << endl;
          return(1);
        }
    
        /* Wait for a response from server */
        numrcv=recv(destSocket, recvText, 
          MAXBUFLEN, NO_FLAGS_SET);
        if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
        {
          cout << "Connection terminated.";
          status=closesocket(destSocket);
          if (status == SOCKET_ERROR)
            cerr << "ERROR: closesocket unsuccessful"
              << endl;
          status=WSACleanup();
          if (status == SOCKET_ERROR)
            cerr << "ERROR: WSACleanup unsuccessful"
              << endl;
          return(1);
        }
    
        cout << "Received: " <<  recvText << endl;
    
     } /* while */
      
    }
    I'm trying to run this code on MS .NET 2003 but i'm not getting any lucky
    Last edited by Salem; 09-03-2004 at 04:56 PM. Reason: tagging -

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code tags. Use 'em.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    And still there were no error messages....
    If you can't compile it, paste your compiler error message(s)
    If you can't run it, paste your run-time error(s)

    Lemme guess, your firewall is blocking access between your client and server?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    No there are no compilation errorS when I run this code.Obviously because I took it off a text book. I need step by step instructions to run this code on MS.NET 2003.And for such code there are some dependencies to be set on VisualStudio . I needto know whatthey are.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Obviously because I took it off a text book.
    Oh, if only we could trust all code from text books. The harsh reality is that all books (with a handful of exceptions) present erroneous code. Most of those books do so regularly and embarrassingly to an experienced reader.

    >No there are no compilation errorS when I run this code.
    >to run this code on MS.NET 2003
    That's funny. When I compile your code in Visual C++ .NET 2003, I get errors with the non-standard header iostream.h. Are you sure that there are no errors?
    My best code is written with the delete key.

  6. #6
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    Try to open this code in a project and then run it .I had the same error initially but got over it. it should actually say Build Succeeded. It's the project that actually an executable. As far as the code is concerned, I do agree that text books have erroneous code, but this is from Andrew Tanenbaum's Distributed OS.And I think Andrew makes sure the code works ok

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well for the sake of argument, you should have two projects, each containing either client.c or server.c
    From those, you create client.exe and server.exe
    From two different consoles, run client.exe in one of them and server.exe in the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And I think Andrew makes sure the code works ok
    And I'm sure it did...in 1995.

    >Try to open this code in a project and then run it
    Oddly enough, I get a compiler error.

    But once the code is fixed to compile and wsock32.lib is added to the library list, the programs work properly. I would wager that you either aren't seeing the errors, have done something wacky that allows you to compile invalid code and that causes odd run-time behavior, or you forgot to include the library.
    My best code is written with the delete key.

  9. #9
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    Now how do I add the wsock32.lib file to it.Could you tell me how. Are you running it in a winsock32 console application.I see you've got past the <iostream> error. Do you get a console screen requesting the server for information.

  10. #10
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    I did the same but I probably haven't included the wsock32.lib file .How do you do that by the way.I did run the code in two different environments on the same computer though.But still don't get anything.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Now how do I add the wsock32.lib file to it.
    Go to Project, Properties. In the dialog box, choose the Linker folder and at the bottom select Command Line. Add wsock32.lib to the Additional Options text field and click Apply. The next time you build your project, wsock32.lib will be linked with it.

    >I see you've got past the <iostream> error.
    Yes, by correcting it. The naive fix is very simple, just change <iostream.h> to <iostream> and add using namespace std.

    >Do you get a console screen requesting the server for information.
    Yes, and I get the message echoed to me in upper case when I send the server a string, just like the code suggests.

    >I did run the code in two different environments on the same computer though.
    I don't know if that would work. I imagine it would, but I'm old fashioned and most IDE features are beyond me. What I do is build the server, copy the binary to another location, run it, then open the client project and run it from within the IDE.
    My best code is written with the delete key.

  12. #12
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    Thank you for your help. I'll try what you said. But are there any more dependencies to be added in its properties or just set them to default?.By the way on what environment areyou running the code? under visual C++.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But are there any more dependencies to be added
    Unless I accidentally set a switch without noticing, no. All you need to do is link with the library and life should be good.

    >By the way on what environment areyou running the code?
    My test project in Visual Studio is always custom made from an Empty Project. That way Visual Studio doesn't do anything annoying that I don't want it to.
    My best code is written with the delete key.

  14. #14
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    Hi Prelude,
    I'm trying to run the code right now . as you said I have added wsock32.lib to the LINKER->Command Line field. But these are the error's i'm getting.

    MSIPCLIENT warning LNK4243: DLL containing objects compiled with /clr is not linked with /NOENTRY; image may not run correctly
    MSIPCLIENT error LNK2001: unresolved external symbol __DllMainCRTStartup@12
    MSIPCLIENT fatal error LNK1120: 1 unresolved externals


    Have you got these errors before?.Any suggestions on how to resolve them

  15. #15
    Hail Linus Torvalds
    Join Date
    Sep 2004
    Posts
    14
    Thanks a bunch, prelude
    I finally got the program to run. Though I didn't at first i eventually ended up getting the client and server to run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 09:54 PM
  2. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 09:33 AM
  3. Need winsock select() client & server source code or tutorial
    By draggy in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-19-2006, 11:49 AM
  4. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  5. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21