Solved

Problem creating a socket

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

628 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