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
Solved

Problem creating a socket

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 gracefully close the c++ 11 thread? 3 108
White board coding practice 3 90
FMX TCameraComponent Problem 2 83
Error creating a new C++ project in ,net 20 41
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

856 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