当前位置: 首页>>代码示例>>Java>>正文


Java DatagramSocket.close方法代码示例

本文整理汇总了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();
}
 
开发者ID:drytoastman,项目名称:scorekeeperfrontend,代码行数:28,代码来源:Network.java

示例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);
    }
}
 
开发者ID:AmauryOrtega,项目名称:Sem-Update,代码行数:24,代码来源:VentanaPrincipal.java

示例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);
  }
}
 
开发者ID:angeldove,项目名称:LanChatLite,代码行数:17,代码来源:Utils.java

示例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());

}
 
开发者ID:ARMmbed,项目名称:java-coap,代码行数:22,代码来源:ClientServerTest.java

示例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 "";
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:22,代码来源:Network.java

示例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();
			}
		}
	}

}
 
开发者ID:jonasxiao,项目名称:FinalSpeed,代码行数:25,代码来源:CapEnv.java

示例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;
}
 
开发者ID:wseemann,项目名称:Jaku,代码行数:39,代码来源:DiscoveryRequest.java

示例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);
}
 
开发者ID:mguntli,项目名称:sonos-remote-things,代码行数:41,代码来源:SSDPClient.java

示例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;
}
 
开发者ID:mguntli,项目名称:sonos-remote-things,代码行数:31,代码来源:SSDPClient.java

示例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();
    }
}
 
开发者ID:archos-sa,项目名称:aos-FileCoreLibrary,代码行数:8,代码来源:IOUtils.java

示例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;
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:30,代码来源:RConThreadBase.java

示例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();
	}
}
 
开发者ID:SensorsINI,项目名称:jaer,代码行数:13,代码来源:Resetter.java

示例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();
	}
}
 
开发者ID:SensorsINI,项目名称:jaer,代码行数:14,代码来源:PowerSettingsDialog.java

示例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();
}
 
开发者ID:ARMmbed,项目名称:java-coap,代码行数:12,代码来源:TrafficClassVerifier.java

示例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();
    }
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:33,代码来源:SimpleUdpClient.java


注:本文中的java.net.DatagramSocket.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。