Solved

Problem creating a socket

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 123
Create a path if not exists 7 76
FMX TCameraComponent Problem 2 68
Issues with C++ Class 19 92
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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

19 Experts available now in Live!

Get 1:1 Help Now