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•12mo ago
Message Not Public
Sign In & Join Server To View
@LordoToasty
Unknown User•12mo 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...
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•12mo ago
Message Not Public
Sign In & Join Server To View
For TCP you need to use connect
And Send, not SendTo
I get the "Send" but I never heared of "connect"
is it instead of stream or..?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
TCP requires a connection to be established before you can send anything
so bassicall I should stripe the SendTo and replace it with socket.connect?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
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•12mo ago
Message Not Public
Sign In & Join Server To View
The world(broadcast) untill someone joyns the server(hoocks in to the port on my IP)
wat
you can't broadcast a packet to the entire internet
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Did I get the PCP wrong?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
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 ...
part of UPnP
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.
?
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
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
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
relevant RFC, probably https://datatracker.ietf.org/doc/rfc6887/
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.
that part is the big problem, I didn'T find an explicit one really beeing direct about how that crap works.
that was...quick
i just googled it
first result
brugh
maybe cus I used duck duck go instead of google.
thanks tho.
that really helps.
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
the top circle is unrelated
Im cant be...I got betrayed by the Internet, and stopped from doing a greate deed for humanity.
you sure it isn't easier to just forward a port in your router?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
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•12mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
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
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.