c - How to open the two udp client socket with same local port -
how open 2 client socket same local port java. in java can mention source port while creating datagrampacket. trying udp hole punching. correct me if done wrong.
i have added code here.
void udpclientconnect ( string ip , string port ) { wsadata wsa; if ( wsastartup(makeword(2,2),&wsa) != 0 ) { printf ( "startup failed %d\n" , wsagetlasterror() ) ; return ; } int reuse = 1 ; socket s = socket ( af_inet , sock_dgram , ipproto_udp ) ; setsockopt(s, sol_socket, so_reuseaddr,(char*) &reuse, sizeof(int)); struct sockaddr_in si_other; int slen=sizeof(si_other); char buf[buflen]; char message[buflen]; int p = atoi ( port.c_str() ) ; memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = af_inet; si_other.sin_port = htons(p); si_other.sin_addr.s_un.s_addr = inet_addr( ip.c_str() ); strcpy_s ( message , "hello" ) ; //send message if ( sendto ( s , message , strlen(message) , 0 , (struct sockaddr *) &si_other , slen ) == socket_error) { printf ( "sendto() failed error code : %d" , wsagetlasterror() ) ; return ; } //receive reply , print memset ( buf , '\0' , buflen ) ; //try receive data, blocking call if ( recvfrom ( s , buf , buflen , 0 , (struct sockaddr *) &si_other , &slen ) == socket_error ) { printf("recvfrom() failed error code : %d" , wsagetlasterror()); return ; } struct sockaddr_in localaddress; int addrlen = sizeof(localaddress) ; getsockname ( s , (struct sockaddr*)&localaddress , &addrlen ) ; ushort localport = localaddress.sin_port ; in_addr localaddr = localaddress.sin_addr ; printf("local address: %s\n", inet_ntoa( localaddress.sin_addr)); printf("local port: %d\n", (int) ntohs(localaddress.sin_port)); printf ( "\n******************************************\n\n" ) ; int pos = 1 ; cstringa cbuff = cstringa ( buf ) ; string remortport = cbuff.tokenize ( "-" , pos ) ; string remortip = cbuff.tokenize ( "-" , pos ) ; reuse = 1 ; socket sock_reuse = socket ( af_inet , sock_dgram , ipproto_udp ) ; setsockopt( sock_reuse , sol_socket, so_reuseaddr,(char*) &reuse, sizeof(int)); // optional, recommended struct sockaddr_in sin; memset(&sin,0,sizeof(sin)); sin.sin_family=af_inet; sin.sin_port=htons(localport); sin.sin_addr.s_addr=inaddr_any; if ( socket_error == connect(sock_reuse,(struct sockaddr *)&sin,sizeof(struct sockaddr_in))) { printf ( "bind failed %d" , wsagetlasterror() ) ; } closesocket ( sock_reuse ) ; closesocket ( s ) ; wsacleanup ( ) ;
}
in same method trying open new socket same port.
check java code trying in c/c++. not possible ??
import java.io.* ; import java.net.* ; public class udpholepunchingclient { public static void main(string[] args) throws exception { // prepare socket datagramsocket clientsocket = new datagramsocket(); // prepare data byte[] senddata = "hello".getbytes(); // send data server fix ip (x.x.x.x) // client1 uses port 7070, client2 uses port 7071 datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, inetaddress.getbyname("172.24.8.110"), 7070); clientsocket.send(sendpacket); // receive data ==> format:"<ip of other client>-<port of other client>" datagrampacket receivepacket = new datagrampacket(new byte[1024], 1024); clientsocket.receive(receivepacket); // convert response ip , port string response = new string(receivepacket.getdata()); string[] splitresponse = response.split("-"); inetaddress ip = inetaddress.getbyname(splitresponse[0].substring(1)); int port = integer.parseint(splitresponse[1]); // output converted data check system.out.println("ip: " + ip + " port: " + port); // close socket , open new socket same localport int localport = clientsocket.getlocalport(); clientsocket.close(); clientsocket = new datagramsocket(localport); // set timeout receiving data clientsocket.setsotimeout(1000); // send 5000 messages testing (int = 0; < 5000; i++) { // send message other client senddata = ("datapacket(" + + ")").getbytes(); sendpacket = new datagrampacket(senddata, senddata.length, ip, port); clientsocket.send(sendpacket); // receive message other client try { receivepacket.setdata(new byte[1024]); clientsocket.receive(receivepacket); system.out.println("rec: " + new string(receivepacket.getdata())); } catch (exception e) { system.out.println("server timed out"); } } // close connection clientsocket.close(); } }
contrary commentary, have set socket option so_reuseport
prior bind()
.
Comments
Post a Comment