The Application Programming Interface API and the Socketprogramming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

The Application Programming Interface API and the Socketprogramming

Description:

PrintWriter pout = new. PrintWriter(client.getOutputStream(),true); pout.println('Here is data sent to client!'); client.close(); client: ... – PowerPoint PPT presentation

Number of Views:937
Avg rating:3.0/5.0
Slides: 17
Provided by: nimrud
Category:

less

Transcript and Presenter's Notes

Title: The Application Programming Interface API and the Socketprogramming


1
TheApplication Programming Interface (API) and
the Socket-programming
2
Application Program Interface (API)
3
SMB DCOM
  • Server Message Block
  • Server Message Block (SMB) is an
    application-level network protocol mainly applied
    to shared access to files, printers, serial
    ports, and miscellaneous communications between
    nodes on a network. It also provides an
    authenticated Inter-process communication
    mechanism. It is mainly used by Microsoft Windows
    equipped computers, where it's known simply as
    "Microsoft Windows Network".
  • Distributed Component Object Model
  • Distributed Component Object Model (DCOM) is a
    Microsoft proprietary technology for software
    components distributed across several networked
    computers to communicate with each other. DCOM,
    which originally was called "Network OLE",
    extends Microsoft's COM, and provides the
    communication substrate under Microsoft's COM
    application server infrastructure. It has been
    deprecated in favor of Microsoft .NET.

4
Socket-programming
  • Socket a door between application process and
    end-end-transport protocol (UDP or TCP)
  • TCP service reliable transfer of bytes from one
    process to another

controlled by application developer
controlled by application developer
controlled by operating system
controlled by operating system
internet
host or server
host or server
5
Sockets
  • An abstraction of a network I/O queue
  • Embodies one side of a communication channel
  • Same interface regardless of location of other
    end
  • Could be local machine (called UNIX socket) or
    remote machine (called network socket)
  • First introduced in 4.2 BSD UNIX big innovation
    at time
  • Now most operating systems provide some notion of
    socket
  • Using Sockets for Client-Server (C/C
    interface)
  • On server set up server-socket
  • Create socket, Bind to protocol (TCP), local
    address, port
  • Call listen() tells server socket to accept
    incoming requests
  • Perform multiple accept() calls on socket to
    accept incoming connection request
  • Each successful accept() returns a new socket for
    a new connection can pass this off to handler
    thread
  • On client
  • Create socket, Bind to protocol (e.g. TCP),
    remote address, port
  • Perform connect() on socket to make connection
  • If connect() successful, have socket connected to
    server

6
RAW SOCKET
  • Definition
  • Raw socket is a computer networking term used to
    describe a socket that allows access to packet
    headers on incoming and outgoing packets. Raw
    sockets are usually used at the transport or
    network layers.
  • Usually raw sockets always receive packets with
    the header included (as opposed to non-raw
    sockets, which strip the header ). Whether or not
    a header is automatically prepended to outgoing
    packets is usually a configurable socket option.
  • Raw sockets are not a programming language-level
    construct, they are part of the underlying
    operating system's networking API.

7
RAW SOCKET cont.
  • Work
  • Raw sockets bypass the transport layer (TCP or
    UDP). With IPv4, raw sockets are used to
    access ICMPv4, IGMPv4, and to read and write
    IPv4 datagrams containing a protocol field that
    the kernel does not process. With IPv6 raw
    sockets will be used for ICMPv6 and to read and
    write IPv6 datagrams containing a Next
    Header field that the kernel does not process.
    All data sent via raw sockets must be in network
    byte order and all data received via raw
    sockets will be in network byte order. This
    differs from the IPv4 raw sockets, which did not
    specify a byte ordering and used the host's
    byte order for certain IP header fields.

8
RAW SOCKET cont.
  • Problem
  • Due to the fact that raw sockets allow users to
    make packet headers themselves, their power can
    be abused to perform feats such as IP address
    spoofing
  • When Windows XP was first released in 2001 with
    raw socket support implemented into the Winsock
    interface, the media attacked Microsoft saying
    that raw sockets are only of use to hackers to
    pull off TCP reset attacks.

