KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > net > Socket

java.net
Class Socket

java.lang.Object
  extended by java.net.Socket
Direct Known Subclasses:
SSLSocket
See Also:
Top Examples, Source Code, setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketChannel

public void bind(SocketAddress bindpoint)
          throws IOException
See Also:
isBound(), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void close()
           throws IOException
See Also:
isClosed(), SocketException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[17]Send mail over socket
By Anonymous on 2003/10/26 20:05:31  Rate
//Send mail 
 import java.io.*; 
 import java.net.*; 
 public class email  {  
   public static void main ( String args [  ]  )  throws IOException  {  
     Socket sock; 
     DataInputStream dis; 
     PrintStream ps; 
      
     sock = new Socket ( "localhost", 25 ) ; 
     dis = new DataInputStream (  sock.getInputStream (  )  ) ; 
      
     ps = new PrintStream (  sock.getOutputStream (  )  ) ; 
     ps.println ( "mail from: trelford" ) ; 
     System.out.println (  dis.readLine (  )   ) ; 
     String Addressee= "linden"; 
     ps.println ( "rcpt to: " + Addressee  ) ; 
     System.out.println (  dis.readLine (  )   ) ; 
     ps.println ( "data" ) ; 
     System.out.println (  dis.readLine (  )   ) ; 
     ps.println ( "This is the message\n that Java sent" ) ; 
     ps.println ( "." ) ; 
     System.out.println (  dis.readLine (  )   ) ; 
      
     ps.flush (  ) ; 
     sock.close (  ) ; 
    }  
  } 


public void connect(SocketAddress endpoint)
             throws IOException
See Also:
IllegalArgumentException, IllegalBlockingModeException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void connect(SocketAddress endpoint,
                    int timeout)
             throws IOException
See Also:
IllegalArgumentException, IllegalBlockingModeException, SocketTimeoutException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SocketChannel getChannel()
See Also:
ServerSocketChannel.accept, SocketChannel.open
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public InetAddress getInetAddress()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public InputStream getInputStream()
                           throws IOException
See Also:
shutdownInput(), available, close, read, IllegalBlockingModeException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean getKeepAlive()
                     throws SocketException
See Also:
setKeepAlive(boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public InetAddress getLocalAddress()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1693]GetLocalAddress()
By Raj on 2006/01/05 01:26:24  Rate
I want get machine IP address

[1698]Get an IP Address for a URL or URI
By Anonymous on 2006/01/16 14:11:00  Rate
 
  ( Based on freely available online excerpt from Java Developers Journal 1.4 )  
  
  
 try  {  
           InetAddress addr = InetAddress.getByName ( "java.sun.com" ) ; 
           int port = 80; 
        
           // This constructor will block until the connection succeeds 
           Socket socket = new Socket ( addr, port ) ; 
           System.out.println ( "Connection to java.sun.com made" ) ; 
           System.out.println ( "IP is "+socket.getInetAddress (  )  ) ; 
        }  catch  ( UnknownHostException e )   {  
         System.out.println ( e.getCause (  )  ) ; 
        }  catch  ( IOException e )   {  
         System.out.println ( e.getCause (  )  ) ; 
        }  
        
       // Create a socket with a timeout 
       try  {  
           InetAddress addr = InetAddress.getByName ( "java.sun.com" ) ; 
           int port = 80; 
           SocketAddress sockaddr = new InetSocketAddress ( addr, port ) ; 
        
           // Create an unbound socket 
           Socket sock = new Socket (  ) ; 
        
           // This method will block no more than timeoutMs. 
           // If the timeout occurs, SocketTimeoutException is thrown. 
           int timeoutMs = 2000; // 2 seconds 
           sock.connect ( sockaddr, timeoutMs ) ; 
        }  catch  ( UnknownHostException e )   {  
        }  catch  ( SocketTimeoutException e )   {  
        }  catch  ( IOException e )   {  
        }  
      } 


public int getLocalPort()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SocketAddress getLocalSocketAddress()
See Also:
bind(SocketAddress), getLocalPort(), getLocalAddress()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean getOOBInline()
                     throws SocketException
