Commit eea539e1 authored by Bill Hoffman's avatar Bill Hoffman
Browse files

ENH: add ability to connect to a random port

parent 78aedbac
......@@ -36,7 +36,7 @@
#define vtkCloseSocketMacro(sock) (close(sock))
#endif
vtkCxxRevisionMacro(vtkSocketCommunicator, "1.46");
vtkCxxRevisionMacro(vtkSocketCommunicator, "1.47");
vtkStandardNewMacro(vtkSocketCommunicator);
//----------------------------------------------------------------------------
......@@ -270,9 +270,8 @@ int vtkSocketCommunicator::Receive(vtkIdType* data, int length,
#endif
//----------------------------------------------------------------------------
int vtkSocketCommunicator::WaitForConnection(int port)
int vtkSocketCommunicator::OpenSocket(int port, const char* )
{
if ( this->IsConnected )
{
vtkErrorMacro("Port " << 1 << " is occupied.");
......@@ -305,6 +304,26 @@ int vtkSocketCommunicator::WaitForConnection(int port)
vtkErrorMacro("Can not bind socket to port " << port);
return 0;
}
return sock;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::GetPort(int sock)
{
struct sockaddr_in sockinfo;
memset(&sockinfo, 0, sizeof(sockinfo));
int sizebuf = sizeof(sockinfo);
if(getsockname(sock, (sockaddr*)&sockinfo, &sizebuf) != 0)
{
vtkErrorMacro("No port found for socket " << sock);
return 0;
}
return ntohs(sockinfo.sin_port);
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::WaitForConnectionOnSocket(int sock)
{
listen(sock,1);
this->Socket = accept(sock, 0, 0);
if ( this->Socket == -1 )
......@@ -314,9 +333,9 @@ int vtkSocketCommunicator::WaitForConnection(int port)
}
vtkCloseSocketMacro(sock);
sock = -1;
this->IsConnected = 1;
if ( this->PerformHandshake )
{
// Handshake to determine if the client machine has the same endianness
......@@ -348,10 +367,21 @@ int vtkSocketCommunicator::WaitForConnection(int port)
this->SwapBytesInReceivedData = 1;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkSocketCommunicator::WaitForConnection(int port)
{
int sock = this->OpenSocket(port);
if(sock == 0)
{
return 0;
}
return this->WaitForConnectionOnSocket(sock);
}
void vtkSocketCommunicator::CloseConnection()
{
if ( this->IsConnected )
......
......@@ -54,6 +54,21 @@ public:
vtkTypeRevisionMacro(vtkSocketCommunicator,vtkCommunicator);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Create a socket on the given port, if port is 0, then
// create a random port. If network is specified, then use
// the given network. The socket number is returned.
virtual int OpenSocket(int port, const char* network = 0);
// Description:
// Return the port used by an open socket.
virtual int GetPort(int sock);
// Description:
// Wait for a connection on an already bound port created
// by a call to BindPort.
virtual int WaitForConnectionOnSocket(int socket);
// Description:
// Wait for connection on a given port.
virtual int WaitForConnection(int port);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment