Implementing multiple UDP sockets

1,265 views
Skip to first unread message

Syd

unread,
Apr 7, 2011, 12:04:42 PM4/7/11
to ns-3-users
Hi Everyone,
I am attempting to have 2 clients (n0, n1) connect
to a server(n2) using UDP sockets. From the code below, I do the
following:

1. create 2 client sockets
2. create 1 server socket
3. send a packet from n0 to the server
4. send a packet from n1 to the server

Problem: n0 is successfully able to send a packet to the server and
receive a reply but n1 is unable to send a packet to the server (as a
simple test, if I schedule n0 to send a packet at 2 different times,
that works just fine). However, there are no build errors.

Questions:
1.) Do I need to specify a number of connections that the server
should "listen" to or am I overlooking some other component in UDP
socket creation?

2.) Is there any limitation on the number of sockets I create?

Cheers!
Sydney

/* n0---------n2----------n1 */

Ptr<Socket> srcSocket[2]; /* 2 client sockets */
Ptr<Socket> dstSocket; /* 1 server socket */

/* Create 2 client sockets */

for (int i=0; i< 2; i++)
{
srcSocket[i] = Socket::CreateSocket (nodes.Get(i),
TypeId::LookupByName ("ns3::UdpSocketFactory"));
srcSocket[i]->Bind ();
srcSocket[i]->SetRecvCallback (MakeCallback
(&NS3ElectricalModel::srcSocketRecv, this));
}

/* Create server socket */

dstSocket = Socket::CreateSocket (nodes.Get(2), TypeId::LookupByName
("ns3::UdpSocketFactory"));
uint16_t dstport = 12345;
dstaddr="10.1.1.2";
InetSocketAddress dst = InetSocketAddress (dstaddr, dstport);
dstSocket->Bind (dst);
dstSocket->SetRecvCallback (MakeCallback
(&NS3ElectricalModel::dstSocketRecv, this));

/* n0 sends packet to server */
Simulator::Schedule (Seconds (0.1),&NS3ElectricalModel::SendStuff,
this, srcSocket[0], dstaddr, dstport);

/* n1 sends packet to server */
Simulator::Schedule (Seconds (0.2),&NS3ElectricalModel::SendStuff,
this, srcSocket[1], dstaddr, dstport);



Syd

unread,
Apr 12, 2011, 12:49:54 AM4/12/11
to ns-3-users
I would totally appreciate it if someone could provide feedback on
this post:

Chris Dennett

unread,
Apr 13, 2011, 7:24:36 AM4/13/11
to ns-3-...@googlegroups.com
Hi Syd,

Well, your addresses might be incorrect. Also, you should bind to the ANY address (try 0.0.0.0:12345) with specified port unless you plan on restricting it to a particular interface (one machine might have multiple interfaces). I suppose the problem might be that you could be only using one interface, when you have two interfaces. Generally, the standard socket interface will auto-choose the interface when sending or receiving depending on the desired subnet.

I could be wrong :)

-- Chris

--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.


Reply all
Reply to author
Forward
0 new messages