I have a problem with the socet.ReciveFrom(), can someone Help pls?
private void PortForwardProcess(object sender, RoutedEventArgs e)
{
int targetPort = 4789;
// A port to type in.
int port = int.Parse(Console.ReadLine());
// Socket erstellen
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Zieladresse und -port festlegen
IPAddress address = IPAddress.Parse("0.0.0.0");
// Port anfordern
// PCP-Nachricht erstellen
byte[] message = Encoding.UTF8.GetBytes("PCP\nR\n0\n0\n" + port + "\n" + address.ToString() + "\n" + targetPort);
// Nachricht senden
socket.SendTo(message, 0, message.Length, SocketFlags.None, new IPEndPoint(address, targetPort));
IPEndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0);
EndPoint endpoint = new IPAddress(IPAddress.Any, 0) ;
// Antwort empfangen
byte[] response = new byte[1024];
socket.ReceiveFrom(response, 0, response.Length, SocketFlags.None, endpoint );
// Antwort prüfen
if (response[0] == 'A')
{
// Portforwarding erfolgreich eingerichtet
Console.WriteLine("Portforwarding erfolgreich eingerichtet!");
}
else
{
// Portforwarding fehlgeschlagen
Console.WriteLine("Portforwarding fehlgeschlagen!");
}
try
{
// Portforwarding-Regel entfernen, wenn das Programm beendet wird
// ...
}
finally
{
// Socket schließen
socket.Close();
}
}
55 Replies
what is the problem you're having?
details please
also if you could reformat your post using $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/@LordoToasty
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
@LordoToasty
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
ou thx for comming the problem lied in the: ocket.ReceiveFrom(response, 0, response.Length, SocketFlags.None, ref senderRemote);
tho I allready fixed it, but it took me a while, because I had the statement:" EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0); " twice when appliying a fix, tho thank you for taking notice of me.
tho a new problem occured:
Don't think you can use Dgram and Tcp together
If you would be willing helping me I would be glad to, and this time I will try to be online too 😅
TCP is a stream protocol
ouuu
but how do I do the new socket then?
should I just remove the Dgram?
Stream type instead of Dgram
Assuming you meant to use TCP
Though note the way you use TCP is different than UDP
yes yes, TCP
I know about this one, I had to make an presentation about that in school back in the day
allright, umm well...