CS 580 Client-Server Programming Fall Semester, 2000 Simple Java Client |
||
---|---|---|
© 2000, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 13-Sep-00 |
Building a Connection
Transport layer association is uniquely defined by the following:
$ telnet sdsu.edu 13 Trying 130.191.229.14... Connected to sdsu.edu. Escape character is '^]'. Thu Feb 3 22:30:35 2000 Connection closed by foreign host. $
IP Connecting Parts
Information a client normally needs:
Information |
Data
from example
|
protocol |
TCP
(implied by the telnet program)
|
name
of the server machine
|
sdsu.edu |
port
number on the server machine
|
13 |
Information |
Where
does it come from?
|
address
of the local machine
|
Automatically
retrieved from the OS
|
port
number on the local machine
|
OS
supplies random open port
|
Simple Java Client
import java.net.*; import java.io.*; class GetTime { public static void main(String[] args) { String hostName; Socket client = null; if ( args.length != 1 ) { System.out.println("Usage: java GetTime hostName"); System.exit(1); } hostName = args[0]; try { client = new Socket(hostName, 13); } catch (UnknownHostException hostError) { hostError.printStackTrace(); System.exit(1); } catch (IOException genericError) { System.err.println(genericError.getMessage()); System.exit(1); } try { BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received: " + inputLine); } } catch (IOException IOError) { System.err.println(IOError.getMessage()); System.exit(1); } } }
java.net.Socket
Several constructors:
Socket(String hostname, int portNumber) Socket(InetAddress hostAddress, int portNumber)
The constructor builds the connection
Exceptions thrown by the constructors:
java.net.Socket Methods
close( )
Terminates an existing connection.
getInputStream( ) getOutputStream( )
These two provide access to IO through the connection.
NOTE network IO is always done in packets. This may produce strange results. Solution: use the Buffered IO classes.
getLocalPort( )
Domain Name Service (DNS)
On UNIX:
DNS through java.net.InetAddress
Use following static methods to create InetAddress
InetAddress getByName(String host) InetAddress[] getAllByName(String host) InetAddress getLocalHost( )
String getHostName( ) byte[] getAddress( )
InetAddress Example import java.net.InetAddress; import java.net.InetAddress; import java.io.*; class SimpleDNS { public static void main(String[] args) { String hostName; if ( args.length != 1 ) { System.out.println("Usage: java SimpleDNS hostName"); System.exit(1); } hostName = args[0]; try { InetAddress address = InetAddress.getByName(hostName); byte ip[] = address.getAddress(); for (int octet=0; octet < ip.length; octet++) { System.out.print( ((int)ip[octet] & 0xff) + " " ); } System.out.println(); } catch (IOException DNSerror) { System.err.println(DNSerror); System.exit(1); } } }
Running the Example
$ java SimpleDNS www.sdsu.edu 130 191 13 5 $
Closing Sockets
A socket is closed when:
PortScanner From Java Network Programming chapter 10
import java.net.*; import java.io.*; public class PortScanner { public static void main( String[] args) throws IOException { String host = "localhost"; Socket connection = null; try { for (int port = 1; port < 65536; port++) { try { connection = new Socket(host, port); System.out.println("Server on port " + port ) } catch (IOException error ) {// no server on this port } } } catch (UnknownHostException error ) { System.err.println( error ); } finally { if (connection != null ) connection.close(); } } }
Socket Options
You to change parameters controlling how native sockets send and receive data
TCP_NODELAY
public void setTcpNoDelay(boolean on) throws SocketException public boolean getTcpNoDelay() throws SocketExceptionDisable/enable Nagle's algorithm
SO_LINGER
public void setSoLinger(boolean on, int lingerSeconds)
throws SocketException
public boolean getSoLinger() throws SocketException
What happens to waiting packets when socket is closed?
Option 1
SO_TIMEOUT
public void setSoTimeout(int timeout) throws SocketException public boolean getSoTimeout() throws SocketExceptionHow long does read() block on a socket?
Default is to block until there is data.
SO_TIMEOUT
SO_RCVBUF & SO_SNDBUF
public void setReceiveBufferSize(int size) throws SocketException public int getReceiveBufferSize() throws SocketException
public void setSendBufferSize(int size) throws SocketException public int getSendBufferSize() throws SocketException
Suggests the input/output buffer size used in the TCP stack
SO_KEEPALIVE
New in JDK 1.3
Have client send data on an idle connection to insure server is still alive
Default is off
Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 13-Sep-00    Next