本文整理汇总了Java中java.net.Socket.connect方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.connect方法的具体用法?Java Socket.connect怎么用?Java Socket.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Socket
的用法示例。
在下文中一共展示了Socket.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createEndpoint
import java.net.Socket; //导入方法依赖的package包/类
private Endpoint createEndpoint() throws IOException {
URL realUrl = getUrl();
try {
if ("https".equals(realUrl.getProtocol())) { // NOI18N
SSLContext context = ContextProvider.getInstance().getSSLContext(instance);
return Endpoint.forSocket(context.getSocketFactory().createSocket(realUrl.getHost(), realUrl.getPort()));
} else if ("http".equals(realUrl.getProtocol())) { // NOI18N
Socket s = new Socket(ProxySelector.getDefault().select(realUrl.toURI()).get(0));
int port = realUrl.getPort();
if (port < 0) {
port = realUrl.getDefaultPort();
}
s.connect(new InetSocketAddress(realUrl.getHost(), port));
return Endpoint.forSocket(s);
} else {
throw new IOException("Unknown protocol: " + realUrl.getProtocol());
}
} catch (URISyntaxException ex) {
throw new IOException(ex);
}
}
示例2: connectButtonActionPerformed
import java.net.Socket; //导入方法依赖的package包/类
/**
* Lancia la connessione
*/
private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
if (!InetAddressValidator.getInstance().isValid(this.ipField.getText()) || Integer.parseInt(portField.getText()) > 65535)
JOptionPane.showMessageDialog(this, "Indirizzo ip non valido", "Errore", JOptionPane.ERROR_MESSAGE);
else {
try {
socket = new Socket();
String ip = ipField.getText();
int port = Integer.parseInt(portField.getText());
socket.connect(new InetSocketAddress(ip, port), 10000);
connectionLabel.setText("Connesso!");
nickname = nicknameField.getText();
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//out = new PrintStream(socket.getOutputStream(), true);
Client client = new Client(socket, nickname);
Thread clientThread = new Thread(client);
clientThread.start();
dispose();
} catch (IOException ex) {
connectionLabel.setText("Errore di connessione");
}
}
}
示例3: send
import java.net.Socket; //导入方法依赖的package包/类
private synchronized void send(Message message) throws IOException {
if(message == null) return;
Socket socket = new Socket();
socket.setReuseAddress(true);
socket.setPerformancePreferences(2, 1, 0); // connection, latency, bandwidth
socket.setTcpNoDelay(true);
socket.setTrafficClass(0x10); // low delay
socket.setSoTimeout(timeoutMillis);
socket.connect(address);
@SuppressWarnings("resource")
OutputStream out = socket.getOutputStream();
// TODO additional check. Is it needed?
if(out == null) throw new IOException("Socket has no output stream.");
String text = message.toString();
out.write(text.getBytes());
if(CRUSH.debug) CRUSH.debug(this, "DRP> " + text);
out.flush();
socket.close();
}
示例4: createSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* Attempts to get a new socket connection to the given host within the
* given time limit.
* <p>
* To circumvent the limitations of older JREs that do not support connect
* timeout a controller thread is executed. The controller thread attempts
* to create a new socket within the given limit of time. If socket
* constructor does not return until the timeout expires, the controller
* terminates and throws an {@link ConnectTimeoutException}
* </p>
*
* @param host the host name/IP
* @param port the port on the host
* @param clientHost the local host name/IP to bind the socket to
* @param clientPort the port on the local machine
* @param params {@link HttpConnectionParams Http connection parameters}
* @return Socket a new socket
* @throws IOException if an I/O error occurs while creating the socket
* @throws UnknownHostException if the IP address of the host cannot be
* determined
*/
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException
{
if( params == null )
{
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if( timeout == 0 )
{
return socketfactory.createSocket(host, port, localAddress, localPort);
}
else
{
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}
示例5: unsyncConnect
import java.net.Socket; //导入方法依赖的package包/类
@Override
protected void unsyncConnect(ISocketConnectionAddress address) throws ConnectionException {
log.info("Using encoding: " + encoding);
socket = new Socket();
try {
socket.connect(new InetSocketAddress(address.getHost(), address.getPort()));
socketReader = new InputStreamReader(socket.getInputStream(), encoding);
socketWriter = new OutputStreamWriter(socket.getOutputStream(), encoding);
} catch (IOException e) {
throw new ConnectionException(e + " (" + address.getHost() + ":" + address.getPort() + ")", log);
}
}
示例6: getConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection(int serverIndex) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
}
示例7: getSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* construct Socket object
* @param ip_addr ip address or hostname
* @param port port number
* @return connected Socket object
*/
public static Socket getSocket(String ip_addr, int port) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
return sock;
}
示例8: createSocket
import java.net.Socket; //导入方法依赖的package包/类
/***
* Creates a Socket connected to the given host and port.
* <p>
* @param address The address of the host to connect to.
* @param port The port to connect to.
* @return A Socket connected to the given host and port.
* @exception java.io.IOException If an I/O error occurs while creating the Socket.
***/
@Override
public Socket createSocket(InetAddress address, int port)
throws IOException
{
if (connProxy != null)
{
Socket s = new Socket(connProxy);
s.connect(new InetSocketAddress(address, port));
return s;
}
return new Socket(address, port);
}
示例9: getSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* construct Socket object
*
* @param addr InetSocketAddress object, including ip address and port
* @return connected Socket object
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(addr, ClientGlobal.g_connect_timeout);
return sock;
}
示例10: connect
import java.net.Socket; //导入方法依赖的package包/类
void connect() {
socket = new Socket();
clientThread = new Thread(new Runnable() {
@Override
public void run() {
try {
socket.connect(new InetSocketAddress(HOST, PORT));
} catch (IOException e) {
exceptionInConnect = e;
}
}
});
clientThread.start();
}
示例11: createSocket
import java.net.Socket; //导入方法依赖的package包/类
@Override
public Socket createSocket(String host, int port, InetAddress localHostAddr, int localPort) throws IOException, UnknownHostException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(host, port));
return socket;
}
示例12: createSocket
import java.net.Socket; //导入方法依赖的package包/类
@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
Socket socket = createSocket();
socket.connect(new InetSocketAddress(host, port));
return socket;
}
示例13: agentmain
import java.net.Socket; //导入方法依赖的package包/类
public static void agentmain(String args, Instrumentation inst) throws Exception {
System.out.println("RedefineAgent running...");
System.out.println("RedefineAgent redefine supported: " + inst.isRedefineClassesSupported());
int port = Integer.parseInt(args);
System.out.println("RedefineAgent connecting back to Tool....");
Socket s = new Socket();
s.connect( new InetSocketAddress(port) );
System.out.println("RedefineAgent connected to Tool.");
testRedefine(inst);
s.close();
}
示例14: replaceBlock
import java.net.Socket; //导入方法依赖的package包/类
private boolean replaceBlock(
ExtendedBlock block,
DatanodeInfo source,
DatanodeInfo sourceProxy,
DatanodeInfo destination,
StorageType targetStorageType) throws IOException, SocketException {
Socket sock = new Socket();
try {
sock.connect(NetUtils.createSocketAddr(destination.getXferAddr()),
HdfsServerConstants.READ_TIMEOUT);
sock.setKeepAlive(true);
// sendRequest
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
new Sender(out).replaceBlock(block, targetStorageType,
BlockTokenSecretManager.DUMMY_TOKEN, source.getDatanodeUuid(),
sourceProxy);
out.flush();
// receiveResponse
DataInputStream reply = new DataInputStream(sock.getInputStream());
BlockOpResponseProto proto =
BlockOpResponseProto.parseDelimitedFrom(reply);
while (proto.getStatus() == Status.IN_PROGRESS) {
proto = BlockOpResponseProto.parseDelimitedFrom(reply);
}
return proto.getStatus() == Status.SUCCESS;
} finally {
sock.close();
}
}
示例15: createSocket
import java.net.Socket; //导入方法依赖的package包/类
@Override
public Socket createSocket(String host, int port,
InetAddress localHostAddr, int localPort) throws IOException,
UnknownHostException {
Socket socket = createSocket();
socket.bind(new InetSocketAddress(localHostAddr, localPort));
socket.connect(new InetSocketAddress(host, port));
return socket;
}