Answer the question
In order to leave comments, you need to log in
Can't send a message from one PC to another connected to the Internet via UDP?
There are 2 applications server and client:
class Program
{
static void Main(string[] args)
{
UDPSocket s = new UDPSocket();
s.Server(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
Console.WriteLine("Сервер запущен, ожидайте...");
Console.ReadKey();
}
}
class UDPSocket
{
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int bufSize = 8 * 1024;
private State state = new State();
private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private AsyncCallback recv = null;
private List<Clients> clients = new List<Clients>();
public void AddUser(Clients client)
{
clients.Add(client);
}
public class State
{
public byte[] buffer = new byte[bufSize];
}
public void Server(string address, int port)
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
socket.Bind(new IPEndPoint(IPAddress.Parse(address), port));
Recive();
}
private void Recive()
{
socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndReceiveFrom(ar, ref epFrom);
socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
BroadcastMessage(epFrom, Encoding.ASCII.GetString(so.buffer, 0, bytes));
}, state);
}
public void BroadcastMessage(EndPoint end, string msg)
{
byte[] data = Encoding.ASCII.GetBytes(msg);
for(int i = 0; i < clients.Count; i++)
{
if (clients[i].epFrom != end)
socket.BeginSendTo(state.buffer, 0, bufSize, SocketFlags.None, clients[i].epFrom, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndSend(ar);
}, state);
}
}
}
class Clients
{
public EndPoint epFrom = null;
UDPSocket udpSocket = null;
public Clients(EndPoint end, UDPSocket udp)
{
epFrom = end;
udpSocket = udp;
udpSocket.AddUser(this);
}
}
class Program
{
static void Main(string[] args)
{
UDPSocket c = new UDPSocket();
c.Client(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()));
c.Send("TEST!");
Console.ReadKey();
}
}
class UDPSocket
{
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int bufSize = 8 * 1024;
private State state = new State();
private EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private AsyncCallback recv = null;
public class State
{
public byte[] buffer = new byte[bufSize];
}
public void Client(string address, int port)
{
socket.Connect(IPAddress.Parse(address), port);
Recive();
}
public void Send(string text)
{
byte[] data = Encoding.ASCII.GetBytes(text);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndSend(ar);
Console.WriteLine("SEND: {0}, {1}", bytes, text);
}, state);
}
private void Recive()
{
socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socket.EndReceiveFrom(ar, ref epFrom);
socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
}, state);
}
}
Answer the question
In order to leave comments, you need to log in
Answer found:
The ip in the server application must be set to 0.0.0.0 to listen on all interfaces. Actually, that's all.
Yes, a lot of everything. The firewall in Windows cuts any such external requests, the computer can be turned on through a router, and in order to forward from the router to the PC, you need to configure it, finally, the provider itself can have NAT, and the IP that you see on 2ip.ru belongs to half of the area .. Here, lift VPN, get both computers into it and inside this virtual network, you can get through to each other
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question