Thread: I have problem in the code (creating socket).

  1. #1
    Registered User
    Join Date
    Jan 2013

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

    Can some one tell me when is the problem in this 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);
            cout <<"socket fail" << endl;
        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;
        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
    Registered User
    Join Date
    Feb 2013
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  4. #4
    Registered User
    Join Date
    Jan 2013
    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, 07:15 AM
  2. Socket operation on non-socket problem
    By MiniComa in forum C Programming
    Replies: 9
    Last Post: 08-20-2012, 12:59 PM
  3. Replies: 7
    Last Post: 09-28-2010, 04: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, 03:58 AM