Solved

Trying to open a socket

Posted on 2002-03-18
4
218 Views
Last Modified: 2013-12-14
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
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
newmang earned 50 total points
ID: 6878317
The error you are getting is not a coding error, it indicates that the linker cannot find one of the SDK functions it needs to include to create the executable. Are you including the windows socket library file(s) in your linker path? You need to add the relevent winsock*.lib file to your linker library path.

Cheers - Gavin
0
 
LVL 11

Expert Comment

by:griessh
ID: 6879861
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:
http://www.experts-exchange.com/cplusprog/Q.20278486.html

======
Werner
0
 
LVL 1

Author Comment

by:dyma82
ID: 6879891
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.
0
 
LVL 1

Author Comment

by:dyma82
ID: 6879917
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now