本文整理匯總了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());
}
示例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();
}
示例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();
}
}
示例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();
}
示例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);
}
示例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;
}
示例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();
}
示例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();
}
}
示例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());
}
}
示例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());
}
}
}
示例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();
}
}
示例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;
}
示例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();
}
}
}
示例14: open
import java.net.DatagramSocket; //導入方法依賴的package包/類
private void open(int port) throws IOException {
dsocket = new DatagramSocket(port);
dsocket.setSoTimeout(5);
}
示例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();
}