Thread: sending variable from server program to client program (with ack)

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

    sending variable from server program to client program (with ack)

    ok to start, windows based program, using pelles c ide

    winsock2, using "sample" codes, able to connect, BUT no clue what 1/2 of the codes do, and most of the coding is for nix based systems!

    trying to understand, and implement a minimal amount of code to add to a current menu based program!

    being able to open connection, wait for input from server, receive input, acknowledge the input, then "act" then return to waiting for new input.

    or vice versa, server waiting on client (but then server would have to send all the current status`s to client, so that client could be in real time, prob by sending an array from server to client)

    not asking anyone to DO this for me (as some of you already know I don't play that game, as "tutor" or "student" on here!)

    just need to know ON WINDOWS!!! where to even start!!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    This may help you:
    Using Winsock (Windows) - MSDN
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Or Beej's Guide to Network Programming

    Apart from some Windows specific startup and shutdown, network programming is pretty similar between most systems.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    beej`s guide was good, but confusing trying to edit it to fix the errors I get from the windows side!

    the MSDN is more API then I want to deal with!

    is there a "simplier" c based, no api type programming for sockets?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Or you could just post your error messages, and then learn something new.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok this is one I found made for windows.
    no idea what 3/4 of it means or how it does what it does!
    especially the WPI type commands (something.something.something)

    trying to take this idea,
    get variable a,b,c from server, and send to client(s)
    (late on going to tey to sent data to specific clients)

    receive message that data was received

    then wait for next input

    quit session if exit.

    Code:
    /*
     Server on port 8888
    */
    #include<io.h>
    #include<stdio.h>
    #include<winsock2.h>
    #pragma comment(lib,"ws2_32.lib") //Winsock Library
    int main(void)
    {
     WSADATA wsa;
     SOCKET s , new_socket;
     struct sockaddr_in server , client;
     
     if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
     {
      printf("Failed. Error Code : %d",WSAGetLastError());
      return 1;
     }
     
     //Create a socket
     if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
     {
      printf("Could not create socket : %d" , WSAGetLastError());
     }
     //Prepare the sockaddr_in structure
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = INADDR_ANY;
     server.sin_port = htons( 8888 );
     
     //Bind
     if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
     {
      printf("Bind failed with error code : %d" , WSAGetLastError());
      exit(EXIT_FAILURE);
     }
     
     //Listen to incoming connections
     listen(s , 3);
     
     //Accept and incoming connection
     puts("Waiting for incoming connections...");
     
     c = sizeof(struct sockaddr_in);
     
     if( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
     {
      puts("Connection accepted");
      
      //Reply to the client
      message = "Hello Client , I have received your connection. But I have to go now, bye\n";
      send(new_socket , message , strlen(message) , 0);
     }
     
     if (new_socket == INVALID_SOCKET)
     {
      printf("accept failed with error code : %d" , WSAGetLastError());
      return 1;
     }
     closesocket(s);
     WSACleanup();
     
     return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    So apart from undeclared message, what else do you see?

    I don't have windows, so you have to do all the detailed testing.

    Try stepping through the code using a debugger, or add plenty of
    printf("DEBUG: got to line %d\n", __LINE__ );

    or
    printf("DEBUG: var is %d\n", someIntVariable );
    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.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    sorry I missed copy and paste of a part!

    basically, I do not know winsock, so I am basically asking for a "flow chart" of sorts of how to do this

    would rather start a program myself and do it, instead of using something like this program!


    so I need a walkthrough. like step X do this. so I can code it.

    like first step would be to include the librarys, second step variables, third step init socket.

    after that I am lost!

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok started a new from nothing, going down steps...

    Code:
    #include <winsock2.h>
    #include <Windows.h>
    #include <stdio.h>
    #define PORT "8888"
    int main(void)
    {
     WSADATA wsaData;
     int socket_desc;
     struct sockaddr_in server_addr;
     if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
      {
       fprintf(stderr, "WSAStartup failed.\n");
       exit(1);
      }
     else
      {
       fprintf(stderr, "WSAStartup Success.\n");
      }
    
        socket_desc = socket(AF_INET , SOCK_STREAM , 0);
         
     if (socket_desc == -1)
      {
       printf("Could not create socket.\n");
      }
     else
      {
       printf("Socket Created.\n");
      }
     
     int retcode = bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr));
     
     if (retcode == -1)
      {
       printf("Could not bind socket.\n");
      }
     else
      {
       printf("Socket bound.\n");
      }
     
     WSACleanup();
     return 0;
    }
    so far, starts sock, opens socket, fails with the bind to socket part! (learning it as I go!)

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    changed the code to
    Code:
    #include <winsock2.h>
    #include <Windows.h>
    #include <stdio.h>
    #define PORT 8888
    int main(void)
    {
     WSADATA wsaData;
     int socket_desc;
     struct sockaddr_in server_addr;
     
     server_addr.sin_family = AF_INET;
        
        
     if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
      {
       printf("WSAStartup failed.\n");
       exit(1);
      }
     else
      {
       printf("WSAStartup Success.\n");
      }
    
        socket_desc = socket(AF_INET , SOCK_STREAM , 0);
         
     if (socket_desc == -1)
      {
       printf("Could not create socket.\n");
      }
     else
      {
       printf("Socket Created.\n");
      }
     
       
     int retcode = bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr));
     
     if (retcode < 0)
      {
       fprintf(stderr,"Server: bind() failed with error %d\n", WSAGetLastError());
      }
     else
      {
       printf("Socket bound.\n");
      }
     
     WSACleanup();
     return 0;
    }
    BTW using fprintf just for my errors!
    getting "bind() failed with error 10049"

    being an address prob I think, but no idea how, why, or anything!

    any ideas tips etc? like I said, this is all new to me, so looking to learn it as I go!



    //-----UPDATE-----//

    fixed it by
    Code:
    server_addr.sin_addr.s_addr = INADDR_ANY;
    NOW WHAT? lol seriously what is my next step?
    Last edited by Crossfire; 02-25-2015 at 09:08 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    > NOW WHAT? lol seriously what is my next step?
    You could look up the error message on MSDN
    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    You could google your error message, perhaps say
    c - bind() fails with windows socket error 10049 - Stack Overflow

    Which recommends doing something you're not doing.
    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.

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    guess you missed the "fixed the bind"

    now what ment, what is my next step!

    working on the accept() part.

    will post code in a few!

    //-----update-----//
    Code:
    #include <winsock2.h>
    #include <Windows.h>
    #include <stdio.h>
    #define PORT 8888
    int main(void)
    {
     WSADATA wsaData;
     int socket_desc;
     struct sockaddr_in server_addr, client_addr;
     
     server_addr.sin_family = AF_INET;
     server_addr.sin_addr.s_addr = INADDR_ANY;
     server_addr.sin_port = htons(PORT);
         
     if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
      {
       fprintf(stderr,"Server: WSAStartup failed.\n", WSAGetLastError());
      }
     else
      {
       printf("Server: WSAStartup Success.\n");
      }
    
        socket_desc = socket(AF_INET , SOCK_STREAM , 0);
         
     if (socket_desc == -1)
      {
       fprintf(stderr,"Server: Could not create socket.\n", WSAGetLastError());
      }
     else
      {
       printf("Server: Socket Created.\n");
      }
     
       
     int retcode = bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr));
     
     if (retcode < 0)
      {
       fprintf(stderr,"Server: bind() failed with error %d\n", WSAGetLastError());
      }
     else
      {
       printf("Server: Socket bound.\n");
      }
     if (listen(socket_desc,5) == SOCKET_ERROR)
      {
                fprintf(stderr,"Server: listen() failed with error %d\n", WSAGetLastError());
            }
     else
      {
       printf("Server: listen() is OK.\n");
      }
        printf("Server: I'm listening and waiting connection on port %d\n\n", PORT);
     while(1)
     {
      int fromlen =sizeof(client_addr);
            
      if (accept(socket_desc, (struct sockaddr*)&client_addr, &fromlen) < 0)
       {
        fprintf(stderr,"Server: accept() error %d\n", WSAGetLastError());
       }
      else
       {
        printf("Server: accept() is OK.\n");
        printf("Server: accepted connection from %s, port %d\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));
       }
     }
     WSACleanup();
     return 0;
    }
    OK, have a few errors, mostly "infiniate and code not reachable" no prob to me, because I am not done yet!
    but lost to "Result of comparison is constant."
    now really don't know what to do next!!!

    do I keep the loop where it is, and put a receive in the loop, or do I unloop the listen, and make a loop with receive?

    and also might need help on the receive part!
    Last edited by Crossfire; 02-25-2015 at 03:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server-Client Program
    By rocksoad23 in forum C Programming
    Replies: 2
    Last Post: 12-18-2012, 05:38 PM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. Server and client in the same program
    By smithx in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-25-2008, 09:30 PM
  4. Replies: 2
    Last Post: 07-24-2008, 06:05 AM
  5. Client-server program
    By erictran in forum C Programming
    Replies: 3
    Last Post: 09-18-2006, 07:59 PM