Solved

Problem creating a socket

Posted on 2002-03-19
4
223 Views
Last Modified: 2010-04-02
I have problems with the following code:

#include <Winsock2.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
     SOCKET ss;
     SOCKADDR_IN ip_addrs;
     int connResult = 0;

     int nErr = 0;
     WORD wVersionRequested;
     WSADATA wsaData;

     //********* Initializing windows socket library ******************
     wVersionRequested = MAKEWORD(1,1);
     if(nErr = WSAStartup(   wVersionRequested,  &wsaData))
    {  
       printf("\nunable to initialize WINSOCK, reason: %d\n",nErr);
       exit(0);
    }
     else printf("WSAStartup OK....!!!\n");
     
     //********** Creating the socket *****************************
     ss = socket( AF_INET,SOCK_STREAM, IPPROTO_TCP );
     if(ss = INVALID_SOCKET) printf("Invalid socket...!!!  ss = %d\n",ss);
     else printf("Socket created OK...!!!\n");

     printf("..................Terminating Application...................\n");
     WSACleanup();
     return 0;
}

when I run this I got this :

WSAStartup OK....!!!
Invalid socket...!!!  ss = -1
Last error was 0

Why am I getting an invalid socket???
Everything seems to be right!
Could it be that I am not using version 1.1, but version 2??

Thanks in advance!
0
Comment
Question by:dyma82
  • 2
4 Comments
 
LVL 1

Author Comment

by:dyma82
ID: 6880294
I missed one line I am getting in the output:

("..................Terminating Application...................


jkr :

I am already including the right library in my linker's path, but now I am getting this other error!
0
 
LVL 86

Expert Comment

by:jkr
ID: 6880297
What does 'WSAGetLastError()' report?
0
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 50 total points
ID: 6880574
>> typo

  if(ss = INVALID_SOCKET) printf("Invalid socket...!!!  ss = %d\n",ss);


You need to  use == (not = )

The fix code is

  if(ss == INVALID_SOCKET)  .....
0
 
LVL 1

Author Comment

by:dyma82
ID: 6880595
thienpnguyen :
You are right. I can not believe I made that mistake!

jkr : Finding out!.....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 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

12 Experts available now in Live!

Get 1:1 Help Now