I have problem in the code (creating socket).

This is a discussion on I have problem in the code (creating socket). within the C++ Programming forums, part of the General Programming Boards category; Can some one tell me when is the problem in this code ??? Code: #include <iostream> #include <WinSock.h> #pragma comment(lib, ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    Exclamation I have problem in the code (creating socket).

    Can some one tell me when is the problem in this code ???
    Code:
    #include <iostream>
    #include <WinSock.h>
    #pragma comment(lib, "wsock32.lib")
    
    
    using namespace std;
    int main()
    {
        WSAData d_WSA_data;
        SOCKET sck;
        sockaddr_in server;
        unsigned long address;
    
    
        WSAStartup(MAKEWORD(1 , 1), &d_WSA_data);
    
    
        sck = socket(AF_INET, SOCK_STREAM, 0);
    
    
        if(sck==SOCKET_ERROR)
        {
            cout <<"socket fail" << endl;
            cin.get();
        }
    
    
        cout << "enter your server ip" << endl;
    
    
        cin >> address;
    
    
        memcpy((char *)&server.sin_addr, &address, sizeof(address));
    
    
        server.sin_family = AF_INET;
        server.sin_port = htons(80);
        if(connect(sck,(sockaddr *)&server, sizeof(server) == SOCKET_ERROR)){
                cout << "text... " << endl;
                cin.get();
        }
    
    
        closesocket(sck);
        WSACleanup();
    
    
    
    
        return 0;
    }
    It shows that I have made mistake in:
    WSAStartup(MAKEWORD(1 , 1), &d_WSA_data);
    Last edited by Roleaxes; 02-27-2013 at 01:18 PM.

  2. #2
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    So... what's the mistake? What's the error? Compiler? Linker? Logic error?

    You're not giving enough information . Your code is only a piece of the puzzle.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    WSAStartup function (Windows)
    Study the includes carefully.
    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
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    Ok thanks for the information guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error while creating socket....
    By sanddune008 in forum C Programming
    Replies: 3
    Last Post: 09-26-2012, 08:15 AM
  2. Socket operation on non-socket problem
    By MiniComa in forum C Programming
    Replies: 9
    Last Post: 08-20-2012, 01:59 PM
  3. Replies: 7
    Last Post: 09-28-2010, 05:12 AM
  4. Error in creating socket in a client (UDP)
    By ferenczi in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-27-2008, 11:11 AM
  5. irc socket code
    By lost_in_logic in forum C Programming
    Replies: 3
    Last Post: 08-16-2005, 04:58 AM

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