C
C#10mo ago
LordoToasty

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
Jimmacle
Jimmacle10mo ago
what is the problem you're having?
jcotton42
jcotton4210mo ago
details please also if you could reformat your post using $code
MODiX
MODiX10mo ago
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/
jcotton42
jcotton4210mo ago
@LordoToasty
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton4210mo ago
@LordoToasty
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty10mo ago
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.
LordoToasty
LordoToasty10mo ago
tho a new problem occured:
No description
jcotton42
jcotton4210mo ago
Don't think you can use Dgram and Tcp together
LordoToasty
LordoToasty10mo ago
If you would be willing helping me I would be glad to, and this time I will try to be online too 😅
jcotton42
jcotton4210mo ago
TCP is a stream protocol
LordoToasty
LordoToasty10mo ago
ouuu but how do I do the new socket then? should I just remove the Dgram?
jcotton42
jcotton4210mo ago
Stream type instead of Dgram Assuming you meant to use TCP Though note the way you use TCP is different than UDP
LordoToasty
LordoToasty10mo ago
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...
Want results from more Discord servers?
Add your server