Solved

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

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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

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 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 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.

739 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