List of usage examples for java.net InetSocketAddress getAddress
public final InetAddress getAddress()
From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java
@Test public void testRcptHookTemporaryError() throws Exception { RcptHook hook = new RcptHook() { @Override//ww w.j a v a 2 s . c o m public void init(Configuration config) throws ConfigurationException { } @Override public void destroy() { } public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) { if (RCPT1.equals(rcpt.toString())) { return new HookResult(HookReturnCode.DENYSOFT); } else { return new HookResult(HookReturnCode.DECLINED); } } }; InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null; try { server = createServer(createProtocol(hook), address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.setSender(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.addRecipient(RCPT1); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode())); client.addRecipient(RCPT2); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); } finally { if (server != null) { server.unbind(); } } }
From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java
@Test public void testMessageHookPermanentError() throws Exception { TestMessageHook testHook = new TestMessageHook(); MessageHook hook = new MessageHook() { @Override/*from w w w .ja va 2s .c o m*/ public void init(Configuration config) throws ConfigurationException { } @Override public void destroy() { } public HookResult onMessage(SMTPSession session, MailEnvelope mail) { return new HookResult(HookReturnCode.DENY); } }; InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null; try { server = createServer(createProtocol(hook, testHook), address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.setSender(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.addRecipient(RCPT2); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); assertFalse(client.sendShortMessageData(MSG1)); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); Iterator<MailEnvelope> queued = testHook.getQueued().iterator(); assertFalse(queued.hasNext()); } finally { if (server != null) { server.unbind(); } } }
From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java
@Test public void testMessageHookTemporaryError() throws Exception { TestMessageHook testHook = new TestMessageHook(); MessageHook hook = new MessageHook() { @Override//from w ww .ja v a 2s .c o m public void init(Configuration config) throws ConfigurationException { } @Override public void destroy() { } public HookResult onMessage(SMTPSession session, MailEnvelope mail) { return new HookResult(HookReturnCode.DENYSOFT); } }; InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null; try { server = createServer(createProtocol(hook, testHook), address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.setSender(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.addRecipient(RCPT2); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); assertFalse(client.sendShortMessageData(MSG1)); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); Iterator<MailEnvelope> queued = testHook.getQueued().iterator(); assertFalse(queued.hasNext()); } finally { if (server != null) { server.unbind(); } } }
From source file:org.codehaus.stomp.tcp.TcpTransport.java
protected void connect() throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, URISyntaxException { if (socket == null && socketFactory == null) { throw new IllegalStateException("Cannot connect if the socket or socketFactory have not been set"); }//www.j av a 2 s . com InetSocketAddress localAddress = null; InetSocketAddress remoteAddress = null; if (localLocation != null) { localAddress = new InetSocketAddress(InetAddress.getByName(localLocation.getHost()), localLocation.getPort()); } if (remoteLocation != null) { String host = remoteLocation.getHost(); remoteAddress = new InetSocketAddress(host, remoteLocation.getPort()); } if (socket != null) { if (localAddress != null) { socket.bind(localAddress); } // If it's a server accepted socket.. we don't need to connect it // to a remote address. if (remoteAddress != null) { if (connectionTimeout >= 0) { socket.connect(remoteAddress, connectionTimeout); } else { socket.connect(remoteAddress); } } } else { // For SSL sockets.. you can't create an unconnected socket :( // This means the timout option are not supported either. if (localAddress != null) { socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort(), localAddress.getAddress(), localAddress.getPort()); } else { socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort()); } } // TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), ioBufferSize); this.dataIn = new DataInputStream(socket.getInputStream());// new DataInputStream(buffIn); // TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), ioBufferSize); this.dataOut = new DataOutputStream(socket.getOutputStream());// new DataOutputStream(buffOut); }
From source file:com.budrotech.jukebox.service.ssl.SSLSocketFactory.java
/** * @since 4.1//from w w w .j a va 2 s .co m */ public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpParams params) throws IOException { if (remoteAddress == null) { throw new IllegalArgumentException("Remote address may not be null"); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } SSLSocket sslSocket = (SSLSocket) (sock != null ? sock : createSocket()); if (localAddress != null) { // sslSocket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); sslSocket.bind(localAddress); } int connTimeout = HttpConnectionParams.getConnectionTimeout(params); int soTimeout = HttpConnectionParams.getSoTimeout(params); try { sslSocket.connect(remoteAddress, connTimeout); } catch (SocketTimeoutException ex) { throw new ConnectTimeoutException(String.format("Connect to %s/%s timed out", remoteAddress.getHostName(), remoteAddress.getAddress())); } sslSocket.setSoTimeout(soTimeout); if (this.hostnameVerifier != null) { try { this.hostnameVerifier.verify(remoteAddress.getHostName(), sslSocket); // verifyHostName() didn't blowup - good! } catch (IOException iox) { // close the socket before re-throwing the exception try { sslSocket.close(); } catch (Exception x) { /*ignore*/ } throw iox; } } return sslSocket; }
From source file:com.techcavern.pircbotz.IdentServer.java
/** * Wait for and process the next connection. * * @throws IOException If any error occurred during reading or writing */// w w w .j ava2 s. c o m public String handleNextConnection(InetSocketAddress remoteAddress, String line) throws IOException { //Get and validate Ident from server if (StringUtils.isBlank(line)) { log.error("Ignoring connection from " + remoteAddress + ", received blank line"); return null; } String[] parsedLine = StringUtils.split(line, ", "); if (parsedLine.length != 2) { log.error("Ignoring connection from " + remoteAddress + ", recieved unknown line: " + line); return null; } int localPort = Utils.tryParseInt(parsedLine[0], -1); int remotePort = Utils.tryParseInt(parsedLine[1], -1); if (localPort == -1 || remotePort == -1) { log.error("Ignoring connection from " + remoteAddress + ", recieved unparsable line: " + line); return null; } //Grab the IdentEntry for this ident log.debug("Received ident request from " + remoteAddress + ": " + line); IdentEntry identEntry = null; synchronized (identEntries) { for (IdentEntry curIdentEntry : identEntries) if (curIdentEntry.getRemoteAddress().equals(remoteAddress.getAddress()) && curIdentEntry.getRemotePort() == remotePort && curIdentEntry.getLocalPort() == localPort) { identEntry = curIdentEntry; break; } } if (identEntry == null) { String response = localPort + ", " + remotePort + " : ERROR : NO-USER"; log.error("Unknown ident " + line + " from " + remoteAddress + ", responding with: " + response); return response; } //Respond to correct ident entry with login String response = line + " : USERID : UNIX : " + identEntry.getLogin(); log.debug("Responded to ident request from " + remoteAddress + " with: " + response); return response; }
From source file:org.cohorte.remote.multicast.MulticastDiscovery.java
@Override public void handlePacket(final InetSocketAddress aSender, final byte[] aContent) { // Read the packet content final String content; try {//from ww w . ja v a 2s . c om content = new String(aContent, CHARSET_UTF8).trim(); } catch (final UnsupportedEncodingException ex) { pLogger.log(LogService.LOG_ERROR, "Error reading multicast packet data", ex); return; } // Parse it from JSON final PelixMulticastPacket endpointPacket; try { final JSONObject parsed = new JSONObject(content); endpointPacket = new PelixMulticastPacket(parsed); } catch (final JSONException ex) { // Log pLogger.log(LogService.LOG_ERROR, "Error parsing a multicast packet", ex); return; } // Avoid handling our own packets if (endpointPacket.isFromSender(pFrameworkUID)) { return; } // Get information about the sender final InetAddress senderAddress = aSender.getAddress(); // Get the kind of event final String event = endpointPacket.getEvent(); // Dispatch... if (IPacketConstants.EVENT_DISCOVERY.equals(event)) { // Discovery request: send a packet back pDispatcherServlet.sendDiscovered(senderAddress.getHostAddress(), endpointPacket.getAccessPort(), endpointPacket.getAccessPath()); } else { // Handle an end point event handleEvent(endpointPacket, senderAddress); } }
From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java
@Test public void testHeloEnforcementDisabled() throws Exception { TestMessageHook hook = new TestMessageHook(); InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null;// w w w . jav a2 s . c o m try { Protocol protocol = createProtocol(hook); ((SMTPConfigurationImpl) protocol.getConfiguration()).setHeloEhloEnforcement(false); server = createServer(protocol, address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.setSender(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertFalse(queued.hasNext()); } finally { if (server != null) { server.unbind(); } } }
From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java
@Test public void testInvalidNoBracketsEnformance() throws Exception { TestMessageHook hook = new TestMessageHook(); InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null;/* w w w .j a va 2 s . com*/ try { Protocol protocol = createProtocol(hook); ((SMTPConfigurationImpl) protocol.getConfiguration()).setUseAddressBracketsEnforcement(false); server = createServer(protocol, address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.mail(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.addRecipient(RCPT1); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertFalse(queued.hasNext()); } finally { if (server != null) { server.unbind(); } } }
From source file:com.github.hrpc.rpc.Server.java
public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Option conf, String rangeConf) throws IOException { try {/*w w w . j a v a 2 s. co m*/ IntegerRanges range = null; if (rangeConf != null) { range = conf.getRange(rangeConf, ""); } if (range == null || range.isEmpty() || (address.getPort() != 0)) { socket.bind(address, backlog); } else { for (Integer port : range) { if (socket.isBound()) break; try { InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port); socket.bind(temp, backlog); } catch (BindException e) { //Ignored } } if (!socket.isBound()) { throw new BindException("Could not find a free port in " + range); } } } catch (SocketException e) { throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e); } }