?
Solved

Connect socket synchronously but communicate Asynshronously

Posted on 2008-10-15
4
Medium Priority
?
898 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
[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
  • 2
4 Comments
 
LVL 31

Assisted Solution

by:Zoppo
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,

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 31

Assisted Solution

by:Zoppo
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.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 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