C
C#13mo 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
Jimmacle12mo ago
what is the problem you're having?
jcotton42
jcotton4212mo ago
details please also if you could reformat your post using $code
MODiX
MODiX12mo 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
jcotton4212mo ago
@LordoToasty
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton4212mo ago
@LordoToasty
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToastyOP12mo 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
LordoToastyOP12mo ago
tho a new problem occured:
No description
jcotton42
jcotton4212mo ago
Don't think you can use Dgram and Tcp together
LordoToasty
LordoToastyOP12mo ago
If you would be willing helping me I would be glad to, and this time I will try to be online too 😅
jcotton42
jcotton4212mo ago
TCP is a stream protocol
LordoToasty
LordoToastyOP12mo ago
ouuu but how do I do the new socket then? should I just remove the Dgram?
jcotton42
jcotton4212mo ago
Stream type instead of Dgram Assuming you meant to use TCP Though note the way you use TCP is different than UDP
LordoToasty
LordoToastyOP12mo 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...
LordoToasty
LordoToastyOP12mo ago
No description
LordoToasty
LordoToastyOP12mo ago
I really never was the best in IT, but still I thought I could figure this one out by loocking in to the documentation and as greateful as I am for the help you provided me with I would like to request it once more.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton4212mo ago
For TCP you need to use connect And Send, not SendTo
LordoToasty
LordoToastyOP12mo ago
I get the "Send" but I never heared of "connect" is it instead of stream or..?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle12mo ago
TCP requires a connection to be established before you can send anything
LordoToasty
LordoToastyOP12mo ago
so bassicall I should stripe the SendTo and replace it with socket.connect?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToastyOP12mo ago
It automatically sets up a minecraft server(the easy part) aaaannd now I wanted to automatize the "port-forwarding" basicalli I want to open a port via PCP for acces outside of the router, without having to directly acces the router. (I thought it might be a fun/cool project)
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToastyOP12mo ago
The world(broadcast) untill someone joyns the server(hoocks in to the port on my IP)
Jimmacle
Jimmacle12mo ago
wat you can't broadcast a packet to the entire internet
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToastyOP12mo ago
Did I get the PCP wrong?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle12mo ago
Port Control Protocol
Port Control Protocol (PCP) is a computer networking protocol that allows hosts on IPv4 or IPv6 networks to control how the incoming IPv4 or IPv6 packets are translated and forwarded by an upstream router that performs network address translation (NAT) or packet filtering. By allowing hosts to create explicit port forwarding rules, handling of ...
Jimmacle
Jimmacle12mo ago
part of UPnP
LordoToasty
LordoToastyOP12mo ago
ou I know, I didnt wnat to really send anything, what I ment to do is just forward a port on the router for people to be able to send TCP packages over a specefied port, when hoocked in. that was my first idea but.. I've red its supposed to be the same thing but just outdated, Did I get things really that wrong. ?
Jimmacle
Jimmacle12mo ago
what do you mean, it's the same idea PCP is part of UPnP you basically want to send a UDP message to your router telling it to start forwarding a port to your server
LordoToasty
LordoToastyOP12mo ago
I have just red the Wikipedia and then some c# documentations and stack overflow problems to get into it, no clue about details, ik kinda dumb of me to think it would be easy or anything like that. jup, but I want the pot to be configured for TCPtraffic
Jimmacle
Jimmacle12mo ago
i assume that is specified in the control message you need to send to the router it has no relation to the protocol required to communicate with the router in the first place as far as networking goes this seems pretty easy assuming you can find documentation for the control message structure
Jimmacle
Jimmacle12mo ago
IETF Datatracker
RFC 6887: Port Control Protocol (PCP)
The Port Control Protocol allows an IPv6 or IPv4 host to control how incoming IPv6 or IPv4 packets are translated and forwarded by a Network Address Translator (NAT) or simple firewall, and also allows a host to optimize its outgoing NAT keepalive messages.
LordoToasty
LordoToastyOP12mo ago
that part is the big problem, I didn'T find an explicit one really beeing direct about how that crap works. that was...quick
Jimmacle
Jimmacle12mo ago
i just googled it
Jimmacle
Jimmacle12mo ago
first result
No description
LordoToasty
LordoToastyOP12mo ago
brugh maybe cus I used duck duck go instead of google. thanks tho. that really helps.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle12mo ago
the top circle is unrelated when
LordoToasty
LordoToastyOP12mo ago
Im LETSFUCKINGdood cant be...I got betrayed by the Internet, and stopped from doing a greate deed for humanity.
Jimmacle
Jimmacle12mo ago
you sure it isn't easier to just forward a port in your router? when
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToastyOP12mo ago
but it ain't automatic then anymore. that is stuff I know, cus I had IT-coureses at school, which I had to take'n stuff, its about implemanting it in to C#. And you should bare with me as I have just barely 9-Days of C# Experience (6h a day of efficient self learning Daily).
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle12mo ago
UPnP isn't guaranteed to be available either, i'd expect it to be off by default for security but maybe i'm too optimistic
LordoToasty
LordoToastyOP12mo ago
Its not that bad with me, I mean like the simple stuff, e.g. downloading a file, creating and running a batchfile, editing a nother file, 'n stuff is really easy,...only the networking implementation sucks.
Want results from more Discord servers?
Add your server