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


Java DatagramSocket.setSoTimeout方法代码示例

本文整理汇总了Java中java.net.DatagramSocket.setSoTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramSocket.setSoTimeout方法的具体用法?Java DatagramSocket.setSoTimeout怎么用?Java DatagramSocket.setSoTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.DatagramSocket的用法示例。


在下文中一共展示了DatagramSocket.setSoTimeout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: CameraClient

import java.net.DatagramSocket; //导入方法依赖的package包/类
/**
 * Creates a new CameraServer. A datagram socket is created at the local address listening to a given port. The read thread
 * is immediately started and data is received. In order to initiate connection with the remote {@link CameraServer} a handshake
 * is sent to a given port and address which belong to the remote camera server. To avoid to much data, the data buffer is
 * limited to a maximum amount of bytes.
 * 
 * @param name the name of client, for logging
 * @param localPort local port for data listening
 * @param remoteAdd remote server address
 * @param remotePort remote server port
 * @param maxBytes maximum buffer size
 * 
 * @throws SocketException if failed to create the socket
 */
public CameraClient(String name, int localPort, InetAddress remoteAdd, int remotePort, int maxBytes) throws SocketException{
	port = localPort;
	sendPort = remotePort;
	sendAddress = remoteAdd;
	this.name = name;
	logName = name+"-CameraClient";
	
	socket = new DatagramSocket(new InetSocketAddress(localPort));
	socket.setSoTimeout(READ_TIMEOUT);
	
	recBytes = new byte[maxBytes];
	 
	runThread = new Thread(new Task(this));
	runThread.start();
}
 
开发者ID:Flash3388,项目名称:FlashLib,代码行数:30,代码来源:CameraClient.java

示例3: UDPReceive

import java.net.DatagramSocket; //导入方法依赖的package包/类
public UDPReceive() {
	
	try {
		int port = 10030;

		// Create a socket to listen on the port.
		dsocket = new DatagramSocket(port);
		dsocket.setSoTimeout(2);
		
		// Create a buffer to read datagrams into.
		buffer = new byte[2048];

		// Create a packet to receive data into the buffer
		packet = new DatagramPacket(buffer, buffer.length);
	} catch (Exception e) {
		e.printStackTrace();
	}
	
}
 
开发者ID:Team997Coders,项目名称:2017SteamBot2,代码行数:20,代码来源:UDPReceive.java

示例4: pingHost

import java.net.DatagramSocket; //导入方法依赖的package包/类
@Override
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> invalid){
    Thread thread = new Thread(() -> {
        try {

            Serialization ser = (Serialization) UCore.getPrivate(client, "serialization");
            DatagramSocket socket = new DatagramSocket();
            ByteBuffer dataBuffer = ByteBuffer.allocate(64);
            ser.write(dataBuffer, new DiscoverHost());

            dataBuffer.flip();
            byte[] data = new byte[dataBuffer.limit()];
            dataBuffer.get(data);
            socket.send(new DatagramPacket(data, data.length, InetAddress.getByName(address), port));

            socket.setSoTimeout(2000);

            addresses.clear();

            DatagramPacket packet = handler.onRequestNewDatagramPacket();

            socket.receive(packet);

            handler.onDiscoveredHost(packet);

            Host host = addresses.values().next();

            if (host != null) {
                Gdx.app.postRunnable(() -> valid.accept(host));
            } else {
                Gdx.app.postRunnable(() -> invalid.accept(new IOException("Outdated server.")));
            }
        } catch (IOException e) {
            Gdx.app.postRunnable(() -> invalid.accept(e));
        }
    });

    thread.setDaemon(true);
    thread.start();
}
 
开发者ID:Anuken,项目名称:Mindustry,代码行数:41,代码来源:KryoClient.java

示例5: 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

示例6: 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

示例7: SocketController

import java.net.DatagramSocket; //导入方法依赖的package包/类
public SocketController(String host, int port, int timeout) throws SocketException {
    mObservable = new ZiggsObservable();
    receivePacket = new DatagramPacket(receiveData, receiveData.length);
    mTimeOut = timeout;
    sendAddress = new InetSocketAddress(host, port);
    mSocket = new DatagramSocket();
    mSocket.setSoTimeout(0);// 永远等待接受消息!
    initUIHandlerAndThread();

}
 
开发者ID:Henryyyyyyy,项目名称:ZiggsUdp,代码行数:11,代码来源:SocketController.java

示例8: 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

示例9: initReceiver

import java.net.DatagramSocket; //导入方法依赖的package包/类
protected void initReceiver(){
	try {
		dsocket = new DatagramSocket(RECEIVE_PORT);
		dsocket.setSoTimeout(10000);
	} catch (SocketException ex) {
		log.warning(TunnelStateMachine.class.getName()+ex.getMessage());
	}
}
 
