Thread: windows network programming noob question

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    windows network programming noob question

    Hey all,
    I am trying to learn network programming but in a very simple form. I looked at it a bit in linux and it made sense but i want it to work in windows. Here is my code. I am getting a error when trying to bind the socket. I'm using dev C++

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <winsock.h>
    
    int main()
    {
      int socket_desc;
      struct sockaddr_in address;
      int addrlen;
      int new_socket;
    
      if ((socket_desc=socket(AF_INET,SOCK_STREAM,0))==0)
      {
        perror("Create socket");
        exit(EXIT_FAILURE);
      }
    
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;
      address.sin_port = htons(33440);
      
      if (bind(socket_desc,(struct sockaddr *)&address,sizeof(address)) == -1)
      {
        perror("bind");
        exit(EXIT_FAILURE);
      }
    
      if (listen(socket_desc,3)<0)
      {
        perror("listen");
        exit(EXIT_FAILURE);
      }
    
      addrlen=sizeof(address);
      if ((new_socket=accept(socket_desc,(struct sockaddr *)&address,&addrlen))<0)
      {
        perror("accept");
        exit(EXIT_FAILURE);
      }
      printf("New socket is %d\n",new_socket);
    
      sleep(10);
    
      close(socket_desc);
    }
    -- Will you show me how to c++?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And the error you're receiving is???

  3. #3
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    The errors from my Dev-C++ state:

    sleep is undeclared
    close is undeclared

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sleep is called Sleep() in Windows [and you probably want to put 10000 as the parameter, as Unix sleep takes a time in seconds, whilst Sleep takes millisecond times], and you probably want to use closesocket() rather than close().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    You also need to use WSAStartup().

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You also need to work on your communication skills to get effective help. Here are a couple of specific hints:

    -- You should have relayed your actual errors in your first post.
    -- You should have relayed that you were unable to compile and/or link the program. You see, your initial message would seem to indicate that you were successful in compiling and running the program.
    -- Don't give false information. Your initial message would lead us to believe that you were receiving an error from the perror call following the failed bind. In fact, this problem has absolutely nothing to do with bind. When in doubt, just give the facts...all the facts you are able.

  7. #7
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Hey, sorry for the confusion. The error i was getting was during run time when trying to bind the socket. But i figured out the problem and it was indeed the WSAStartup().
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. Simple Windows Question
    By samGwilliam in forum Windows Programming
    Replies: 2
    Last Post: 09-16-2008, 01:50 PM
  4. newbie question windows programming
    By gemini_shooter in forum Windows Programming
    Replies: 4
    Last Post: 04-14-2005, 03:28 AM
  5. Wireless network question
    By dirkduck in forum Tech Board
    Replies: 1
    Last Post: 12-26-2002, 04:56 PM