[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get CAsyncSocket sending datagrams to another app on the same computer

Posted on 2010-01-02
18
Medium Priority
?
546 Views
Last Modified: 2013-11-20
Hello, little embarrassed I can't get this to work, but we are playing around with windows sockets, ASyncSocket, and we think we want the UDP datagram mode.  We want to play around with a couple of application-level approaches, and want to start up a couple of applications on the same computer (for convenience) that talk via sockets.

Anyway, I can't seem to get the right order of socket functions to make this work, here's what I do:

Application 1:
instantiate an AsyncSocket (well, my class derived from it)
call Create(1000, SOCK_DGRAM)
call Connect("127.0.0.1", 1000)
call Send("abc", 3, 0) (this seems to work, in fact it triggers its own OnReceive)

Application 2:
can't even create a socket on the same port 1000

So how do I hear datagrams sent by application 1 with application 2?  Is it possible?

The MSDN documentation is not great on this topic, in fact in a few places it seems to be talking only about streaming connections, not datagrams.

Any help is very appreciated, thanks.

0
Comment
Question by:riceman0
  • 10
  • 8
18 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 26163657
re: "can't even create a socket on the same port 1000"
- what error message are you getting?
- I take it that this remark is for the client. Generally, the client lets the OS pick an ephemeral port. If you are running both client and server on the same machine, and they both create an identical 5-tuple, then that could be your problem.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26163664
OK, I think your Application 1 is the client. What is the order in which you bring up your application? Try starting the server first, and see if you get different error results. Do yo have access to two computers? If so, maybe you can get it to work that way without having to worry about identical 5-tuples.
0
 

Author Comment

by:riceman0
ID: 26163669
The error is 10048, address is already in use.

Are you saying you can't run both ends of a datagram on the same machine?

What's a 5-tuple, the 4 parts of the ip address + port?
0
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.

 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 26163768
re: 5-tuple, the 4 parts of the ip address + port?
- Yes

re: Are you saying you can't run both ends of a datagram on the same machine?
Not saying that. Just saying that your 5-tuples should be unique.
0
 

Author Comment

by:riceman0
ID: 26163793

Can you give me an example of distinct 5-tuples that would work?  Seems to me that both are at the same IP address, and I thought they had to connected to the same port to communicate.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164118
If it is just the duplicate 5-tuple issue that we talked about, then try having the client use an ephemeral port chosen by the OS. Or, you can pick a different port than used by the server.

fyi - here are some possibly useful links:

Winsock Programmer's FAQ
http://tangentsoft.net/wskfaq/

UDP Send and Receive Using CAsyncSocket:
http://simplesamples.info/MFC/UDPSendReceive.php

There are examples in these links. But, if you submit your entire project, both client and server, I will see if I can get it to run. I am more familiar with unix sockets, but I am willing to try to look at your program.
0
 

Author Comment

by:riceman0
ID: 26164158
Yeah actually already found those exact links, got as far as I did above looking them.  None specifically talk intra-machine;  something is clearly wrong with my code, and I'm taking stabs at it... but someone who has used these classes can probably quickly confirm for me that it is possible to talk within a machine, and whether those should be the same IP address, different ports, etc.  Maybe even tell me the right sequence of commands.  Appreciate the offer, my project would need a bit of cleaning up before sharing, might take you up on it.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164317
re: "confirm for me that it is possible to talk within a machine"
Yes, there is no problem having client and server on same machine.

You have:
Application 1: <== i.e., the client
call Create(1000, SOCK_DGRAM)
call Connect("127.0.0.1", 1000)

This suggests that Application 1 is creating a socket on port 1000, and then connecting to it. This is great if your Application 1 wants to send UDP messages to itself.

Instead.. ref: http://msdn.microsoft.com/en-us/library/t7a47kk4(VS.80).aspx - "For a client socket, you typically accept the default value for this parameter, which lets Windows Sockets select a port." So, use:
===
CAsyncSocket sock;
sock.Create( );    // Use the default parameters
===
0
 

Author Comment

by:riceman0
ID: 26164362
I don't think the no parameter syntax will work, because Create is the only place I can specify SOCK_DGRAM.  Port of 0 will allow the OS to select, but I guess your underlying point is that it should talk and listen on same address, different ports.  I will try that.

By the way that is the confusing section of MSDN I was referring to, because some of it clearly doesn't apply to UDP, since it regularly refers to "connections."
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164366
Also, suggest starting your server first; then your client since you probably want your client's connect to succeed.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164371
ref: http://msdn.microsoft.com/en-us/library/xz019029(VS.80).aspx

nSocketPort
A well-known port to be used with the socket, or 0 if you want Windows Sockets to select a port.

So, use 0 for the port for the client-side.

0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164379
msdn is confusing because they break up the info into many small sections. And try to find out what header file you need - you'd think that they would include that. But if you read every related post on many many passes, it starts to make sense. Or, just save yourself the trouble and get the answers elsewhere.
0
 

Author Comment

by:riceman0
ID: 26164418

Yeah I'm using different ports and that got me past the "address conflict" error.  But I can't get it to hear anything (I'm hoping that is not because they are on different ports, or I will be on the wrong path for awhile).  So continuing to stab on the listener side...

I've gotten TCP sockets to work several times before, it's just been a while.  The documentation doesn't seem to translate to UDP very well.  MSDN says use "listen" method but I'm pretty sure you don't do that for UDP since it's connectionless, and I get a "bad operation" error which supports that theory.

One of those samples seems to not "Connect", just "Create" on the listener side but that didn't work either.

Will report any progress.
0
 

Author Comment

by:riceman0
ID: 26164455

Found a sample that works:

http://support.microsoft.com/kb/214396

will summarize how it works when I figure it out
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164470
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26164472
Oh, I saw that, but didn't want to post it since it looked like you had to download a .exe file.
0
 

Author Comment

by:riceman0
ID: 26164478
... it looks like I shouldn't be using connect.  Just:

Application 1:
Connect(local port)
SendTo(remote port)

Application 2:
Connect(local port)
OnReceive will fire

Friggin MSDN.  Thanks phoffic.
0
 

Author Comment

by:riceman0
ID: 26164485
Oops: replace "Connect" with "Create" in my last post
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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

831 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