本文整理了Java中java.net.Socket.<init>()
方法的一些代码示例,展示了Socket.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Socket.<init>()
方法的具体详情如下:
包路径:java.net.Socket
类名称:Socket
方法名:<init>
[英]Creates a new unconnected socket. When a SocketImplFactory is defined it creates the internal socket implementation, otherwise the default socket implementation will be used for this socket.
[中]创建新的未连接套接字。定义SocketImplFactory时,它将创建内部套接字实现,否则将对此套接字使用默认套接字实现。
代码示例来源:origin: iluwatar/java-design-patterns
@Override
public void run() {
try (Socket socket = new Socket(InetAddress.getLocalHost(), serverPort)) {
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream);
sendLogRequests(writer, socket.getInputStream());
} catch (IOException e) {
LOGGER.error("error sending requests", e);
throw new RuntimeException(e);
}
}
代码示例来源:origin: stackoverflow.com
public static boolean pingHost(String host, int port, int timeout) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), timeout);
return true;
} catch (IOException e) {
return false; // Either timeout or unreachable or failed DNS lookup.
}
}
代码示例来源:origin: apache/incubator-druid
@VisibleForTesting
protected void checkConnection(String host, int port) throws IOException
{
new Socket(host, port).close();
}
代码示例来源:origin: oldmanpushcart/greys-anatomy
/**
* 激活网络
*/
private Socket connect(InetSocketAddress address) throws IOException {
final Socket socket = new Socket();
socket.setSoTimeout(0);
socket.connect(address, _1MIN);
socket.setKeepAlive(true);
socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return socket;
}
代码示例来源:origin: apache/zookeeper
public static void kill(String host, int port) {
Socket s = null;
try {
byte[] reqBytes = new byte[4];
ByteBuffer req = ByteBuffer.wrap(reqBytes);
req.putInt(ByteBuffer.wrap("kill".getBytes()).getInt());
s = new Socket();
s.setSoLinger(false, 10);
s.setSoTimeout(20000);
s.connect(new InetSocketAddress(host, port));
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
os.write(reqBytes);
byte[] resBytes = new byte[4];
int rc = is.read(resBytes);
String retv = new String(resBytes);
System.out.println("rc=" + rc + " retv=" + retv);
} catch (IOException e) {
LOG.warn("Unexpected exception", e);
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
LOG.warn("Unexpected exception", e);
}
}
}
}
代码示例来源:origin: mpusher/mpush
public static boolean checkHealth(String ip, int port) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000);
socket.close();
return true;
} catch (IOException e) {
return false;
}
}
代码示例来源:origin: thinkaurelius/titan
public static void main(String args[]) {
if (2 != args.length) {
System.err.println(MSG_USAGE);
System.exit(E_USAGE);
}
try {
Socket s = new Socket(
InetAddress.getByName(args[0]),
Integer.valueOf(args[1]).intValue());
s.close();
System.exit(0);
} catch (Throwable t) {
System.err.println(t.toString());
System.exit(E_FAILED);
}
}
}
代码示例来源:origin: stackoverflow.com
try {
Socket x = new Socket("1.1.1.1", 6789);
x.getInputStream().read()
} catch (IOException e) {
System.err.println("Connection could not be established, please try again later!")
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Tell the server to exit
*/
public void sendQuit()
throws IOException
{
Socket socket = new Socket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
out.write("quit\n");
out.flush();
socket.close();
}
代码示例来源:origin: apache/flink
private void createConnection() throws IOException {
client = new Socket(hostName, port);
client.setKeepAlive(true);
client.setTcpNoDelay(true);
outputStream = client.getOutputStream();
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Returs a Tree from the server connected to at host:port.
*/
public Tree getTree(String query)
throws IOException
{
Socket socket = new Socket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
out.write("tree " + query + "\n");
out.flush();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Object o;
try {
o = ois.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (!(o instanceof Tree)) {
throw new IllegalArgumentException("Expected a tree");
}
Tree tree = (Tree) o;
socket.close();
return tree;
}
代码示例来源:origin: apache/zookeeper
public static void dump(String host, int port) {
Socket s = null;
try {
byte[] reqBytes = new byte[4];
ByteBuffer req = ByteBuffer.wrap(reqBytes);
req.putInt(ByteBuffer.wrap("dump".getBytes()).getInt());
s = new Socket();
s.setSoLinger(false, 10);
s.setSoTimeout(20000);
s.connect(new InetSocketAddress(host, port));
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
os.write(reqBytes);
byte[] resBytes = new byte[1024];
int rc = is.read(resBytes);
String retv = new String(resBytes);
System.out.println("rc=" + rc + " retv=" + retv);
} catch (IOException e) {
LOG.warn("Unexpected exception", e);
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
LOG.warn("Unexpected exception", e);
}
}
}
}
代码示例来源:origin: stackoverflow.com
public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
return es.submit(new Callable<Boolean>() {
@Override public Boolean call() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
return true;
} catch (Exception ex) {
return false;
}
}
});
}
代码示例来源:origin: stackoverflow.com
// output on my machine: "192.168.1.102"
Socket s = new Socket("192.168.1.1", 80);
System.out.println(s.getLocalAddress().getHostAddress());
s.close();
// output on my machine: "127.0.1.1"
System.out.println(InetAddress.getLocalHost().getHostAddress());
代码示例来源:origin: prestodb/presto
private static boolean isPortOpen(String host, Integer port)
{
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), 1000);
return true;
}
catch (ConnectException | SocketTimeoutException e) {
return false;
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Tokenize the text according to the parser's tokenizer,
* return it as whitespace tokenized text.
*/
public String getTokenizedText(String query)
throws IOException
{
Socket socket = new Socket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
out.write("tokenize " + query + "\n");
out.flush();
String result = readResult(socket);
socket.close();
return result;
}
代码示例来源:origin: jMonkeyEngine/jmonkeyengine
public SocketConnector( InetAddress address, int port ) throws IOException
{
this.sock = new Socket(address, port);
remoteAddress = sock.getRemoteSocketAddress(); // for info purposes
// Disable Nagle's buffering so data goes out when we
// put it there.
sock.setTcpNoDelay(true);
in = sock.getInputStream();
out = sock.getOutputStream();
connected.set(true);
}
代码示例来源:origin: apache/storm
@Override
public void prepare(Map<String, Object> topoConf, Object registrationArgument, TopologyContext context, IErrorReporter errorReporter) {
String[] parts = ((String) registrationArgument).split(":", 2);
host = parts[0];
port = Integer.valueOf(parts[1]);
try {
socket = new Socket(host, port);
out = socket.getOutputStream();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: pentaho/pentaho-kettle
private static int getUnusedPort() throws IOException {
Socket s = new Socket();
s.bind( (SocketAddress) null );
int var1;
try {
var1 = s.getLocalPort();
} finally {
s.close();
}
return var1;
}
代码示例来源:origin: apache/storm
@Override
public void open(Map<String, Object> conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector = collector;
this.queue = new LinkedBlockingDeque<>();
this.emitted = new HashMap<>();
this.objectMapper = new ObjectMapper();
try {
socket = new Socket(host, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
throw new RuntimeException("Error opening socket: host " + host + " port " + port);
}
readerThread = new Thread(new SocketSpout.SocketReaderRunnable());
readerThread.start();
}
内容来源于网络,如有侵权,请联系作者删除!