1 17 package org.alfresco.filesys.netbios; 18 19 import java.io.IOException ; 20 import java.net.DatagramPacket ; 21 import java.net.DatagramSocket ; 22 import java.net.InetAddress ; 23 import java.net.SocketException ; 24 import java.net.UnknownHostException ; 25 26 31 public class NetBIOSDatagramSocket 32 { 33 35 private static NetBIOSDatagramSocket m_nbSocket; 36 37 39 private static int m_defPort = RFCNetBIOSProtocol.DATAGRAM; 40 private static InetAddress m_defBindAddr; 41 42 44 private DatagramSocket m_socket; 45 46 48 private InetAddress m_broadcastAddr; 49 50 56 private NetBIOSDatagramSocket() throws SocketException , UnknownHostException 57 { 58 59 61 if (m_defBindAddr == null) 62 m_socket = new DatagramSocket (m_defPort); 63 else 64 m_socket = new DatagramSocket (m_defPort, m_defBindAddr); 65 66 68 if (m_defBindAddr == null) 69 m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null)); 70 else 71 m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(m_defBindAddr 72 .getHostAddress())); 73 } 74 75 82 public final static synchronized NetBIOSDatagramSocket getInstance() throws SocketException , UnknownHostException 83 { 84 85 87 if (m_nbSocket == null) 88 m_nbSocket = new NetBIOSDatagramSocket(); 89 90 92 return m_nbSocket; 93 } 94 95 100 public final static void setDefaultPort(int port) 101 { 102 m_defPort = port; 103 } 104 105 110 public final static void setBindAddress(InetAddress bindAddr) 111 { 112 m_defBindAddr = bindAddr; 113 } 114 115 122 public final int receiveDatagram(NetBIOSDatagram dgram) throws IOException 123 { 124 125 127 DatagramPacket pkt = new DatagramPacket (dgram.getBuffer(), dgram.getBuffer().length); 128 129 131 m_socket.receive(pkt); 132 return pkt.getLength(); 133 } 134 135 143 public final void sendDatagram(NetBIOSDatagram dgram, InetAddress destAddr, int destPort) throws IOException 144 { 145 146 148 DatagramPacket pkt = new DatagramPacket (dgram.getBuffer(), dgram.getLength(), destAddr, destPort); 149 150 152 m_socket.send(pkt); 153 } 154 155 161 public final void sendBroadcastDatagram(NetBIOSDatagram dgram) throws IOException 162 { 163 164 166 DatagramPacket pkt = new DatagramPacket (dgram.getBuffer(), dgram.getLength(), m_broadcastAddr, m_defPort); 167 168 170 m_socket.send(pkt); 171 } 172 } 173 | Popular Tags |