Link to home
Start Free TrialLog in
Avatar of dyma82
dyma82

asked on

Trying to open a socket

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.
ASKER CERTIFIED SOLUTION
Avatar of newmang
newmang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dyma82

It would be much easier for us to help you if you wouldn't post the same question in different areas. We try to focus on one posting. If you think there are more TAs you could get help from, post a link with 0 points to the original question and experts will follow that link.

The original question is at:
https://www.experts-exchange.com/cplusprog/Q.20278486.html

======
Werner
Avatar of dyma82
dyma82

ASKER

Yes, that is true. I just wanted to get answers from to different kind of developers C++ and C. However, that is a good idea since I won't be lossing my other 50 points. I'll keep that in mine thanks.

newmang:

Yes, I think that is probably the problem. I thought I only had to #include <Winsock2.h>

How do I know which is the library I have to include in my linker's path. I mean the library which I guess would back up <Winsock2.h>???

I appologize if this is a silly question, but I am a Java programmer that all of a sudden find himself immerse in the super detailed world of C and C++.

Thansk in advance.
Avatar of dyma82

ASKER

Thanks a lot. That was the problem, and I found that the library I was missing was Ws2_32.lib. As soon as I include it it worked.

Thanks again!