Sockets for Clients
-
Upload
kiona-harding -
Category
Documents
-
view
34 -
download
0
description
Transcript of Sockets for Clients
Sockets for ClientsSockets for Clients
[email protected]@feu.ac.th
Socket BasicsSocket Basics
Connect to a remote machineConnect to a remote machine Send dataSend data Receive dataReceive data Close a connectionClose a connection Bind to a portBind to a port Listen for incoming dataListen for incoming data Accept connections from remote machines Accept connections from remote machines
on the bound porton the bound port
The Socket Class The Socket Class
อยู่��ใน อยู่��ใน javajava..netnet..SocketSocket ทำงน ทำงน clientclient--side side สร้�ง สร้�ง TCP network connectionsTCP network connections กร้ส�งและร้�บ จะผ่�น กร้ส�งและร้�บ จะผ่�น StreamStream
The Socket ClassThe Socket Class The ConstructorsThe Constructors
public Socketpublic Socket((String host, int portString host, int port) ) throws throws UnknownHostException, IOExceptionUnknownHostException, IOException
try { Socket toOReilly = new Socket("www.oreilly.com", 80); // send and receive data...
} catch (UnknownHostException ex) { System.err.println(ex); } catch (IOException ex) { System.err.println(ex); }
The Socket ClassThe Socket Class ตั�วอยู่�งโปร้แกร้มตัร้วจสอบ ตั�วอยู่�งโปร้แกร้มตัร้วจสอบ server Portserver Port import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;;
public class LowPortScanner {public class LowPortScanner { public static void mainpublic static void main((StringString[] [] argsargs) ) {{ String host String host = "= "localhostlocalhost"";; if if ((argsargs..length > 0length > 0) ) {{ host host = = argsargs[[00]];; }} for for ((int i int i = = 1; i < 1024; i1; i < 1024; i++) ++) {{ try {try { Socket s Socket s = = new Socketnew Socket((host, ihost, i));; SystemSystem..outout..printlnprintln("("There is a server on port There is a server on port " + " + i i + " + " of of " " + + hosthost));; }} catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; break;break; }} catch catch ((IOException exIOException ex) ) {{ // // must not be a server on this portmust not be a server on this port }} } } // // end forend for } } // // end mainend main } } // // end PortScannerend PortScanner
The Socket ClassThe Socket Class ผ่ลกร้ร้�นโปร้แกร้มผ่ลกร้ร้�นโปร้แกร้ม((ขึ้��นอยู่��ก�บเคร้!"องขึ้��นอยู่��ก�บเคร้!"อง))
There is a server on port 21 of localhostThere is a server on port 21 of localhost
There is a server on port 22 of localhostThere is a server on port 22 of localhost
There is a server on port 23 of localhostThere is a server on port 23 of localhost
There is a server on port 25 of localhostThere is a server on port 25 of localhost
There is a server on port 37 of localhostThere is a server on port 37 of localhost
There is a server on port 111 of localhostThere is a server on port 111 of localhost
There is a server on port 139 of localhostThere is a server on port 139 of localhost
There is a server on port 210 of localhostThere is a server on port 210 of localhost
There is a server on port 515 of localhostThere is a server on port 515 of localhost
There is a server on port 873 of localhostThere is a server on port 873 of localhost
public Socketpublic Socket((InetAddress host, int portInetAddress host, int port) ) throws throws IOExceptionIOException
try {try {
InetAddress oreilly InetAddress oreilly = = InetAddressInetAddress..getByNamegetByName("("wwwwww..oreillyoreilly..comcom")");;
Socket oreillySocket Socket oreillySocket = = new Socketnew Socket((oreilly , 80oreilly , 80));;
// // send and receive datasend and receive data......
}}
catch catch ((UnknownHostException exUnknownHostException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
catch catch ((IOException exIOException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
The Socket ClassThe Socket Class import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;;
public class HighPortScanner {public class HighPortScanner { public static void mainpublic static void main((StringString[] [] argsargs) ) {{ String host String host = "= "localhostlocalhost"";; if if ((argsargs..length > 0length > 0) ) {{ host host = = argsargs[[00]];; }} try {try { InetAddress theAddress InetAddress theAddress = = InetAddressInetAddress..getByNamegetByName((hosthost));; for for ((int i int i = = 1024; i < 65536; i1024; i < 65536; i++) ++) {{ try {try { Socket theSocket Socket theSocket = = new Socketnew Socket((theAddress, itheAddress, i));; SystemSystem..outout..printlnprintln("("There is a server on port There is a server on port " " + + i i + " + " of of " + " + hosthost));; }} catch catch ((IOException exIOException ex) ) {{ // // must not be a server on this portmust not be a server on this port }} } } // // end forend for } } // // end tryend try catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} } } // // end mainend main } } // // end HighPortScannerend HighPortScanner
public Socketpublic Socket((String host, int port, String host, int port, InetAddress interface, int localPortInetAddress interface, int localPort))
try {try { InetAddress inward InetAddress inward = =
InetAddressInetAddress..getByNamegetByName("("routerrouter")");; Socket socket Socket socket = = new Socketnew Socket("("mailmail"", 25, inward, 0, 25, inward, 0));; // // work with the socketswork with the sockets...... }} catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} catch catch ((IOException exIOException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }}
public Socketpublic Socket((InetAddress host, int port, InetAddress host, int port, InetAddress interface, int localPortInetAddress interface, int localPort))
try {try { InetAddress inward InetAddress inward = = InetAddressInetAddress..getByNamegetByName("("routerrouter")");; InetAddress mail InetAddress mail = = InetAddressInetAddress..getByNamegetByName("("mailmail")");; Socket socket Socket socket = = new Socketnew Socket((mail, 25, inward, 0mail, 25, inward, 0));; // // work with the socketswork with the sockets...... }} catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} catch catch ((IOException exIOException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }}
ขึ้�อม�ลเก#"ยู่วก�บ ขึ้�อม�ลเก#"ยู่วก�บ SocketSocket
public InetAddress getInetAddresspublic InetAddress getInetAddress( ) ( ) public int getPortpublic int getPort( ) ( ) public int getLocalPortpublic int getLocalPort( ) ( ) public InetAddress getLocalAddresspublic InetAddress getLocalAddress( ) ( ) public InputStream getInputStreampublic InputStream getInputStream( ) ( )
throws IOExceptionthrows IOException public OutputStream getOutputStreampublic OutputStream getOutputStream( ) ( )
throws IOExceptionthrows IOException
public InetAddress getInetAddresspublic InetAddress getInetAddress( ) ( )
try {try {
Socket theSocket Socket theSocket = = new Socketnew Socket("("javajava..sunsun..comcom"", 80, 80));;
InetAddress host InetAddress host = = theSockettheSocket..getInetAddressgetInetAddress( )( );;
SystemSystem..outout..printlnprintln("("Connected to remote host Connected to remote host " + " + hosthost));;
} } // // end tryend try
catch catch ((UnknownHostException exUnknownHostException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
catch catch ((IOException exIOException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
int getLocalPortint getLocalPort( ) ( ) try {try {
Socket theSocket Socket theSocket = = new Socketnew Socket("("javajava..sunsun..comcom"", 80, true, 80, true));;
int localPort int localPort = = theSockettheSocket..getLocalPortgetLocalPort( )( );;
SystemSystem..outout..printlnprintln("("Connecting from local port Connecting from local port " + " + localPortlocalPort));;
} } // // end tryend try
catch catch ((UnknownHostException exUnknownHostException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
catch catch ((IOException exIOException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
public InetAddress getLocalAddresspublic InetAddress getLocalAddress( )( ) try {try {
Socket theSocket Socket theSocket = = new Socketnew Socket((hostname, 80hostname, 80));;
InetAddress localAddress InetAddress localAddress = = theSockettheSocket..getLocalAddressgetLocalAddress( )( );;
SystemSystem..outout..printlnprintln("("Connecting from local address Connecting from local address " + " + localAddresslocalAddress));;
} } // // end tryend try
catch catch ((UnknownHostException exUnknownHostException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
catch catch ((IOException exIOException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
socket informationsocket information import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;; public class socketInfo {public class socketInfo { public static void mainpublic static void main((StringString[] [] argsargs) ) {{ String hostNameString hostName="="wwwwww..thairaththairath..coco..thth"";; try {try {
Socket theSocket Socket theSocket = = new Socketnew Socket((hostName, 80hostName, 80));; SystemSystem..outout..printlnprintln("("Connected to Connected to " + " + theSockettheSocket..getInetAddressgetInetAddress( ) ( ) + " + " on port on port " + " + theSockettheSocket..getPortgetPort( ) + " ( ) + " from port from port " " + + theSockettheSocket..getLocalPortgetLocalPort( ) + " ( ) + " of of " " + + theSockettheSocket..getLocalAddressgetLocalAddress( ))( ));; } } // // end tryend try catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln("("I can't find I can't find "+"+hostName hostName ));; }} catch catch ((SocketException exSocketException ex) ) {{ SystemSystem..errerr..printlnprintln("("Could not connect to Could not connect to "+"+hostName hostName ));; }} catch catch ((IOException exIOException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} } } // // end mainend main } } // // end SocketInfoend SocketInfo
ผ่ลกร้ร้�น Connected to www.thairath.co.th/203.151.217.76 on port 80 from port 2678 of /192.168.2.13
public InputStream getInputStreampublic InputStream getInputStream( ) ( ) throws IOExceptionthrows IOException import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;; class datetimeClient {class datetimeClient { public static void mainpublic static void main((StringString[] [] argsargs) ) {{ String hostname;String hostname; if if ((argsargs..length > 0length > 0) ) {{ hostname hostname = = argsargs[[00]];; }} else {else { hostname hostname = "= "timetime..nistnist..govgov"";; }} try {try { Socket theSocket Socket theSocket = = new Socketnew Socket((hostname, 13hostname, 13));; InputStream timeStream InputStream timeStream = = theSockettheSocket..getInputStreamgetInputStream( )( );; StringBuffer time StringBuffer time = = new StringBuffernew StringBuffer( )( );; int c;int c; while while ((((c c = = timeStreamtimeStream..readread( )) != ( )) != -1-1) ) timetime..appendappend((((charchar) ) cc));; String timeString String timeString = = timetime..toStringtoString( ).( ).trimtrim( )( ); ; SystemSystem..outout..printlnprintln("("It is It is " + " + timeString timeString + " + " at at " + " + hostnamehostname));; } } // // end tryend try catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} catch catch ((IOException exIOException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} } } // // end mainend main } } // // end DaytimeClientend DaytimeClient
public OutputStream getOutputStreampublic OutputStream getOutputStream( ) ( ) throws IOExceptionthrows IOException import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;; class opStream{class opStream{ public static void mainpublic static void main((StringString[] [] argvargv)) {{ OutputStreamWriter outOutputStreamWriter out==null;null; try {try {
Socket http Socket http = = new Socketnew Socket("("wwwwww..oreillyoreilly..comcom"", 80, 80));;
OutputStream raw OutputStream raw = = httphttp..getOutputStreamgetOutputStream( )( );;
OutputStream buffered OutputStream buffered = = new BufferedOutputStreamnew BufferedOutputStream((rawraw));;
out out = = new OutputStreamWriternew OutputStreamWriter((buffered, buffered, ""ASCIIASCII")");;
outout..writewrite("("GET GET / / HTTP 1.0\r\n\r\nHTTP 1.0\r\n\r\n")");;
// // read the server responseread the server response......
}}
catch catch ((Exception exException ex) ) {{
SystemSystem..errerr..printlnprintln((exex));;
}}
finally {finally {
try {try {
outout..closeclose( )( );;
}}
catch catch ((Exception exException ex) ) {} {}
}}
}} }}
Port ScannerPort Scanner import javaimport java..netnet.*.*;; import javaimport java..ioio.*.*;; class PortScanner {class PortScanner { public static void mainpublic static void main((StringString[] [] argsargs) ) {{ String host String host = "= "localhostlocalhost"";; if if ((argsargs..length > 0length > 0) ) {{ host host = = argsargs[[00]];; }} try {try { InetAddress theAddress InetAddress theAddress = = InetAddressInetAddress..getByNamegetByName((hosthost));; for for ((int i int i = = 1; i < 65536; i1; i < 65536; i++) ++) {{ Socket connection Socket connection = = null;null; try {try { connection connection = = new Socketnew Socket((host, ihost, i));; SystemSystem..outout..printlnprintln("("There is a server on port There is a server on port " " + + i i + " + " of of " + " + hosthost));; }} catch catch ((IOException exIOException ex) ) {{ // // must not be a server on this portmust not be a server on this port }} finally {finally { try {try { if if ((connection connection != != nullnull) ) connectionconnection..closeclose( )( ); ; }} catch catch ((IOException exIOException ex) ) {}{} }} } } // // end forend for } } // // end tryend try catch catch ((UnknownHostException exUnknownHostException ex) ) {{ SystemSystem..errerr..printlnprintln((exex));; }} } } // // end mainend main } } // // end PortScannerend PortScanner