Connect socket synchronously but communicate Asynshronously

Posted on 2008-10-15
Medium Priority
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.
Question by:tvurt
  • 2
  • 2
LVL 31

Assisted Solution

Zoppo earned 160 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,


Author Comment

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

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

Assisted Solution

Zoppo earned 160 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.


Accepted Solution

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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

607 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