socket programming by ratnakar kamath. what is a socket? server has a socket bound to a specific...

23
Socket Programming By Ratnakar Kamath

Upload: myles-welch

Post on 17-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Socket Programming

By

Ratnakar Kamath

Page 2: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What Is a Socket?

Server has a socket bound to a specific port number.

Client makes a connection request.Server accepts.A socket is one endpoint of a two-way

communication link between two programs running on the network.

Page 3: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Sockets in Java

Java.net package provides a class , Socket, that implements one side of a two way communication.

Sits on a platform-dependent implementation, hiding the details of any particular system.

Also has ServerSocket class.

Page 4: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Communication

What the server needs to do. Create a server socket. Listen for incoming connection attempts. Communicate with client. Interact until it is time to close the connection. Server or client can close the connection. Go back to listening.

Page 5: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Communication

What the client needs to do Connect to server. Send data. Receive data. Close the connection.

Page 6: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

java.net.Socket

Constructors: Socket(): Creates an unconnected socket. Socket(InetAddress address, int port)

Creates a stream socket and connects to the specified port at the specified address.

Socket(InetAddress address, int port, InetAddress localAddr, int localPort):

Creates a socket and connects it to the specified remote address on the specified remote port.

Page 7: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Things I didn’t tell you

The actual work of the socket is performed by an instance of the SocketImpl class.

The abstract class SocketImpl is a common superclass of all classes that actually implement sockets. It is used to create both client and server sockets.

Page 8: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Methods of java.net.Socket

Bind(SocketAddress)close() connect(SocketAddress )isConnected() isClosed() isBound()

Page 9: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

Page 10: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

client = new Socket( server, port_id );

server = new ServerSocket( PORT );

Page 11: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

   DataOutputStream os;   DataInputStream is;

Page 12: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

  Socket client = server.accept();

Page 13: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

  is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); String line = is.readLine(); os.writeBytes("Hello\n");

Page 14: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

client.close();

Page 15: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

is = new DataInputStream(client.getInputStream() );   os = new DataOutputStream( client.getOutputStream() );

Page 16: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

String line = is.readLine();

os.writeBytes("Hello\n");

Page 17: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

What happens under the hood?

ServerClient

Create Socket

Create streams to connect with client

Wait for client

Communicate with client

Close connection

Create Socket object

Create streams to connect with server

Communicate with server

Close socket

 client.close(); 

Page 18: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Sample Program

import java.io.*;import java.net.*;

public class echo3 {    public static void main(String args[]) {

// declaration section:// declare a server socket and a client socket for the server// declare an input and an output stream

        ServerSocket echoServer = null;        String line;        DataInputStream is;        PrintStream os;        Socket clientSocket = null;

// Try to open a server socket on port 9999// Note that we can't choose a port less than 1023 if we are not// privileged users (root)

        try {           echoServer = new ServerSocket(9999);        }        catch (IOException e) {           System.out.println(e);        }  

// Create a socket object from the ServerSocket to listen and accept // connections.// Open input and output streams

try {           clientSocket = echoServer.accept();           is = new DataInputStream(clientSocket.getInputStream());           os = new PrintStream(clientSocket.getOutputStream());

// As long as we receive data, echo that data back to the client.

           while (true) {             line = is.readLine();             os.println(line);            }        }   catch (IOException e) {           System.out.println(e);        }    }}

Page 19: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Client Program

import java.io.*;import java.net.*; public class EchoClient {

public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try {

echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new

InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) {

System.err.println("Don't know about host: taranis."); System.exit(1);

} catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to:taranis."); System.exit(1);

} BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) {

out.println(userInput); System.out.println("echo: " + in.readLine());

} out.close(); in.close(); stdIn.close(); echoSocket.close();

} }

Page 20: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

TCP/IP and UDP/IP communications

Datagram communication: Connectionless

Stream communication: Connection oriented

UDP or TCP? UDP is unreliable Setup time for TCP

Page 21: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Sockets and Security

Means by which computers communicatePossible attacks

DOS Buffer overflow

Machine becomes a mule. Firewall

Blocks or restricts access to ports.

Page 22: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

Just scratching the surface!

Multiple clients

Multi-threaded server sockets

Proxies

Page 23: Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server

If you didn’t believe what I said, go here…(References)

http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html

http://www.ccs.neu.edu/home/kenb/com1335/socket_tut.html