Solved

Trying to open a socket

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should CArray be used for a list of pointers in C++? 19 119
Copying WordPress Pages 5 88
Global Keyboard Hooks Blocked 4 81
Why is compiler in oracle server ? 9 75
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…
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 columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

828 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