List of usage examples for java.net Socket Socket
public Socket(InetAddress address, int port) throws IOException
From source file:cit360.sandbox.BackEndMenu.java
public static final void smtpExample() { // declaration section: // smtpClient: our client socket // os: output stream // is: input stream Socket smtpSocket = null;/*www. j a v a 2 s .c om*/ DataOutputStream os = null; DataInputStream is = null; // Initialization section: // Try to open a socket on port 25 // Try to open input and output streams try { smtpSocket = new Socket("localhost", 25); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Did not recognize server: localhost"); } catch (IOException e) { System.err.println("Was not able to open I/O connection to: localhost"); } // If everything has been initialized then we want to write some data // to the socket we have opened a connection to on port 25 if (smtpSocket != null && os != null && is != null) { try { os.writeBytes("HELO\n"); os.writeBytes("MAIL From: david.banks0889@gmail.com\n"); os.writeBytes("RCPT To: david.banks0889@gmail.com\n"); os.writeBytes("DATA\n"); os.writeBytes("From: david.banks0889@gmail.com\n"); os.writeBytes("Subject: TEST\n"); os.writeBytes("Hi there\n"); // message body os.writeBytes("\n.\n"); os.writeBytes("QUIT"); // keep on reading from/to the socket till we receive the "Ok" from SMTP, // once we received that then we want to break. String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); if (responseLine.contains("Ok")) { break; } } // clean up: // close the output stream // close the input stream // close the socket os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } }
From source file:ext.services.network.Proxy.java
/** * Instantiates a new proxy./*from w w w . j av a 2s .co m*/ * * @param type * @param url * @param port * @param user * @param password * * @throws UnknownHostException the unknown host exception * @throws IOException Signals that an I/O exception has occurred. */ public Proxy(Type type, String url, int port, String user, String password) throws UnknownHostException, IOException { super(type, new Socket(url, port).getRemoteSocketAddress()); this.url = url; this.port = port; this.user = user; this.password = password; }
From source file:net.sf.profiler4j.console.client.Client.java
public synchronized void connect(String host, int port) throws ClientException { try {// ww w . ja v a 2 s. c o m if (isConnected()) { throw new ClientException("Client already connected"); } s = new Socket(host, port); out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream())); in = new ObjectInputStream(new BufferedInputStream(s.getInputStream())); String serverVersion = in.readUTF(); if (!serverVersion.equals(AgentConstants.VERSION)) { s.close(); s = null; out = null; in = null; throw new ClientException("Version of remote agent is incompatible: console is '" + AgentConstants.VERSION + "' but agent is '" + serverVersion + "'"); } log.info("Client connected to " + host + ":" + port); } catch (Exception e) { handleException(e); } // fireClientEvent(new ClientEvent(this, ClientEvent.CONNECTED, null)); }
From source file:android.net.http.HttpConnection.java
/** * Opens the connection to a http server * * @return the opened low level connection * @throws IOException if the connection fails for any reason. *//*w w w . j a va2 s . c o m*/ @Override AndroidHttpClientConnection openConnection(Request req) throws IOException { // Update the certificate info (connection not secure - set to null) EventHandler eventHandler = req.getEventHandler(); mCertificate = null; eventHandler.certificate(mCertificate); AndroidHttpClientConnection conn = new AndroidHttpClientConnection(); BasicHttpParams params = new BasicHttpParams(); Socket sock = new Socket(mHost.getHostName(), mHost.getPort()); params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192); conn.bind(sock, params); return conn; }
From source file:com.bitsofproof.supernode.core.IRCDiscovery.java
@Override public List<InetSocketAddress> discover() { List<InetSocketAddress> al = new ArrayList<InetSocketAddress>(); try {/*www . j a v a 2s . com*/ log.trace("Connect to IRC server " + server); Socket socket = new Socket(server, port); PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String[] answers = new String[] { "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname" }; String line; boolean stop = false; while (!stop && (line = reader.readLine()) != null) { log.trace("IRC receive " + line); for (int i = 0; i < answers.length; ++i) { if (line.contains(answers[i])) { stop = true; break; } } } String nick = "bop" + new SecureRandom().nextInt(Integer.MAX_VALUE); writer.println("NICK " + nick); writer.println("USER " + nick + " 8 * : " + nick); writer.flush(); log.trace("IRC send: I am " + nick); while ((line = reader.readLine()) != null) { log.trace("IRC receive " + line); if (hasCode(line, new String[] { " 004 ", " 433 " })) { break; } } log.trace("IRC send: joining " + channel); writer.println("JOIN " + channel); writer.println("NAMES"); writer.flush(); while ((line = reader.readLine()) != null) { log.trace("IRC receive " + line); if (hasCode(line, new String[] { " 353 " })) { StringTokenizer tokenizer = new StringTokenizer(line, ":"); String t = tokenizer.nextToken(); if (tokenizer.hasMoreElements()) { t = tokenizer.nextToken(); } tokenizer = new StringTokenizer(t); tokenizer.nextToken(); while (tokenizer.hasMoreTokens()) { String w = tokenizer.nextToken().substring(1); if (!tokenizer.hasMoreElements()) { continue; } try { byte[] m = ByteUtils.fromBase58WithChecksum(w); byte[] addr = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, 0, 0, 0, 0 }; System.arraycopy(m, 0, addr, 12, 4); al.add(new InetSocketAddress(InetAddress.getByAddress(addr), chain.getPort())); } catch (ValidationException e) { log.trace(e.toString()); } } } if (hasCode(line, new String[] { " 366 " })) { break; } } writer.println("PART " + channel); writer.println("QUIT"); writer.flush(); socket.close(); } catch (UnknownHostException e) { log.error("Can not find IRC server " + server, e); } catch (IOException e) { log.error("Can not use IRC server " + server, e); } return al; }
From source file:com.beyondjservlet.gateway.servlet.support.NonBindingSocketFactory.java
private static Socket createNonBindingSocket(String host, int port) throws IOException { return new Socket(host, port); }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(String host, int port) throws IOException { final String resolvedHost = resolver.resolve(host); if (resolvedHost != null && !resolvedHost.equals(host)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); }/*from ww w . ja v a 2 s . c o m*/ } return defaultFactory.createSocket(host, port); }
From source file:org.jasig.portlet.cms.model.security.ClamAVAntiVirus.java
private Socket connect() { Socket socket = null;/*from w w w. j av a 2 s .co m*/ try { socket = new Socket(ip, port); } catch (final Exception e) { socket = null; logger.error(e.getMessage(), e); } return socket; }
From source file:net.mohatu.bloocoin.miner.RegCustom.java
private void register() { try {//from w w w.ja va 2 s . co m String result = new String(); Socket sock = new Socket(this.url, this.port); String command = "{\"cmd\":\"register" + "\",\"addr\":\"" + addr + "\",\"pwd\":\"" + key + "\"}"; DataInputStream is = new DataInputStream(sock.getInputStream()); DataOutputStream os = new DataOutputStream(sock.getOutputStream()); os.write(command.getBytes()); os.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { result += inputLine; } is.close(); os.close(); sock.close(); System.out.println(result); if (result.contains("\"success\": true")) { System.out.println("Registration successful: " + addr); saveBloostamp(); } else if (result.contains("\"success\": false")) { System.out.println("Result: Failed"); JOptionPane.showMessageDialog(Main.scrollPane, "Registration failed.\nCheck your network connection", "Registration Failed", JOptionPane.ERROR_MESSAGE); System.exit(0); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:net.mohatu.bloocoin.miner.RegisterClass.java
private void register() { try {// w w w .j a v a 2 s .c o m String result = new String(); Socket sock = new Socket(this.url, this.port); String command = "{\"cmd\":\"register" + "\",\"addr\":\"" + addr + "\",\"pwd\":\"" + key + "\"}"; DataInputStream is = new DataInputStream(sock.getInputStream()); DataOutputStream os = new DataOutputStream(sock.getOutputStream()); os.write(command.getBytes()); os.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { result += inputLine; } is.close(); os.close(); sock.close(); System.out.println(result); if (result.contains("\"success\": true")) { System.out.println("Registration successful: " + addr); saveBloostamp(); } else if (result.contains("\"success\": false")) { System.out.println("Result: Failed"); MainView.updateStatusText("Registration failed. "); System.exit(0); } } catch (UnknownHostException e) { System.out.println("Error: Unknown host."); } catch (IOException e) { System.out.println("Error: Network error."); } }