Thread: Server Side program

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    307

    Server Side program

    ok went back to working on my server side TCP/IP program.

    so far I am able to create, listen, and connect from a client.

    just having probs with what to do next.

    my goal is the following:

    1server running
    2client connects
    3server sends "8888" to show that it is connected and waiting to receive
    4client sends a number to server
    5server ack with sending "8888" for good, or "9999" for bad

    then repeating 4-5, until client sends 0000 for exit

    the following is the start of my program:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    
    #pragma comment(lib,"ws2_32.lib") //Winsock Library
    
    //----- Defines ---------------------------------------------------------------
    #define  PORT_NUM   8888    // Arbitrary port number for the server
    
    int main(void)
    {
      WSADATA wsaData;                      // Stuff for WSA functions
      int                  welcome_s;       // Welcome socket descriptor
      struct sockaddr_in   server_addr;     // Server Internet address
      int                  connect_s;       // Connection socket descriptor
      struct sockaddr_in   client_addr;     // Client Internet address
      struct in_addr       client_ip_addr;  // Client IP address
      int                  addr_len;        // Internet address length
      char                 out_buf[8];   // Output buffer for data
      char                 in_buf[8];    // Input buffer for data
      int                  retcode;         // Return code
     int number;
    
    // This stuff initializes winsock
     if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
      {
       fprintf(stderr,"Server: WSAStartup failed.\n", WSAGetLastError());
      }
     else
      {
       printf("Server: WSAStartup Success.\n");
      }
      
    // >>> Step #1 <<<
    // Create a welcome socket
    //   - AF_INET is Address Family Internet and SOCK_STREAM is streams
    
      welcome_s = socket(AF_INET, SOCK_STREAM, 0);
      if (welcome_s < 0)
      {
       fprintf(stderr,"Server: Could not create socket.\n", WSAGetLastError());
      }
     else
      {
       printf("Server: Socket Created.\n");
      }
     
    
      // >>> Step #2 <<<
      // Fill-in server (my) address information and bind the welcome socket
      
    memset((char *)&server_addr,0,sizeof(server_addr)); //Clear server address
     
      server_addr.sin_family = AF_INET;                 // Address family to use
      server_addr.sin_port = htons(PORT_NUM);           // Port number to use
      server_addr.sin_addr.s_addr = htonl(INADDR_ANY);  // Listen on any IP address
       if (bind(welcome_s, (struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)
      {
       fprintf(stderr,"Server: bind() failed with error %d\n", WSAGetLastError());
      }
     else
      {
       printf("Server: Socket bound.\n");
      }
     
    
      // >>> Step #3 <<<
      // Listen on welcome socket for a connection
      
    listen(welcome_s, 1);
     
     // >>> Step #4 <<<
      // Accept a connection.  The accept() will block and then return with
      // connect_s assigned and client_addr filled-in.
      
    printf("Waiting for accept() to complete... \n");
      addr_len = sizeof(client_addr);
      connect_s = accept(welcome_s, (struct sockaddr *)&client_addr, &addr_len);
      if (connect_s < 0)
      {
                fprintf(stderr,"Server: listen() failed with error %d\n", WSAGetLastError());
            }
     else
      {
       printf("Server: listen() is OK.\n");
      }
      
    // Copy the four-byte client IP address into an IP address structure
      
    memcpy(&client_ip_addr, &client_addr.sin_addr.s_addr, 4);
      
    // Print an informational message that accept completed
      
    printf("Accept completed (IP address of client = %s  port = %d) \n",
        inet_ntoa(client_ip_addr), ntohs(client_addr.sin_port));

    where do I go with this now?

    thank you
    Last edited by Crossfire; 08-14-2015 at 03:24 PM. Reason: code clean up

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The Windows sub-forum?

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    if there was a windows sub-forum under cprogramming then I would have posted this there. instead I posted it under the c programming forum, because that is what this is, c programming!

    thank you for lack of help

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    not exactly a sub-forum of c programming....

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Well, your code is not exactly C either, because it uses Microsoft extensions only available on the Windows platform.

    Believe or not, but I would have been happy to help you, but I can't, because I only have access to standard C tools, plus some BSD and POSIX extensions. I cannot even compile your code, even less run it should it compile.

    In fact, standard C (C89, C99, or C11) does not provide networking facilities. There are libraries that abstract the low-level differences away -- for example apr and MQ -- but it seems those are quite rare to be used on the Windows platform.

    Also, basically all platforms except for Windows implement Berkeley sockets for networking. Most current OSes and C libraries provide POSIX sockets with a few minor differences (reentrant, better facilities for e.g. resolving names, and so on). Those, I would be happy to help with, because I can.

    There is a ten-year-old document here discussing the history of the Windows interfaces, and their differences to BSD sockets (or to POSIX sockets, which as I said, are close to BSD sockets). I don't know how close they are to todays Windows library features, and the document does not talk about POSIX sockets -- for example, that POSIX people use getaddrinfo() and getnameinfo() for name resolution. (Note: although the links are to a project called "Linux man pages", I only linked there because those man pages clearly indicate on each page, which standard or extension defines the functions. It's the most reliable set I know of, and doesn't push Linux on you at all. It is "Linux" because most Linux distributions use it for their man pages, that's all.)

    If you're willing to do the translation from POSIX/BSD sockets to Winsock (or whatever it is called nowadays), and don't mind that I cannot test or even compile your code, I could try to help you.

    However, moving this thread to the Windows Programming sub-forum would mean other members, those using and developing on the Windows platform, would read your post. There, there would be no issues on what is C, or portable; as everyone would be using tools appropriate for the Windows platform instead.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    now that makes sense!!!

    i was trying to take nix winsock pages (as it seems those are the majority out there) and use the "if you have windows" sections, but yeah all screwed up. i did post this to the windows section, because sadly, i could not MOVE or DELETE this post.

    POSIX i have not had any chance to even look at that at all. i would have no problem compiling my code to be closer to a more "universal C", as opposed to a windows only one. even though this client server setup will most likely be ran on a windows lan (MAYBE client running through HTML, or droid app, with server being windows or nix server, but all this one step at a time!)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,251
    *moved to Windows Programming forum*

    A clue that your topic might belong here is if you #include <windows.h> and really need it. Alternatively, you could have posted in the Networking/Device Communication forum, especially if you're open to the use of other networking libraries.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server side question.
    By ~Kyo~ in forum Game Programming
    Replies: 6
    Last Post: 02-14-2011, 09:19 PM
  2. Server Side
    By lruc in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 03-04-2009, 07:14 PM
  3. server side C++ question
    By twist2b in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2008, 06:09 PM
  4. Running SQL queries server side with C++?
    By taelmx in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-10-2006, 07:59 PM
  5. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 02:58 PM

Tags for this Thread