Reputation: 69
I am trying to send data back to the client when the server receives "debug". ATM the following provides this error:
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
Added my main class to help answer questions
static Socket newSocket;
static byte[] data;
static EndPoint tmpRemote;
static IPEndPoint sender, endpoint;
static int recv;
static void Main(string[] args)
{
data = new byte[1024];
endpoint = new IPEndPoint(IPAddress.Any, 3000);
newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newSocket.Bind(endpoint);
sender = new IPEndPoint(IPAddress.Any, 904);
tmpRemote = (EndPoint)sender;
newSocket.BeginReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote, new AsyncCallback(OperatorCallBack), data);
Console.Read();
}
private static void OperatorCallBack(IAsyncResult ar)
{
log("[" + DateTime.Now + "][New Connection] " + tmpRemote.ToString() + "");
try
{
int size = newSocket.EndReceiveFrom(ar, ref tmpRemote);
if (size > 0)
{
data = (byte[])ar.AsyncState;
string[] dataCommand = Encoding.ASCII.GetString(data, 0, size).Split(' ');
if (dataCommand[0] == "debug")
{
newSocket.Send(Encoding.ASCII.GetBytes("HA IT WORKED :)"));
log("Sent debug");
}
else
{
log("Invalid Command");
}
}
data = new byte[1024];
newSocket.BeginReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote, new AsyncCallback(OperatorCallBack), data);
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}
Upvotes: 4
Views: 52593
Reputation: 311
I had similar issues when trying to connect to sockets over a slow connection. I resolved it by this code before call receive:
for (int i = 0; i < 50; i++)
{
if (!client.Connected)
{
Thread.Sleep(50);
}
else
{
break;
}
}
if (!client.Connected)
{
SendMessage("Unable to connect to network!");
}
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
Upvotes: 0
Reputation: 51
As connection(socket.connect()) is not needed when you send data via ProtocolType.Udp that error may occur when you dont suggest the address wher UDP Message should forward
IN YOUR CASE here the address is not provided for udp Send()
SOLUTION
Try SendTo() instead
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
soc.EnableBroadcast = true;
IPEndPoint ipend = new IPEndPoint(IPAddress.Broadcast, 58717);
EndPoint endp = (EndPoint)ipend;
byte[] bytes = new byte[1024];
bytes = Encoding.ASCII.GetBytes(str);
soc.SendTo(bytes,ipend);
soc.Close();
Upvotes: 3
Reputation: 61
I had similar issues when trying to connect to sockets over a slow connection. I resolved it by making sure the newSocket.Connected property was true before any Send/Receive calls.
Upvotes: 3
Reputation: 311050
The error message is quite clear. You are calling send() on an unconnected socket and without providing a target address. Where are you sending to? UDP doesn't know.
Upvotes: 2