本文整理匯總了Java中java.net.DatagramSocket.close方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramSocket.close方法的具體用法?Java DatagramSocket.close怎麽用?Java DatagramSocket.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.DatagramSocket
的用法示例。
在下文中一共展示了DatagramSocket.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPrimaryAddress
import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
* For multihomed machines (i.e. anything with Docker Machine), use the
* routing table default route to find what should be the primary network
* facing interface.
* @return an InetAddress for the primary address
*/
public static InetAddress getPrimaryAddress()
{
InetAddress ret;
try {
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByAddress(new byte[]{1,1,1,1}), 0);
ret = s.getLocalAddress();
s.close();
return ret;
} catch (SocketException | UnknownHostException se) {
log.info("get by route failed: " + se);
}
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
log.info("getLocalHost failed: " + ex);
}
return InetAddress.getLoopbackAddress();
}
示例2: jButtonDetenerActionPerformed
import java.net.DatagramSocket; //導入方法依賴的package包/類
private void jButtonDetenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetenerActionPerformed
try {
// Se intenta conectar, retorna IOException en caso que no pueda
DatagramSocket clienteSocket = new DatagramSocket();
byte[] bufferOut = new byte[1000];
String mensajeAMandar = "Mata server" + id;
bufferOut = mensajeAMandar.getBytes();
IPServer = InetAddress.getByName(servidor);
DatagramPacket sendPacket = new DatagramPacket(bufferOut, bufferOut.length, IPServer, numeroPuerto);
clienteSocket.send(sendPacket);
jLabel1.setForeground(Color.red);
clienteSocket.close();
url.setText("");
jlabelSQL.setText("");
this.setTitle("App [ID:?]");
} catch (IOException ex) {
System.out.println("(LOG) [ERROR] No se pudo contactar al servidor");
Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例3: sendUdpMsg
import java.net.DatagramSocket; //導入方法依賴的package包/類
public static void sendUdpMsg(String msg, String ip, int port) {
try {
InetAddress address = InetAddress.getByName(ip);
// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), address, port);
// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
} catch (Exception e) {
System.err.println(e);
}
}
示例4: duplicateTest
import java.net.DatagramSocket; //導入方法依賴的package包/類
@Test
public void duplicateTest() throws Exception {
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.setSoTimeout(3000);
CoapPacket cpRequest = new CoapPacket(Method.GET, MessageType.Confirmable, "/test/1", null);
cpRequest.setMessageId(4321);
DatagramPacket packet = new DatagramPacket(cpRequest.toByteArray(), cpRequest.toByteArray().length, InetAddress.getLocalHost(), SERVER_PORT);
DatagramPacket recPacket = new DatagramPacket(new byte[1024], 1024);
DatagramPacket recPacket2 = new DatagramPacket(new byte[1024], 1024);
datagramSocket.send(packet);
datagramSocket.receive(recPacket);
//send duplicate
Thread.sleep(20);
datagramSocket.send(packet);
datagramSocket.receive(recPacket2);
datagramSocket.close();
assertArrayEquals(recPacket.getData(), recPacket2.getData());
}
示例5: getIp
import java.net.DatagramSocket; //導入方法依賴的package包/類
public static String getIp() {
IOException e;
try {
DatagramSocket socket = new DatagramSocket();
try {
socket.connect(InetAddress.getByName("114.114.114.114"), 53);
InetAddress local = socket.getLocalAddress();
socket.close();
return local.getHostAddress();
} catch (IOException e2) {
e = e2;
DatagramSocket datagramSocket = socket;
e.printStackTrace();
return "";
}
} catch (IOException e3) {
e = e3;
e.printStackTrace();
return "";
}
}
示例6: detectMac_udp
import java.net.DatagramSocket; //導入方法依賴的package包/類
private void detectMac_udp(){
for(int i=0;i<10;i++){
try {
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp=new DatagramPacket(new byte[1000], 1000);
dp.setAddress(InetAddress.getByName(testIp_udp));
dp.setPort(5555);
ds.send(dp);
ds.close();
Thread.sleep(500);
if(local_mac!=null){
break;
}
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
示例7: scanForRoku
import java.net.DatagramSocket; //導入方法依賴的package包/類
private String scanForRoku(URL url) throws IOException {
/* create byte arrays to hold our send and response data */
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
/* our M-SEARCH data as a byte array */
String MSEARCH = "M-SEARCH * HTTP/1.1\nHost: " + url.getHost() + ":" + url.getPort() + "\nMan: \"ssdp:discover\"\nST: roku:ecp\n";
sendData = MSEARCH.getBytes();
/* create a packet from our data destined for 239.255.255.250:1900 */
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName(url.getHost()), url.getPort());
/* send packet to the socket we're creating */
DatagramSocket clientSocket = new DatagramSocket();
clientSocket.send(sendPacket);
/* recieve response and store in our receivePacket */
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
/* get the response as a string */
String response = new String(receivePacket.getData());
/* close the socket */
clientSocket.close();
/* parse the IP from the response */
/* the response should contain a line like:
Location: http://192.168.1.9:8060/
and we're only interested in the address -- not the port.
So we find the line, then split it at the http:// and the : to get the address.
*/
response = response.toLowerCase();
String address = response.split("location:")[1].split("\n")[0].split("http://")[1].split(":")[0].trim();
/* return the IP */
return address;
}
示例8: discover
import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
* Discover any UPNP device using SSDP (Simple Service Discovery Protocol).
* @param timeout in milliseconds
* @param serviceType if null it use "ssdp:all"
* @return List of devices discovered
* @throws IOException
* @see <a href="https://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol">SSDP Wikipedia Page</a>
*/
public static List<Device> discover(int timeout, String serviceType) throws IOException {
ArrayList<Device> devices = new ArrayList<Device>();
byte[] sendData;
byte[] receiveData = new byte[1024];
/* Create the search request */
StringBuilder msearch = new StringBuilder(
"M-SEARCH * HTTP/1.1\nHost: 239.255.255.250:1900\nMan: \"ssdp:discover\"\n");
if (serviceType == null) { msearch.append("ST: ssdp:all\n"); }
else { msearch.append("ST: ").append(serviceType).append("\n"); }
/* Send the request */
sendData = msearch.toString().getBytes();
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1900);
DatagramSocket clientSocket = new DatagramSocket();
clientSocket.setSoTimeout(timeout);
clientSocket.send(sendPacket);
/* Receive all responses */
while (true) {
try {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
devices.add(Device.parse(receivePacket));
}
catch (SocketTimeoutException e) { break; }
}
clientSocket.close();
return Collections.unmodifiableList(devices);
}
示例9: discoverOne
import java.net.DatagramSocket; //導入方法依賴的package包/類
public static Device discoverOne(int timeout, String serviceType) throws IOException {
Device device = null;
byte[] sendData;
byte[] receiveData = new byte[1024];
/* Create the search request */
StringBuilder msearch = new StringBuilder(
"M-SEARCH * HTTP/1.1\nHost: 239.255.255.250:1900\nMan: \"ssdp:discover\"\n");
if (serviceType == null) { msearch.append("ST: ssdp:all\n"); }
else { msearch.append("ST: ").append(serviceType).append("\n"); }
/* Send the request */
sendData = msearch.toString().getBytes();
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length, InetAddress.getByName("239.255.255.250"), 1900);
DatagramSocket clientSocket = new DatagramSocket();
clientSocket.setSoTimeout(timeout);
clientSocket.send(sendPacket);
/* Receive one response */
try {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
device = Device.parse(receivePacket);
}
catch (SocketTimeoutException e) { }
clientSocket.close();
return device;
}
示例10: closeSilently
import java.net.DatagramSocket; //導入方法依賴的package包/類
/** close things if not null, for use in finally blocks, swallows exceptions */
public static void closeSilently(final DatagramSocket socket) {
if (socket != null && !socket.isClosed()) {
// does not throw
socket.close();
}
}
示例11: closeSocket
import java.net.DatagramSocket; //導入方法依賴的package包/類
/**
* Closes the specified DatagramSocket
*/
protected boolean closeSocket(DatagramSocket socket, boolean removeFromList)
{
this.logDebug("closeSocket: " + socket);
if (null == socket)
{
return false;
}
else
{
boolean flag = false;
if (!socket.isClosed())
{
socket.close();
flag = true;
}
if (removeFromList)
{
this.socketList.remove(socket);
}
return flag;
}
}
示例12: sendString
import java.net.DatagramSocket; //導入方法依賴的package包/類
private void sendString(String s) throws SocketException, UnknownHostException, IOException{
outputSocket = new DatagramSocket(sendPort);
outputSocket.setSoTimeout(100);
byte[] b = s.getBytes();
InetAddress IPAddress = InetAddress.getByName(powerSupplyIP);
powerSupplySocketAddress = new InetSocketAddress(IPAddress,sendPort);
DatagramPacket d = new DatagramPacket(b,b.length,powerSupplySocketAddress);
if (outputSocket != null){
outputSocket.send(d);
outputSocket.close();
}
}
示例13: sendString
import java.net.DatagramSocket; //導入方法依賴的package包/類
private void sendString(String s) throws SocketException, UnknownHostException, IOException{
showSettingsArea.append("Send: "+s+"\r\n");
outputSocket = new DatagramSocket(sendPort);
outputSocket.setSoTimeout(100);
byte[] b = s.getBytes();
InetAddress IPAddress = InetAddress.getByName(powerSupplyIP);
powerSupplySocketAddress = new InetSocketAddress(IPAddress,sendPort);
DatagramPacket d = new DatagramPacket(b,b.length,powerSupplySocketAddress);
if (outputSocket != null){
outputSocket.send(d);
outputSocket.close();
}
}
示例14: datagramSocket
import java.net.DatagramSocket; //導入方法依賴的package包/類
@Test
public void datagramSocket() throws Exception {
DatagramSocket socket = new DatagramSocket(0);
socket.setTrafficClass(0x10);
socket.send(new DatagramPacket(DATA, 3, InetAddress.getByName(DESTINATION), 5683));
System.out.println("VERIFY IN WIRESHARK THAT PACKET'S TRAFFIC-CLASS IS SET!");
assertEquals(0x10, socket.getTrafficClass());
socket.close();
}
示例15: run
import java.net.DatagramSocket; //導入方法依賴的package包/類
public void run() throws IOException {
InetAddress IPAddress = InetAddress.getByName(host);
byte[] sendData = request.getBytes();
byte[] receiveData = new byte[65535];
// Use the provided socket if there is one, else just make a new one.
DatagramSocket socket = this.clientSocket == null ?
new DatagramSocket() : this.clientSocket;
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, port);
socket.send(sendPacket);
socket.setSoTimeout(500);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
socket.receive(receivePacket);
// Check reply status
XDR xdr = new XDR(Arrays.copyOfRange(receiveData, 0,
receivePacket.getLength()));
RpcReply reply = RpcReply.read(xdr);
if (reply.getState() != RpcReply.ReplyState.MSG_ACCEPTED) {
throw new IOException("Request failed: " + reply.getState());
}
} finally {
// If the client socket was passed in to this UDP client, it's on the
// caller of this UDP client to close that socket.
if (this.clientSocket == null) {
socket.close();
}
}
}