Tuesday 15 June 2010

c# - sockets - UDP Send data via internet -



c# - sockets - UDP Send data via internet -

i've made simple chat application in c#.net sends , receives info between 2 computers.

so, used method send data:

int port = 11000; private void send(string data, string ip) { socket sending_socket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); ipendpoint sending_end_point = null; byte[] send_buffer = encoding.ascii.getbytes(data); sending_end_point = new ipendpoint(ipaddress.parse(ip), port); seek { sending_socket.sendto(send_buffer, sending_end_point); } grab { } }

and receive used this:

string receiveddata = ""; private void backgroundworker1_dowork(object sender, doworkeventargs e) { udpclient listener = new udpclient(port); ipendpoint groupep = new ipendpoint(ipaddress.any, port); byte[] receive_byte_array; seek { receive_byte_array = listener.receive(ref groupep); receiveddata = encoding.ascii.getstring(receive_byte_array, 0, receive_byte_array.length); } grab { } listener.close(); }

this works without problems between 2 computers on lan, know (if possible) how same thing on internet.

from i've searched on internet, seems have utilize port-forwarding in order that, did that, don't know should know.

so question is, how should alter code (if have to) send , receive info (udp) on internet, assuming have port-forwarded correctly , assuming know external ips of both routers?

thank in advance.

this should work fine, long (public) ip-address right , ports forwarded correctly on router (meaning, forwarded right private ip, on right protocol, in case udp).

you aware udp though, it's not reliable info transfer.

c# .net sockets

No comments:

Post a Comment