9
RAW SOCKET cont.
  • If a hacker had the ability to forge a packet's
    information, he or she could create a SYN packet
    with a fake, or spoofed, return address. In this
    case, the host computer would receive the SYN
    packet from the client computer (hacker), read
    the return address, and send the SYN-ACK packet
    to a fake return address. If there were no
    computer at the spoofed address, the host
    computer would sit and wait for several minutes
    before realizing that no one was connecting to
    it. However, during that time, the host computer
    would have a port open, waiting for a returning
    ACK. Because there are only so many ports
    available for connecting client computers, a
    hacker could quickly use up all of the host
    computer's resources

10
Socket Example (Java)
  • server //Makes socket, binds addr/port,
    calls listen() ServerSocket sock new
    ServerSocket(6013) while(true) Socket
    client sock.accept() PrintWriter pout
    new PrintWriter(client.getOutputStream(),true
    ) pout.println(Here is data sent to
    client!) client.close()
  • client // Makes socket, binds addr/port,
    calls connect() Socket sock new
    Socket(169.229.60.38,6013) BufferedReader
    bin new BufferedReader( new
    InputStreamReader(sock.getInputStream)) String
    line while ((line bin.readLine())!null)
    System.out.println(line) sock.close()

11
Another example Java client (TCP)
import java.io. import java.net. class
TCPClient public static void main(String
argv) throws Exception String
sentence String modifiedSentence
BufferedReader inFromUser new
BufferedReader(new InputStreamReader(System.in))
Socket clientSocket new
Socket("hostname", 6789)
DataOutputStream outToServer new
DataOutputStream(clientSocket.getOutputStream())

Create input stream
Create client socket, connect to server
Create output stream attached to socket
Study guide be able to write a
simple Client-server program in Java.
12
Another example Java server (TCP)
import java.io. import java.net. class
TCPServer public static void main(String
argv) throws Exception String
clientSentence String capitalizedSentence
ServerSocket welcomeSocket new
ServerSocket(6789) while(true)
Socket connectionSocket
welcomeSocket.accept()
BufferedReader inFromClient new
BufferedReader(new
InputStreamReader(connectionSocket.getInputStream(
)))
Create welcoming socket at port 6789
Wait, on welcoming socket for contact by client
Create input stream, attached to socket
13
Distributed Applications
  • How do you actually program a distributed
    application?
  • Need to synchronize multiple threads, running on
    different machines
  • No shared memory, so cannot use testset
  • One Abstraction send/receive messages
  • Already atomic no receiver gets portion of a
    message and two receivers cannot get same message
  • Interface
  • Mailbox (mbox) temporary holding area for
    messages
  • Includes both destination location and queue
  • Send(message,mbox)
  • Send message to remote mailbox identified by mbox
  • Receive(buffer,mbox)
  • Wait until mbox has message, copy into buffer,
    and return

14
Using Messages Send/Receive behavior
  • When should send(message,mbox) return?
  • When receiver gets message? (i.e. ack received)
  • When message is safely buffered on destination?
  • Right away, if message is buffered on source
    node?
  • Actually two questions here
  • When can the sender be sure that the receiver
    actually received the message?
  • When can sender reuse the memory containing
    message?
  • Mailbox provides 1-way communication from T1?T2
  • T1?buffer?T2
  • Very similar to producer/consumer
  • Send V, Receive P
  • However, cant tell if sender/receiver is local
    or not!

15
Example Java client (UDP)
import java.io. import java.net. class
UDPClient public static void main(String
args) throws Exception
BufferedReader inFromUser new
BufferedReader(new InputStreamReader(System.in))
DatagramSocket clientSocket new
DatagramSocket() InetAddress IPAddress
InetAddress.getByName("hostname")
byte sendData new byte1024 byte
receiveData new byte1024 String
sentence inFromUser.readLine() sendData
sentence.getBytes()
Create input stream
Create client socket
Translate hostname to IP address using DNS
16
Example Java server (UDP)
import java.io. import java.net. class
UDPServer public static void main(String
args) throws Exception
DatagramSocket serverSocket new
DatagramSocket(9876) byte
receiveData new byte1024 byte
sendData new byte1024 while(true)
DatagramPacket
receivePacket new
DatagramPacket(receiveData, receiveData.length)
serverSocket.receive(receivePacket)
Create datagram socket at port 9876
Create space for received datagram
Receive datagram
Write a Comment
User Comments (0)
About PowerShow.com