Thread: sending numbers through sockets

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

    sending numbers through sockets

    ok, this will be a post that i dont have code for yet!

    not HW, not employment, just me learning (yet again!)

    i have been studying sockets, and confused as hell, mostly because almost everything i read is for *nix and not windows!

    but also confused on how any of it works as most of the tutorials do it different!

    i want to write a program(s) so that the client sits and waits for a scanf to recieve an int, then send to server

    server then recieves the int, processes it, then goes back to sit and wait for new input.

    to start with, where do i start! server side, or client side?

    and also what order do i have to go in my program before the loop to set up the socket, and listen?

    and then what gets looped to keep the connection open and wait for new recieve?

    THEN

    client side same thing!

    sockets are completely new to me, and not being a student, i have no where to go to learn it!

    thanks all!

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    I lied!!! i have the start of a server code, it isnt the best, but i pieced it together from about 4 diff tutorials, and sources, removed the win32 or *nix "choices" and fixed a few probs with the linker libs from a few of the other sources.

    how bad does this look, and what is next for server side? thank you

    Code:
    #include<stdio.h>
    #include<winsock2.h>
    #include<windows.h>
    #pragma lib "Ws2_32.lib"
     
    int main(void)
    {
     WORD wVersionRequested;
     WSADATA wsaData;
     wVersionRequested = MAKEWORD(2, 2);
     SOCKET m_socket;
     if ((WSAStartup(wVersionRequested, &wsaData)) != 0)
      {
           printf("The Winsock dll not found!\n");
       return 0;
      }
     m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (m_socket == INVALID_SOCKET)
      {
          printf("Error at socket(): %ld.\n", WSAGetLastError());
          WSACleanup();
          return 0;
      }
     struct sockaddr_in service;
     service.sin_family = AF_INET;
     service.sin_addr.s_addr = inet_addr("127.0.0.1");
     service.sin_port = htons(55555);
     if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
      {
          printf("bind() failed: %ld.\n", WSAGetLastError());
          closesocket(m_socket);
          return 0;
      }
     if (listen(m_socket, 1) == SOCKET_ERROR)
      {
          printf("listen(): Error listening on socket %ld.\n", WSAGetLastError());
      }
     else
      {
          printf("I'm waiting for connections...\n");
      }
     return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    IMO this is one of the best online socket programming guides out there: Beej's Guide to Network Programming.

    The Windows socket libraries are based heavily on the BSD sockets (what you call *NIX, which is fairly accurate). Much (> 95%) of the info will translate really well.

    I would start with a basic echo sever. Something that reads one int over the socket and prints it out, something like "server got 42". Then you write a dumb client to send numbers to the server, say 1..10 in a for loop. that will ensure you have your setup code correctly for each, and that your basic read/write loops are done. Then you move on to adding processing on the server, and a read-from-user loop on the client.

    When developing, make sure you check the return value of every socket/IO related function. Also, print a useful error message (i.e. not just "error"). Use perror() or strerror() + errno to get a message that will tell you what went wrong. Lots of basic logging/debug/status printfs help too, to identify where your code might be stuck, waiting or infinite looping, etc.

    By default, sockets are blocking, so a read will wait for data to arrive before returning (just like scanf blocks until the user enters data). A write will also block until it can actually hand off the data to the kernel to be sent along the network.

    One thing to note, however, is that you may get a partial read or write (unlikely with very small packets), but your code should account for it. If you're expecting a 4-byte integer, read may only get 2 bytes for now, so check it's return value and if you don't have a complete "packet" (an int in your case), read again (asking for only the remaining number of bytes). Same strategy goes for writing.

    Hope that helps. If you're having trouble, just start coding, copying verbatim from Beej's guide if you need to, and if you get stuck or have questions, post the code and ask.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    never used perror or strerror + errno before, ill look into that
    the blocking is exactly what i want, to send an int, then the client not "looping" to accept the new input untill the server responds that the "command was completed sucessfully"
    didnt think a 4byte int would be "partial" in a packet!
    thank you, i figure a day or two before i post updated code, or a question!

    i like the idea of the 1-10 for testing!

    btw i have seen many samples that after a send/recieve from client/server, the socket is closed.

    do i have to close then reopen the socket connection every send, or connect, then do what i need, then when exit close the socket?

    thank you very much, see you in a day or two!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Crossfire View Post
    never used perror or strerror + errno before, ill look into that
    errno is the global error variable that gets set by so many of the standard functions. strerror gives you a string based on it's value, perror prints that string to stderr.
    Quote Originally Posted by Crossfire View Post
    btw i have seen many samples that after a send/recieve from client/server, the socket is closed.

    do i have to close then reopen the socket connection every send, or connect, then do what i need, then when exit close the socket?
    You close the socket when you're done talking, not after every send. Samples do that just because it's quick test code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a banner using sockets
    By DeanWinchester in forum Networking/Device Communication
    Replies: 4
    Last Post: 12-21-2012, 04:55 AM
  2. sending a file across sockets.
    By mushy in forum C Programming
    Replies: 23
    Last Post: 10-05-2010, 10:02 AM
  3. Need help with sending an int via sockets.
    By pheeze in forum C Programming
    Replies: 5
    Last Post: 11-10-2009, 02:03 PM
  4. sending structures through sockets
    By sunjayc99 in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-01-2008, 07:43 PM
  5. Sending Hex to sockets?
    By chrisjmoss in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-03-2008, 05:50 AM