Solved

Problem creating a socket

Posted on 2002-03-19
4
215 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

18 Experts available now in Live!

Get 1:1 Help Now