C
C#7mo 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
Jimmacle7mo ago
what is the problem you're having?
jcotton42
jcotton427mo ago
details please also if you could reformat your post using $code
MODiX
MODiX7mo 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
jcotton427mo ago
@LordoToasty
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton427mo ago
@LordoToasty
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty7mo 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
LordoToasty7mo ago
tho a new problem occured:
No description
jcotton42
jcotton427mo ago
Don't think you can use Dgram and Tcp together
LordoToasty
LordoToasty7mo ago
If you would be willing helping me I would be glad to, and this time I will try to be online too 😅
jcotton42
jcotton427mo ago
TCP is a stream protocol
LordoToasty
LordoToasty7mo ago
ouuu but how do I do the new socket then? should I just remove the Dgram?
jcotton42
jcotton427mo ago
Stream type instead of Dgram Assuming you meant to use TCP Though note the way you use TCP is different than UDP
LordoToasty
LordoToasty7mo 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
LordoToasty7mo ago
No description
LordoToasty
LordoToasty7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton427mo ago
For TCP you need to use connect And Send, not SendTo
LordoToasty
LordoToasty7mo ago
I get the "Send" but I never heared of "connect" is it instead of stream or..?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle7mo ago
TCP requires a connection to be established before you can send anything
LordoToasty
LordoToasty7mo ago
so bassicall I should stripe the SendTo and replace it with socket.connect?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty7mo ago
The world(broadcast) untill someone joyns the server(hoocks in to the port on my IP)
Jimmacle
Jimmacle7mo ago
wat you can't broadcast a packet to the entire internet
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty7mo ago
Did I get the PCP wrong?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle7mo 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
Jimmacle7mo ago
part of UPnP
LordoToasty
LordoToasty7mo 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
Jimmacle7mo 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
LordoToasty7mo 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
Jimmacle7mo 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
Jimmacle7mo 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
LordoToasty7mo 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
Jimmacle7mo ago
i just googled it
Jimmacle
Jimmacle7mo ago
first result
No description
LordoToasty
LordoToasty7mo ago
brugh maybe cus I used duck duck go instead of google. thanks tho. that really helps.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle7mo ago
the top circle is unrelated when
LordoToasty
LordoToasty7mo ago
Im LETSFUCKINGdood cant be...I got betrayed by the Internet, and stopped from doing a greate deed for humanity.
Jimmacle
Jimmacle7mo ago
you sure it isn't easier to just forward a port in your router? when
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
LordoToasty
LordoToasty7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle7mo 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
LordoToasty7mo 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
More Posts
Why does the following prints "Point" to the console instead of "(2, 3)"?```csharp Point thing = new Point(2, 3); System.Console.WriteLine(thing); // prints Point System.ConJsonException missing required properties when they are there```json "contact_inbox": { "id": 4, "contact_id": 4, "inbox_id": 1, "source_id": "f7b6c095-1✅ High quality list view iconsHow can i make my icons in ListView high quality while not resizing the row? (If i changed the size Blazor net 8 InteractiveServer mode problemI have .net 8 blazor app that enable the following ```xml @rendermode InteractiveServer @attribute [ActivatorUtilities.CreateInstance replacment for native AOTas in title, how can i replace ActivatorUtilities.CreateInstance function? right now it complain thaADO YAML - output variables troubleshootHi, I have pipeline, which compiles my app and publishes artifacts to azure, then I want to drop the✅ I want to work on the ongoing ASP.NET project as free or minimum salary.Hi, Nice to meet you everyone. As a experienced Web and Blockchian developer, now I am trying to lea✅ embed images in outputhi, I'm looking for a way to embed an image in the output in such a way that it's data is readable. Making an interpreter read custom filename extensionsI made an interpreter in Visual Studio (C#) for my own language. My question is, how do I make it soASP.NET Core 8 Web API, can't use additional fields with Identity registrationHello, It's my first time using Identity, so I came across some issues. When I input json data nece