Solved

Trying to open a socket

Posted on 2002-03-18
4
222 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to creat good software interface ? 1 71
Arduino EDI - Programming language 3 116
What is atomic operation? 6 67
Why  my code (program) build with old compiler? 11 112
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

733 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