Solved

Socket question - "active open" vs "passive open"

Posted on 2013-05-14
3
505 Views
Last Modified: 2013-05-15
I am new to sockets.  Trying to communicate with a peice of equipment whose documentation is not very good.  It is set up to use multiple socket connections (one for 'connectivity heartbeat', another for 'main control', another for 'image transmission', etc).  All but one of these is working fine.  For all these sockets, I do the following to configure a socket and make it do what I would call a "blocking listen":

    WSADATA wsaData;
    SOCKET s, S;
    SOCKADDR_IN addr, ADDR;
 
    WSAStartup(MAKEWORD(1, 1), &wsaData);

    s = socket(AF_INET, SOCK_STREAM, 0);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(ThePort);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(s, (SOCKADDR *)&addr, sizeof(addr);
 
    listen(s, 1);

    S = accept(s, (SOCKADDR *)&ADDR, &len);

    int len = recv(S, lpszBuffer, 4096, 0);

It blocks at the "recv" call until something comes in.  Thats all fine.  But I can't get the one last socket to work (the "image transmission" socket).  Finally, I noticed that in the crappy documentation, it shows the other sockets (that I have working) as "passive open" for my side, but the one I'm having trouble with they show as "active open" from my side.  So, I'm not sure what is meant, but I'm guessing it means I should be the 'client' instead of the 'server' for this connection.   It is a connection on which I am to receive a bunch of data, and (apparently) I needn't send out anything -- so I thought that my normal sequence (code above) to open the socket and listen for data would be correct.  But, nothing ever comes, and it looks like I never get past the "accept" call.  

So.... is there a different way I should set up this socket?  Looking at some other socket code, it looks like I see the same preamble as in my code above, but in place of the "bind" call, just do a "connect" call at that point.  But, I don't really know what I'm doing, so I'm hoping a socket expert might have an idea for me!
0
Comment
Question by:tullhead
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39167269
Going just by what you have written above, I would say that you guess in probably on the mark. And so yes, you will want to "connect" the socket rather than bind & accept. The main difference is that instead of using the INADDR_ANY address, you will need to know the address of the piece of equipment. Now in most cases this should be easy, just get it from your ADDR struct (the one you passed to "accept()", it fills the struct with the address info of the remote end that is connecting to you). So you probably just need to change the port number within ADDR, to the port that you have to connect to for "image transmission". So something along the lines of (add this after the code you have above, and also add appropriate error checking, etc)...
SOCKET imageSocket = socket(AF_INET, SOCK_STREAM, 0);
ADDR.sin_port = htons(ImagePort);
connect(imageSocket, (SOCKADDR *)&ADDR, len);

Open in new window

If the "connect()" call succeeds, you can now call "recv()" etc on the imageSocket to retrieve any data that the equipment pushes back to you.
0
 

Author Closing Comment

by:tullhead
ID: 39169849
It works!!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39170032
It works!!
That's good, glad I could help! :)
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

831 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