I tried the suggestion from this question with very little success.
Please... any help will be greatly appreciated!
Here is my code:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000);
UdpClient udpServer = new UdpClient(localpt);
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpClient udpServer2 = new UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(localpt); // <<---------- Exception here
}
You have to set the socket option before binding.
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000);
UdpClient udpServer = new UdpClient();
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
UdpClient udpServer2 = new UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(localpt); // <<---------- No Exception here
Console.WriteLine("Finished.");
Console.ReadLine();
}
Or a more illustrative example:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(IPAddress.Loopback, 6000);
ThreadPool.QueueUserWorkItem(delegate
{
UdpClient udpServer = new UdpClient();
udpServer.ExclusiveAddressUse = false;
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("Listening on " + localpt + ".");
byte[] buffer = udpServer.Receive(ref inEndPoint);
Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
});
Thread.Sleep(1000);
UdpClient udpServer2 = new UdpClient();
udpServer2.ExclusiveAddressUse = false;
udpServer2.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(localpt);
udpServer2.Send(new byte[] { 0x41 }, 1, localpt);
Console.Read();
}
I looked up your error message and this explains what the error is and why it is happening.
Here is the exact error message and reason WSAEACCES 10013 (MSDN)
Permission denied.
An attempt was made to access a socket in a way forbidden by its
access permissions. An example is using a broadcast address for sendto
without broadcast permission being set using setsockopt(SO_BROADCAST).
Another possible reason for the WSAEACCES error is that when the bind
function is called (on Windows NT 4.0 with SP4 and later), another
application, service, or kernel mode driver is bound to the same
address with exclusive access. Such exclusive access is a new feature
of Windows NT 4.0 with SP4 and later, and is implemented by using the
SO_EXCLUSIVEADDRUSE option.
Even changing your code so that I can pass in an IP address I gets the same error message it appears that you can't bind to the same port and only one port can be used
here is the sample code I used your example and Altered it to capture my ip from my local machine..
IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
//IPEndPoint localpt = new IPEndPoint(ipLocalEndPoint);
UdpClient udpServer = new UdpClient(ipLocalEndPoint);
udpServer.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Connect(ipLocalEndPoint);
UdpClient udpServer2 = new UdpClient();
udpServer2.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer2.Client.Bind(ipLocalEndPoint); // <<---------- Exception here
this will produce the exception on the Bind () method.. sorry.
To solve WSAEACCESS 10013 (MSDN) exception in UDP application you can try
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
Related
I have a simple application that sends continuous UDP packets from a server to a client. Everything works well if the server app and client app are on the same computer but the minute I move the client to another computer in the LAN it stops working with a message
"the requested address is not valid in its context". I really don't understand why this is happening.
Here is the server code:
public void SendData()
{
udpClient = new UdpClient();
for (int i = 0; i < 8192; i++)
{
dataBytes[i] = Convert.ToByte(Convert.ToInt16(dataValues[i]));
}
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
udpClient.Connect(ipAddress, port);
udpClient.Send(dataBytes, dataBytes.Length);
}
catch (Exception ex)
{
}
}
On the client machine here is the code:
private void receive()
{
try
{
udpClient = new UdpClient();
udpClient.Client.ReceiveTimeout = 1000;
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
int port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
socket = new UdpClient(localEndPoint);
udpClient.Connect(ipAddress, port);
socket.BeginReceive(new AsyncCallback(ReceivedDataCallback), socket);
}
catch (Exception ex)
{
if (!socketConnected)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ReceivedDataCallback(IAsyncResult AR)
{
try
{
socketConnected = true;
clientSocket = AR.AsyncState as UdpClient;
var port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint source = new IPEndPoint(ipAddress, port);
nums = clientSocket.EndReceive(AR, ref source);
OutputRemoteSamples();
clientSocket.BeginReceive(new AsyncCallback(ReceivedDataCallback), clientSocket);
}
catch (Exception ex)
{
}
}
Thanks for any suggestions
Tom
So I'm trying to make a simple chat server that sends an object, Message between them. My server creation/connection code is
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, port);
Socket serverSocket = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
System.Console.WriteLine("Socket running on port " + port);
System.Console.WriteLine(ipHost);
serverSocket.Bind(localEndPoint);
System.Console.WriteLine("Accepting Connections");
serverSocket.Listen(10);
while (true)
{
Socket clientSocket = serverSocket.Accept();
Thread t = new Thread(() => Login.login(clientSocket));
System.Console.WriteLine("Client accepted.");
}
and my client creation/connection code is
System.Console.WriteLine("attempting to connect");
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Console.WriteLine("tesinft3");
clientSocket.Connect(IPAddress.Parse(server's external IP address), port);
System.Console.WriteLine("tesinft4");
System.Console.WriteLine("Connected to Server");
When I try to connect the client to the server I get attempting to connect and testinft3 and that's it. After about 30 seconds they appear again, but they still won't connect. How can I fix this?
I have a C# Client and a seperate Server application. When i execute both on the same machine, im able to connect to the server. However, when i try to connect to my server remotely it always ends in a timeout. I use the port 139 and I tried running the server application on mulitple machines. I have tried it on a server that is open to the public, my private PC with my router forwarding this port to the PC, as well as laptops in the same network.
I have always checked if the machine the server is running on is listening on the port using netstat -an and made exceptions in the firewall on all of them.
The code I use on the client to connect to the server:
static IPHostEntry ipHostInfo = Dns.GetHostEntry("xxx.xxx.xxx.xxx");
static IPAddress ipAddress = ipHostInfo.AddressList[0];
static IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
static Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
static private void ConnectToServer()
{
try
{
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine("Error: ", ex.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
The code I use to accept the connection on the server:
static IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
static IPAddress ipAddress = ipHostInfo.AddressList[0];
static IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 139);
static Socket listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Listening on: " + ipAddress);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
As I said this works flawlessly when both client and server application are executed on the same PC. If however they are on different machines the client gets a timout error within a couple of seconds.
Any help is appreciated, Thanks.
I am not able to receive back my own packets send on multicast. i created two Udpclient receiver is to receive packets on multicast group and sender is to send packets. my packets are send to the group but i cannot receive back the packets send by me....
public void Join()
{
IPAddress ip1 = IPAddress.Any;
localep = new IPEndPoint(ip1, port);
Receiver = new UdpClient();
Receiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Receiver.Client.Bind(localep);
Sender = new UdpClient();
Sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Sender.Client.Bind(localep);
IPAddress ip = IPAddress.Parse(IP);
remoteep = new IPEndPoint(ip, port);
Sender.JoinMulticastGroup(ip);
Sender.EnableBroadcast = true;
Sender.MulticastLoopback = true;
Receiver.JoinMulticastGroup(ip);
Receiver.EnableBroadcast = true;
Receiver.MulticastLoopback = true;
udpState.ipEndpt = RemoteIpEndPoint;
udpState.udpClient = Receiver;
Receiver.BeginReceive(new AsyncCallback(GetMsg), udpState);
}
void GetMsg(IAsyncResult ar)
{
UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
IPEndPoint ipEndpt = (IPEndPoint)((UdpState)(ar.AsyncState)).ipEndpt;
RecByte = Receiver.EndReceive(ar, ref ipEndpt);
}
//Sending packets logic
McastOTS.Sender.Send(sendBytes, sendBytes.Length, McastOTS.remoteep);
Seems to me all you need to do is call BeginReceive again after your EndReceive. Otherwise, you'll only get 1 message and won't see any others...
void GetMsg(IAsyncResult ar)
{
UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
IPEndPoint ipEndpt = (IPEndPoint)((UdpState)(ar.AsyncState)).ipEndpt;
RecByte = Receiver.EndReceive(ar, ref ipEndpt);
Receiver.BeginReceive(GetMsg, udpState);
}
I am making a client that connect to a server that is locally hosted that gets stock numbers from the server. The program does work if i use this code below but the way it works is by getting the dns name so in theory it only takes www.website.com and I cant figure out how I can get it to recognize a normal ip of 127.0.0.1 or localhost IP:
IPHostEntry ipHostInfo = Dns.GetHostEntry("www.website.com");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Attached is my attemp to get this to resolve the ip but I dont think I am approaching this right the full code can be seen here:StockReader Client Code
public class AsynchronousClient
{
private const int port = 21;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
//******************ISSUE BEGINS HERE*********************************
string sHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(sHostName);
IPAddress [] ipAddress = ipHostInfo.AddressList;
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
When you set the value of ipAddress, you can use the IPAddress.Parse method to pass in a string and retrieve an IPAddress object instead of using the domain name:
string ip = "127.0.0.1";
IPAddress address = IPAddress.Parse(ipAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Try using string sHostName = "localhost";
// Establish the remote endpoint for the socket.
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, portnumber);
// Create a TCP/IP socket.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(remoteEP);