Socket Programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrError
    New Member
    • Dec 2012
    • 11

    Socket Programming

    I have just started to work in socket programming.. I am getting these errors:
    "undefined reference to `__imp_socket'"
    "ld returned 1 exit status"
    my compiler is dev c++ 5.6.1
    here is my code


    Code:
    #include <iostream>
    #include <conio.h>	
    #include <winsock2.h>
    using namespace std;
    int main()
    {
    	int descriptor;
    
    	if ((descriptor = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
    	{
    		cout << "cannot create socket";
    		return 0;
    	}
    
    	cout << "created socket: descriptor= " << descriptor;
    	return 0;
    }
    Last edited by Rabbit; Feb 16 '14, 04:38 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to link with Ws2_32.lib.

    Be sure this .lib is in the list of linker dependencies.

    Comment

    • MrError
      New Member
      • Dec 2012
      • 11

      #3
      and how can i do that.!!!

      Comment

      • MrError
        New Member
        • Dec 2012
        • 11

        #4
        i went to
        project->project options->parameters->add library
        and then browse
        C:\Program Files (x86)\Microsoft SDKs\Windows\v7 .1A\Lib
        and added Ws2_32.lib... but still getting the error :(

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your code compiles and links using Visual Studio.NET 2008. All I did was go to project properties and add the name Ws2_32.lib as linker input but not the path.

          Visual Studio has a linker property named Additional Library Directories and it is there you add all possible paths to all possible libraries for your project.

          I didn't have to mess with additional paths paths since Ws2_32.lib was already in a standard directory. I only mentioned additional paths in case your machine is set up different from mine.

          This Ws2_32.lib is not a real library but instead is a stub used to load the winsock dll and fetch your proc address.

          Comment

          • MrError
            New Member
            • Dec 2012
            • 11

            #6
            My linking problem is resolved.. thanks to you.. but now i am having another problem..
            why my code is not creating the socket... the descriptor is always -1..!!
            Here is the code.

            Code:
            #include "stdafx.h"
            #include <iostream>
            #include <conio.h>	
            #include <winsock2.h>
            using namespace std;
            int main()
            {
            	int descriptor;
            
            	if ((descriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1);
            	{
            		cout << "cannot create socket";
            		_getch();
            		return 0;
            	}
            	cout << "created socket: descriptor= " << descriptor;
            	_getch();
            	return 0;
            }
            Last edited by Rabbit; Feb 16 '14, 10:46 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data. Second warning.

            Comment

            • MrError
              New Member
              • Dec 2012
              • 11

              #7
              oh man...
              i am too dumb.. the traditional mistake.. ";"
              anyway.. thanks for your help.. i appreciate that.. :)

              Comment

              Working...