Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to open a connection socket

Posted on 2002-03-18
4
Medium Priority
?
205 Views
Last Modified: 2012-05-04
This is the code I am using to try to open a socket using
win32 APIs :

#include <Winsock2.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
     char url[14];
     SOCKET socket;
     SOCKADDR_IN ip_addrs;
     int connResult = 0;

     //Setting up the IP Address structure
     ip_addrs.sin_family = SOCK_STREAM;
     ip_addrs.sin_port = 25;
     ip_addrs.sin_addr.S_un.S_un_b.s_b1 = 189;
     ip_addrs.sin_addr.S_un.S_un_b.s_b2 = 136;
     ip_addrs.sin_addr.S_un.S_un_b.s_b3 = 145;
     ip_addrs.sin_addr.S_un.S_un_b.s_b4 = 10;

     printf("Starting mail application \n");
     printf("Name length = %d\n",sizeof(ip_addrs));

     
     connResult = connect(socket,(sockaddr*)&ip_addrs,sizeof(ip_addrs));
     if(connResult==0) printf("Connection OK...!\n");
     else printf("Connection Failed...!!!\n");
     return 0;
}

I think everything is ok. However, compilation fails and throws the following error :

Linking...
MainBlock.obj : error LNK2001: unresolved external symbol __imp__connect@12
Debug/MyMail2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

MyMail2.exe - 2 error(s), 2 warning(s)

Any idea what could be happening? If not, please would you
let me know how to open a socket connection using win32 APIs?

NOT MFC PLEASE!

Thanks a lot for your time.
0
Comment
Question by:dyma82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6878232
You are missing the import library - add

#pragma comment ( lib, "ws2_32.lib")

to your code. Furthermore, you need to call 'WSAStartup()' before using sockets, e.g.

#include <Winsock2.h>
#include <string.h>
#include <stdio.h>

#pragma comment ( lib, "ws2_32.lib")

int main(int argc, char* argv[])
{
    char url[14];
    SOCKET socket;
    SOCKADDR_IN ip_addrs;
    int connResult = 0;

    int             nErr                =   0;
    WORD            wVersionRequested;
    WSADATA         wsaData;
 
    wVersionRequested   =   MAKEWORD(   1,  1);

    if  (   nErr = WSAStartup   (   wVersionRequested,  &wsaData))
        {  
            printf  (   "\nunable to initialize WINSOCK, reason: %d\n",
                        nErr
                    );
            exit    (   0);
        }


    //Setting up the IP Address structure
    ip_addrs.sin_family = SOCK_STREAM;
    ip_addrs.sin_port = 25;
    ip_addrs.sin_addr.S_un.S_un_b.s_b1 = 189;
    ip_addrs.sin_addr.S_un.S_un_b.s_b2 = 136;
    ip_addrs.sin_addr.S_un.S_un_b.s_b3 = 145;
    ip_addrs.sin_addr.S_un.S_un_b.s_b4 = 10;

    printf("Starting mail application \n");
    printf("Name length = %d\n",sizeof(ip_addrs));

   
    connResult = connect(socket,(sockaddr*)&ip_addrs,sizeof(ip_addrs));
    if(connResult==0) printf("Connection OK...!\n");
    else printf("Connection Failed...!!!\n");
    return 0;
}


0
 
LVL 1

Author Comment

by:dyma82
ID: 6879868
Is not enough to "#include <Winsock2.h>" ?????

Do I still have to include the statement #pragma comment ( lib, "ws2_32.lib") ????
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6879883
>>Is not enough to "#include <Winsock2.h>" ?????

No.

>>Do I still have to include the statement #pragma comment ( lib, "ws2_32.lib") ????

Yes.
0
 
LVL 1

Author Comment

by:dyma82
ID: 6879933
Thanks. That was the problem, but I would have understand it better if you would have told me that your solution meant to add the library Ws2_32.lib in the linker's path.

Any way thanks a million again.
p.s. You guys are doing a terrific job!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question