?
Solved

Trying to open a socket

Posted on 2002-03-18
4
Medium Priority
?
224 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
[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
4 Comments
 
LVL 4

Accepted Solution

by:
newmang earned 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

719 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