Overview about socket

download report

Transcript Overview about socket

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);
}
}
}
getByName
import 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");
}
}
}
LocalMachineAddress
import 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 name
import 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
TCP Server
ServerSocket()
TCP Client
Well-known port
Bind()
Listen()
Accept()
Socket()
Connect()
Write()
Connection establishment
(TCP 3-way Handshake)
Data (request)
Blocks until connections
from client
Read()
Process request
Read()
Close()
Data (reply)
Write()
Close()
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 machineindependent 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
TCPServer
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main (String args[]) {
try{
int serverPort = 7896; // the server port
ServerSocket 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 stream
out.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*/}}
}
}
TCPClient
import 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.4
String data = in.readUTF(); // read a line of data from the stream
System.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/javane
texamples/index.html
• Java Docummentation.
• http://users.encs.concordia.ca/~s423_2/Code
Sample/Sockets/Text%20Book/