Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subclassing Socket 1

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I'm trying to use sockets to create a proxy connection to a server. The whole thing worked perfectly until I tried to add a custom class to deal with the proxy (authentication etc). I have two sockets, ListeningSocket and DataSocket.

The problem is that I can't see to get listeningsocket to accept datasocket as a ProxySocket, only as a standard socket.

ProxySocket extends Socket, but I can't get it to work.

Here's the code:
In the head of the class
private ProxySocket dataSocket = null;
private Socket listeningSocket = null; // used to accept active connections

The in the method to accept the connection.

listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listeningSocket.Bind(localEnd);
listeningSocket.Listen(1);
Socket dataSockTemp = listeningSocket.Accept(); // at this point, dataSockTemp is a connected Socket (shown via a breakboint)
dataSocket = dataSockTemp as ProxySocket;
listeningSocket.Close();

I'd really appreciate any help.
 
datatempsocket will always be null. you need to either extend, or decorate the socket with your socket proxy.

i would opt for the decorator. something like this
Code:
public class AuthenticatedSocket : Socket
{
   private Socket socket;
   private IUser user;

   public AuthenticatedSocket(Socket socket, IUser user)
   {
      this.socket = socket;
      this.user = user;
   }

   public override Socket Accept()
   {
      if(user.IsAuthenticated()) 
         return new AuthenticatedSocket(socket.Accept, user);
      return new NullSocket();
   }

   //override other members as well which simply reference this.socket

   private class NullSocket : Socket
   {
       public override void Close ()
       {
       }
   }
}
your code above would now look like this
Code:
listeningSocket = new AuthenticatingSocket(new Socket(AddressFamily.InterNetwork,   SocketType.Stream, ProtocolType.Tcp), GetUserCredentialsFromSomeWhere());
listeningSocket.Bind(localEnd);
listeningSocket.Listen(1);
Socket dataSockTemp = listeningSocket.Accept(); // at this point, dataSockTemp is a connected Socket (shown via a breakboint)
dataSocket = dataSockTemp as ProxySocket;
listeningSocket.Close();
I haven't directly worked with sockets before. i'm assuming sockets have virtual members. if they don't you could either force the issue using the "new" keyword on the member, or create an socket interface and abstract the actual socket away from your client code and work with interfaces.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks.

I re-thought the problem, and eventually ended up creating a handler class that does the dirty work of negotating with the proxy server, which is transparent to the underlying socket.
 
excellent! could you post the code so other (like me) could learn from your example?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Sure. I created a new class (SocketHandler), then passed the socket into it's constructor, then used methods in the handler to perform authentication.

public class SocketHandler{
private Socket socket;
public SocketHandler(Socket socket) {
this.socket = socket;
}

public void Authenticate() {
//do authentication here
}
}
public partial class MyForm {
...
Socket mySock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketHandler handler = new SocketHandler(mySock);
handler.Authenticate();
...
}

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top