Hello everyone ,
I am trying to do a program with IPv6 socket, when i tried to bind the socket with TCP , i was getting no problem , but when trying to bind with UDP (DatagramSocket ) i am getting exception as java.net.SocketException: already bound ( what ever port im giving im getting same problem ).
I m unable to solve this ....can any one help regarding this ....Thanks in advance.
The code is
import java.net.DatagramSocket; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; class Post { static public void main(String[] s) { try { byte[] addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1}; InetAddress inetAddr = Inet6Address.getByAddress(addr); System.out.println(inetAddr.getHostAddress()); InetSocketAddress addr_8501 = new InetSocketAddress(inetAddr,8501); ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(addr_8501); // This works fine for ( ; ; ) { serverSocket.accept(); } } catch (Throwable t) { t.printStackTrace(); } } }
this code worked fine when used with TCP connections ..... but when used Datagram Sockets(UDP) getting problem .....
import java.net.DatagramSocket; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; class Post { static public void main(String[] s) { try { byte[] addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1}; InetAddress inetAddr = Inet6Address.getByAddress(addr); System.out.println(inetAddr.getHostAddress()); InetSocketAddress addr_8502 = new InetSocketAddress(inetAddr,8502); DatagramSocket serverSocket = new DatagramSocket(); serverSocket.bind(addr_8502); } catch (Throwable t) { t.printStackTrace(); } } }
getting exception as
java.net.SocketException: already bound
at java.net.DatagramSocket.bind(Unknown Source)
at Post.main(Post.java:23)