Solved

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

Posted on 2013-05-14
3
500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

861 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

25 Experts available now in Live!

Get 1:1 Help Now