Socket programming 1. getByName import java.net.*; public class GetHostName { public static void...
-
Upload
harvey-carroll -
Category
Documents
-
view
213 -
download
1
Transcript of Socket programming 1. getByName import java.net.*; public class GetHostName { public static void...
Socket programming 1
getByName
import java.net.*;
public class GetHostName { public static void main (String args[]) { String host = "www.concordia.ca"; try { InetAddress address = InetAddress.getByName(host); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find "+ host); }
}
}
getByNameimport java.net.*;
public class GetNameByAddress {
public static void main (String args[]) {
try { InetAddress address = InetAddress.getByName("132.205.7.63"); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find 132.205.7.63"); }
}
}
LocalMachineAddressimport java.net.*;
public class LocalMachineAddress {
public static void main (String args[]) {
try { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find this computer's address."); }
}
}
Local machine nameimport java.net.*;
public class LocalMachineName { public static void main (String args[]) {
try { InetAddress address = InetAddress.getLocalHost(); System.out.println("Local Machine Name is " + address.getHostName()); } catch (UnknownHostException e) { System.out.println("No Name for this machine."); }
}
}
Elementary TCP socket
ServerSocket()
Bind()
Listen()
Accept()
Close()
Read()
Write()
Process request
Blocks until connections from client
TCP ServerTCP Client
Connect()
Write()
Read()
Close()
Socket()
Connection establishment(TCP 3-way Handshake)
Data (request)
Data (reply)
Well-known port
Socket functions for elementary TCP client-server
ServerSocket
• ServerSocket(int port) Creates a server socket, bound to the specifie port.
Back
Listen
• accept() method of ServerSocket instance object returns Socket instance object. Accept method listens for a connection to be made to this socket and accepts it.
Back
Read from a socket• DataInputStream– A data input stream lets an application read primitive Java
data types from an underlying input stream in a machine-independent way.
– An application uses a data output stream to write data that can later be read by a data input stream.
– readUTF() method return String data type – It reads from the stream in a representation of a Unicode
character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.
getInputStream() method of Socket class• getInputStream() method of class socket
Returns an input stream type (InputStream) for this socket.
• If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel.
Back
Write to a socket
• DataOutputStream class – A data output stream lets an application write
primitive Java data types to an output stream in a portable way.
– An application can then use a data input stream to read the data back in.
– writeUTF() method writes a string to the underlying output stream using Java modified UTF-8 encoding in a machine-independent manner.
getOutputStream() method of Socket class • getOutputStream() method of Socket class
returns an output stream for this socket.• If this socket has an associated channel then
the resulting output stream delegates all of its operations to the channel.
Back
Socket
• Socket(InetAddress address, int port) Creates a stream socket and connects it to the specified port number at the specified IP address.
Back
TCPServerimport java.net.*;import java.io.*;public class TCPServer {
public static void main (String args[]) {try{
int serverPort = 7896; // the server portServerSocket listenSocket = new ServerSocket(serverPort);while(true) {Socket clientSocket = listenSocket.accept();Connection c = new Connection(clientSocket);}} catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
}}class Connection extends Thread {
DataInputStream in;DataOutputStream out;Socket clientSocket;
public Connection (Socket aClientSocket) {try {
clientSocket = aClientSocket;in = new DataInputStream( clientSocket.getInputStream());out =new DataOutputStream( clientSocket.getOutputStream());this.start();
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}}public void run(){try { // an echo server
String data = in.readUTF(); // read a line of data from the streamout.writeUTF("Thats what i received from you :"+ data);
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());} catch(IOException e) {System.out.println("readline:"+e.getMessage());} finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}}
}
TCPClientimport java.net.*;import java.io.*;public class TCPClient {
public static void main (String args[]) {// arguments supply message and hostname
Socket s = null;try{
int serverPort = 7896;s = new Socket("127.0.0.1", serverPort); DataInputStream in = new DataInputStream( s.getInputStream());DataOutputStream out =new DataOutputStream( s.getOutputStream());out.writeUTF("127.0.0.1"); // UTF is a string encoding see Sn. 4.4String data = in.readUTF(); // read a line of data from the streamSystem.out.println("Received: "+ data) ;
}catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage());}catch (EOFException e){System.out.println("EOF:"+e.getMessage());}catch (IOException e){System.out.println("readline:"+e.getMessage());}finally {if(s!=null) try {s.close();}catch (IOException e){System.out.println("close:"+e.getMessage());}
}}
References
• http://www.ibiblio.org/java/books/jnp/javanetexamples/index.html
• Java Docummentation.• http://users.encs.concordia.ca/~s423_2/
CodeSample/Sockets/Text%20Book/