Reputation: 563
I have created simple UDP socket connection to send data between local network. The connection is of two types : peer to peer and group broadcast. Below is my class : Peer to peer server:
public class CandyCaneUdpServer
{
Socket sck;
EndPoint LocalEP;
EndPoint RemoteEP;
int port = 80;
IAsyncResult oldAsyncResult;
public event EventHandler<MessageObject> RaiseMessageReceivedEvent;
public CandyCaneUdpServer()
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sck.ExclusiveAddressUse = false;
LocalEP = new IPEndPoint(IPAddress.Parse(UdpUtility.GetLocalIP()), port);
RemoteEP = new IPEndPoint(IPAddress.Any, port);
}
public void Serve()
{
sck.Bind(LocalEP);
byte[] _buffer = new byte[1500];
oldAsyncResult = sck.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(MessageCallBack), _buffer);
}
private void MessageCallBack(IAsyncResult aResult)
{
int size = sck.EndReceiveFrom(aResult, ref RemoteEP);
if (size > 0)
{
byte[] receivedData;
receivedData = (byte[])aResult.AsyncState;
MessageObject obj;
IFormatter f = new BinaryFormatter();
using (MemoryStream s = new MemoryStream(receivedData))
{
obj = (MessageObject)f.Deserialize(s);
}
OnRaiseMessageReceivedEvent(obj);
}
var _buffer = new byte[1500];
oldAsyncResult = sck.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(MessageCallBack), _buffer);
}
protected virtual void OnRaiseMessageReceivedEvent(MessageObject e)
{
EventHandler<MessageObject> handler = RaiseMessageReceivedEvent;
if (handler != null)
{
handler(this, e);
}
}
}
Below is peer to peer client(sender):
public class CandyCaneUdpClient
{
Socket sck;
EndPoint RemoteEP;
public CandyCaneUdpClient(string RemoteIP, int RemotePort)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sck.ExclusiveAddressUse = false;
RemoteEP = new IPEndPoint(IPAddress.Parse(RemoteIP), RemotePort);
}
public void Send(int MessageId, string Message, __MessageObjectType t = __MessageObjectType.P2P)
{
byte[] msg;
msg = Encoding.UTF8.GetBytes(Message);
var obj = new MessageObject()
{
Id = MessageId,
Type = t,
SourceIP = UdpUtility.GetLocalIP(),
TargetIP = ((IPEndPoint)RemoteEP).Address.ToString(),
SentTime = DateTime.Now,
Message = Message
};
IFormatter f = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
f.Serialize(ms, obj);
var msgByte = ms.ToArray();
sck.SendTo(msgByte, msgByte.Length, SocketFlags.None, RemoteEP);
}
}
}
The code is working fine for all ether-net machines. When a wifi client is connected, the wireless pc can send data but not receive data. When I examine the IP, ether-net client has IP of 192.168.211.---. whereas wireless pc has an IP of 192.168.0.---. I wonder if DHCP address causes the issue. The group chat works fine.
public class CandyCaneUdpGroupChatListener
{
UdpClient listener;
IPAddress routerBindIP;
readonly int routerBindPort = UdpUtility.GroupChatDefinedPort();
EndPoint routerEP, senderEP;
public event EventHandler<MessageObject> RaiseMessageReceivedEvent;
public CandyCaneUdpGroupChatListener()
{
routerBindIP = IPAddress.Parse(UdpUtility.GroupChatRouterBindAddress());
routerEP = new IPEndPoint(routerBindIP, routerBindPort);
senderEP = new IPEndPoint(IPAddress.Parse(UdpUtility.GetLocalIP()), routerBindPort);
listener = new UdpClient();
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.ExclusiveAddressUse = false;
listener.Client.Bind(senderEP);
listener.JoinMulticastGroup(routerBindIP);
var _buffer = new byte[1500];
listener.Client.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref routerEP,
new AsyncCallback(ReceiveMessage), _buffer);
}
private void ReceiveMessage(IAsyncResult result)
{
if (listener == null) return;
if (listener.Client == null) return;
int size = listener.Client.EndReceiveFrom(result, ref routerEP);
if (size > 0)
{
byte[] receivedData;
receivedData = (byte[])result.AsyncState;
MessageObject obj;
IFormatter f = new BinaryFormatter();
using (MemoryStream s = new MemoryStream(receivedData))
{
obj = (MessageObject)f.Deserialize(s);
//if (obj.SourceIP == UdpUtility.GetLocalIP()) return;
}
MessageReceived(obj);
}
var _buffer = new byte[1500];
listener.Client.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref routerEP,
new AsyncCallback(ReceiveMessage), _buffer);
}
protected virtual void MessageReceived(MessageObject e)
{
EventHandler<MessageObject> handler = this.RaiseMessageReceivedEvent;
if (handler != null)
{
handler(this, e);
}
}
public void Disconnect()
{
listener.DropMulticastGroup(routerBindIP);
}
}
Upvotes: 1
Views: 837
Reputation: 6174
Check out how you have your wired and wireless network subnets laid out. It looks like you've got two different DHCP servers working here - one for the wired network and another one for the wireless network.
Make sure that your network routers are configured such that a host on one subnet can communicate with a host on the other subnet.
To determine if a host on one subnet can communicate with a host on the other subnet, you could use the network trace route utility or the ping utility.
On Windows, trace route is implemented as the tracert.exe
application, which you should find in the C:\Windows\System32
directory.
Suppose you're on the Ethernet-attached client, you might execute it with the following command line to attempt to trace the route to the wireless PC (suppose it has the address 192.168.0.104):
C:> \Windows\System32\tracert.exe 192.168.0.104
You should see output that looks similar to this:
Tracing route to 192.168.0.104
over a maximum of 30 hops:
1 1 ms 1 ms <1 ms 192.168.211.54
... (possibly other entries her)
2 1 ms 1 ms 1 ms 192.168.0.104
Trace complete.
If the trace fails to complete, it's an indicate that your network traffic cannot get to the machine you're trying to trace to.
Be aware that if tracert
fails it's not a 100% indication of connectivity failure. It may be an indication that ICMP traffic is being blocked by a firewall or the destination machine is not responding to ICMP requests.
Upvotes: 1