Solved

Connect socket synchronously but communicate Asynshronously

Posted on 2008-10-15
4
873 Views
Last Modified: 2013-12-04
I am developing in Visual Studio (Visual Basic 2008) for Windows 2003 servers.  I am designing an application which must create and maintain a socket connection to a third party IP address and port.  Then I need to be able to send a message to this server from a client app, whether it is a web page or a Windows form.  The server can be either Web Service, or Windows Service, but I am not clear how to expose an event to let the server know I want to send a socket transmission or that I have received a socket transmission.

I understand that I will want to use BeginReceive and BeginSend events on the socket, but not how to expose these to the client app which will hopefully reside on another machine.  I have looked at various samples, including <a href="http://msdn.microsoft.com/en-us/library/fx6588te.aspx">this</a>, but the code seems to be ALL synchronous or ALL Asynchronous.  I want to connect once and not disconnect between my client requests.

I'm not too worried about concurrent requests as this is a very low volume app and the individual message streams will have a token in them that uniquely identifies the stream of the messages.  I do, however need to respond to periodic "handshakes" from the vendor at the other end of the socket.

This is essentially an ISO8583 client if anyone is familiar with the architecture.  I have the messaging format fairly under control.  I appreciate any thoughtful advice on the subject.
0
Comment
Question by:tvurt
  • 2
  • 2
4 Comments
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 40 total points
ID: 22728579
Hi tvurt,

I don't really understand the problem - synchronous socket and asynchronous communication are two different things.

The difference between a synchronous and an asynchronous socket is that the 'server application is not suspended while it waits for a connection from a client' (quote from MSDN).

Asynchronous communication means that your applications use a thread for communication while the main thread can do other things (i.e. keep a GUI up to date).

In the sample you posted there's a synchronous socket created, the communication is asynchronous.

Best regards,

ZOPPO
0
 

Author Comment

by:tvurt
ID: 22732030
Hi Zoppo, thanks for the response.

My problem is that the example, specifically disconnects after Sending the data.

I see that in SendCallback with
        handler.Shutdown(SocketShutdown.Both)
        handler.Close()

I'm trying to figure out how to leave the socket connected and listening, but also receive a signal from an external program on my network that wants to "grab" the socket to Sends a message and receive a response.  I think I can just remove the handler.Shutdown and handler.Close, but I'm not clear from there how I would notify my app of that another user wanted to "grab" the socket.
0
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 40 total points
ID: 22732303
Hi tvurt,

sorry, I'm not really used to use .NET in VB, but may be you need these two, because in the sample the Socket is passed 'ByVal', so it might be the handler passed to the SendCallback is a copy of the Socket which needs to be released (at least in C++ I'd guess it is so).

I fear you just have to try if it works as you need it with these calls, and if not remove them and call similar when the socket need to be closed.

Multiple open sockets shouldn't be a problem - everytime a request is recieved to connect to a socket, the accept-method returns a new socket (with another port), so the initial socket can continue listening for new requests. All the communication is done via that new created socket.

ZOPPO
0
 

Accepted Solution

by:
tvurt earned 0 total points
ID: 22840986
Hi Zoppo,

Thanks for your suggestion.  I'm still experimenting with it.  I'll post the code when done.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

21 Experts available now in Live!

Get 1:1 Help Now