开发者ID:SensorsINI,项目名称:jaer,代码行数:9,代码来源:BlurringTunnelTracker.java

示例10: activateCameras

import java.net.DatagramSocket; //导入方法依赖的package包/类
private void activateCameras() {
    try{
        controlPort = CONTROL_PORT;
        localhost = "localhost";
        outputSocket = new DatagramSocket(CONNECT_PORT);
        outputSocket.setSoTimeout(100);
    } catch ( IOException ex ){
        log.warning(ex.toString());
    }
    log.info("sending activation commands to cameras");
    for(int i=cameraDomainOffset; i<=(cameraDomainOffset+MAX_NUM_CAMERAS); i++){
        String s = "t+\r\n";
        byte[] b = s.getBytes();
        try{
            InetAddress IPAddress =  InetAddress.getByName(cameraDomain+i);
            cameraSocketAddress = new InetSocketAddress(IPAddress,controlPort);
            log.info("send "+b+" to "+IPAddress.getHostAddress()+":"+controlPort);
            DatagramPacket d = new DatagramPacket(b,b.length,cameraSocketAddress);
            if (outputSocket != null){
	//repeat the sending 10 to be sure of data transmission
	for(int j=0; j<10; j++){
		outputSocket.send(d);
	}
            }
        } catch ( Exception e ){
            log.warning(e.toString());
        }
    }
}
 
开发者ID:SensorsINI,项目名称:jaer,代码行数:30,代码来源:MultiUDPNetworkDVS128Camera.java

示例11: 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

示例12: sendPkt

import java.net.DatagramSocket; //导入方法依赖的package包/类
/**
 * Sends a compiled packet to a destination host and port, and receives a
 * datagram from the source port specified.
 * 
 * @param sock
 *            Uses an external socket
 * @param pkt
 *            The compiled packet to be sent
 * @param sourceIpAddr
 *            Source IP address to be binded for receiving datagrams
 * @param sourcePort
 *            Source Port to be bineded for receiving datagrams
 * @param destIpAddr
 *            Destination IP address
 * @param destPort
 *            Destination Port
 * @param timeout
 *            Socket timeout. 0 will disable the timeout
 * @param bufSize
 *            Receiving datagram's buffer size
 * @return The received datagram
 * @throws IOException
 *             Thrown if socket timed out, cannot bind source IP and source
 *             port, no permission, etc.
 */
public static DatagramPacket sendPkt(DatagramSocket sock, Packet pkt, InetAddress sourceIpAddr, int sourcePort,
        InetAddress destIpAddr, int destPort, int timeout, int bufSize) throws IOException {
    // sock.bind(new InetSocketAddress(ipAddr, sourcePort));

    byte[] data = pkt.getData();
    log.debug("DESTIP: " + destIpAddr.getHostAddress());
    log.debug("DESTPORT: " + destPort);
    DatagramPacket sendpack = new DatagramPacket(data, data.length, destIpAddr, destPort);
    sock.send(sendpack);

    byte[] rece = new byte[bufSize];
    DatagramPacket recepack = new DatagramPacket(rece, 0, rece.length);

    long startTime = System.currentTimeMillis();
    long elapsed;
    while ((elapsed = System.currentTimeMillis() - startTime) < timeout) {
        try {
            sock.send(sendpack);
            sock.setSoTimeout(1000);
            sock.receive(recepack);
            break;
        } catch (SocketTimeoutException e) {
            if (elapsed > timeout) {
                break;
            }

            continue;
        }
    }

    return recepack;
}
 
开发者ID:mob41,项目名称:broadlink-java-api,代码行数:58,代码来源:BLDevice.java

示例13: 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

示例14: open

import java.net.DatagramSocket; //导入方法依赖的package包/类
private void open(int port) throws IOException {
    dsocket = new DatagramSocket(port);
    dsocket.setSoTimeout(5);
}
 
开发者ID:addertheblack,项目名称:myster,代码行数:5,代码来源:AsyncDatagramSocket.java

示例15: SampQuery

import java.net.DatagramSocket; //导入方法依赖的package包/类
/**
 * Configures the socket and the address that will be used for doing the queries.
 *
 * @param serverAddress
 *            hostname / ip
 * @param serverPort
 *            port
 * @param timeout
 *            the maximum time, that the socket tries connecting
 * @throws SocketException
 *             Thrown if the connection is closed unexpectedly / has never been opened properly
 * @throws UnknownHostException
 *             if the host is unknown
 */
public SampQuery(final String serverAddress, final int serverPort, final int timeout) throws SocketException, UnknownHostException {
	this.server = InetAddress.getByName(serverAddress);
	socket = new DatagramSocket();
	socket.setSoTimeout(timeout);
	this.serverPort = serverPort;
	checkConnection();
}
 
开发者ID:Bios-Marcel,项目名称:ServerBrowser,代码行数:22,代码来源:SampQuery.java


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