See Also:
setOOBInline(boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public OutputStream getOutputStream()
                             throws IOException
See Also:
IllegalBlockingModeException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getPort()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getReceiveBufferSize()
                         throws SocketException
See Also:
setReceiveBufferSize(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public SocketAddress getRemoteSocketAddress()
See Also:
connect(SocketAddress), connect(SocketAddress, int), getPort(), getInetAddress()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean getReuseAddress()
                        throws SocketException
See Also:
setReuseAddress(boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getSendBufferSize()
                      throws SocketException
See Also:
setSendBufferSize(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getSoLinger()
                throws SocketException
See Also:
setSoLinger(boolean, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getSoTimeout()
                 throws SocketException
See Also:
setSoTimeout(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean getTcpNoDelay()
                      throws SocketException
See Also:
setTcpNoDelay(boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getTrafficClass()
                    throws SocketException
See Also:
setTrafficClass(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isBound()
See Also:
bind(java.net.SocketAddress)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isClosed()
See Also:
close()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isConnected()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isInputShutdown()
See Also:
shutdownInput()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isOutputShutdown()
See Also:
shutdownOutput()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void sendUrgentData(int data)
                    throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setKeepAlive(boolean on)
                  throws SocketException
See Also:
getKeepAlive()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setOOBInline(boolean on)
                  throws SocketException
See Also:
getOOBInline()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setPerformancePreferences(int connectionTime,
                                      int latency,
                                      int bandwidth)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setReceiveBufferSize(int size)
                          throws SocketException
See Also:
getReceiveBufferSize(), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setReuseAddress(boolean on)
                     throws SocketException
See Also:
isBound(), isClosed(), bind(SocketAddress), getReuseAddress()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setSendBufferSize(int size)
                       throws SocketException
See Also:
getSendBufferSize(), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setSocketImplFactory(SocketImplFactory fac)
                                 throws IOException
See Also:
SecurityManager.checkSetFactory(), SocketImplFactory.createSocketImpl(), SecurityException, SocketException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setSoLinger(boolean on,
                        int linger)
                 throws SocketException
See Also:
getSoLinger(), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setSoTimeout(int timeout)
                  throws SocketException
See Also:
getSoTimeout()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[919]Socket connection
By wilson { at } lagosnet { dot } com { dot } br on 2004/09/22 07:41:28  Rate
import java.net.*; 
 import java.net.SocketAddress; 
 import java.io.*; 
  
  
 public class socket 
    {  
   public void connect ( InetAddress host, int port )  throws SocketException, IOException 
       {  
          s = s.createSocket ( host, port ) ; 
          _connectAction_ (  ) ; 
       }  
  
  
      
   public static void main ( String args [  ]  )  throws IOException, SocketException  
      {  
     String host; 
     if ( args.length > 0 )  
       host=args [ 0 ] ; 
     else 
       host="www.esqueleto.com.br"; 
      
     int porta; 
     if ( args.length > 1 )  
       porta= Integer.parseInt ( args [ 1 ]  ) ; 
     else 
       porta= 80; 
      
     int timeout= 5000; 
      
     Socket s = new Socket  (  ) ; 
     s.setSoTimeout ( timeout ) ; 
     s.connected ( InetAddress.getByName ( host )  ) ; 
      
     if ( s==null )  
       System.out.println ( "A conexao nao pode ser estabelecida" ) ; 
     else 
       System.out.println ( s ) ; 
      } //main 
    } //socket 
      
 


public void setTcpNoDelay(boolean on)
                   throws SocketException
See Also:
getTcpNoDelay()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setTrafficClass(int tc)
                     throws SocketException
See Also:
getTrafficClass()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void shutdownInput()
                   throws IOException
See Also:
isInputShutdown(), setSoLinger(boolean, int), close(), shutdownOutput()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void shutdownOutput()
                    throws IOException
See Also:
isOutputShutdown(), setSoLinger(boolean, int), close(), shutdownInput()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket(String host,
              int port)
       throws UnknownHostException,
              IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SocketImplFactory.createSocketImpl(), SocketImpl, setSocketImplFactory(java.net.SocketImplFactory), SecurityException, InetAddress.getByName
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public Socket(String host,
                         int port,
                         boolean stream)
       throws IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SocketImplFactory.createSocketImpl(), SocketImpl, setSocketImplFactory(java.net.SocketImplFactory), SecurityException, InetAddress.getByName
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket(String host,
              int port,
              InetAddress localAddr,
              int localPort)
       throws IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SecurityException, InetAddress.getByName
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket(InetAddress address,
              int port)
       throws IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SocketImplFactory.createSocketImpl(), SocketImpl, setSocketImplFactory(java.net.SocketImplFactory), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[16]Connects to an NTP server and asks the time
By Anonymous on 2002/08/05 22:10:19  Rate
//connects to an NTP server and asks the time 
 import java.io.*; 
 import java.net.*; 
 public class AskTime  {  
   public static void main ( String a [  ]  )  throws Exception  {  
     if  ( a.length!=1 )   {  
         System.out.println ( "usage: java AskTime  < systemname >  " ) ; 
   System.exit ( 0 ) ; 
      }  
     String machine = a [ 0 ] ; 
     final int daytimeport = 13; 
      
     Socket so = new Socket ( machine, daytimeport ) ; 
     BufferedReader br = new BufferedReader (  new InputStreamReader (  
                             so.getInputStream (  )   )   ) ; 
           
     String timestamp = br.readLine (  ) ; 
     System.out.println (  machine + " says it is " + timestamp  ) ; 
  } 


@Deprecated
public Socket(InetAddress host,
                         int port,
                         boolean stream)
       throws IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SocketImplFactory.createSocketImpl(), SocketImpl, setSocketImplFactory(java.net.SocketImplFactory), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket(InetAddress address,
              int port,
              InetAddress localAddr,
              int localPort)
       throws IOException
See Also:
SecurityManager.checkConnect(java.lang.String, int), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Socket(Proxy proxy)
See Also:
ProxySelector, SecurityException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Socket(SocketImpl impl)
          throws SocketException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags