Transmission Control Protocol / Internet Protocol Based on Notes By

Embed Size (px)

Citation preview

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    1/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP/IPTCP/IPTransmission Control Protocol / InternetTransmission Control Protocol / Internet

    ProtocolProtocol

    Based on Notes by D. Hollinger

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    2/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TopicsTopics

    IPv6

    TCP

    Java TCP Programming

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    3/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 availabilityIPv6 availability

    Generally available with (new) versions

    of most operating systems. BSD, Linux 2.2 Solaris 8

    An option with Windows 2000/NT Most routers can support IPV6 Supported in J2SDK/JRE 1.4

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    4/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 Design IssuesIPv6 Design Issues

    Overcome IPv4 scaling problem lack of address space.

    Flexible transition mechanism. New routing capabilities.

    Quality of service.

    Security. Ability to add features in the future.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    5/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 HeadersIPv6 Headers

    Simpler header - faster processing byrouters.

    No optional fields - fixed size (40 bytes) No fragmentation fields. No checksum

    Support for multiple headers more flexible than simple protocol field.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    6/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv4 HeaderIPv4 Header

    VERS HL

    Fragment Offset

    Fragment LengthService

    Datagram ID FLAG

    TTL Protocol Header ChecksumSource Address

    Destination Address

    Options (if any)

    Data

    1 byte1 byte 1 byte 1 byte

    4 for IPv4

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    7/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 HeaderIPv6 Header

    VERS PRIO

    Hop Limit

    Flow Label

    Payload Length Next Header

    1 byte1 byte 1 byte 1 byte

    6 for IPv6

    Source Address (128 bits - 16 bytes)

    Dest. Address (128 bits - 16 bytes)

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    8/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 Header FieldsIPv6 Header Fields

    VERS: 6 (IP version number) Priority: will be used in congestion control

    Flow Label: experimental - sender can label asequence of packets as being in the sameflow.

    Payload Length: number of bytes in

    everything following the 40 byte header (up to

    64Kb), or 0 for a Jumbogram (up to 4Gb).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    9/67

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 Header FieldsIPv6 Header Fields

    Next Header is similar to the IPv4protocol field - indicates what type of

    header follows the IPv6 header.

    Hop Limit is similar to the IPv4 TTL field(but now it really means hops, not time).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    10/671

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Extension HeadersExtension Headers

    Routing Header - source routing Fragmentation Header - supports

    fragmentation of IPv6 datagrams.

    Authentication Header

    Encapsulating Security Payload Header

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    11/671

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 AddressesIPv6 Addresses

    128 bits - written as eight 16-bit hexnumbers.

    5f1b:df00:ce3e:e200:0020:0800:2078:e3e3

    High order bits determine the type of

    address.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    12/671

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6IPv6

    Aggregate Global UnicastAggregate Global Unicast

    AddressAddress

    010 TLA ID NLA ID SLA ID Interface ID

    3 13 32 16 64

    TLA: top-level aggregation (provider)

    NLA: next-level (subscriber)

    SLA: site-level (subnet)

    Interface ID is (typically) based on hardware

    MAC address

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    13/671

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv4-Mapped IPv6 AddressIPv4-Mapped IPv6 Address

    IPv4-Mapped addresses allow a host thatsupport both IPv4 and IPv6 to communicatewith a host that supports only IPv4.

    The IPv6 address is based completely onthe IPv4 address.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    14/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv4-Mapped IPv6 AddressIPv4-Mapped IPv6 Address

    80 bits of 0s followed by 16 bits of ones,followed by a 32 bit IPv4 Address:

    0000 . . . 0000 IPv4 AddressFFFF

    80 bits 32 bits16 bits

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    15/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Works with DNSWorks with DNS

    An IPv6 application asks DNS for theaddress of a host, but the host only has

    an IPv4 address. DNS creates the IPv4-Mapped IPv6

    address automatically.

    Kernel understands this is a specialaddress and really uses IPv4communication.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    16/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv4-Compatible IPv6IPv4-Compatible IPv6

    AddressAddress An IPv4 compatible address allows a

    host supporting IPv6 to talk IPv6 even if

    the local router(s) dont talk IPv6. IPv4 compatible addresses tell endpoint

    software to create a tunnel by

    encapsulating the IPv6 packet in anIPv4 packet.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    17/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv4-Compatible

    IPv6 Address

    0000 . . . 0000 IPv4 Address0000

    80 bits 32 bits16 bits

    80 bits of 0s followed by 16 bits of 0s,

    followed by a 32 bit IPv4 Address:

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    18/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TunnelingTunneling

    (done automatically by kernel when(done automatically by kernel when

    IPv4-Compatible IPv6 addresses used)IPv4-Compatible IPv6 addresses used)IPv6

    Host

    IPv6

    Host

    IPv4 Routers

    IPv6 Datagram

    IPv4 Datagram

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    19/67

    1Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    IPv6 in Java 1.4IPv6 in Java 1.4

    Inet6Address class :: corresponds to 0.0.0.0 (unspecified) in IPv4

    ::1 corresponds to 127.0.0.1 (loopback) in IPv4

    ::ffff:w.x.y.z IPv4-mapped address ::w.x.y.z to tunnel IPv6 packets over IPv4 routing

    For details, see:http://java.sun.com/j2se/1.4/docs/guide/net/ipv6_guide/

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    20/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCPTCP

    Transmission Control ProtocolTransmission Control Protocol TCP is an alternative transport layer

    protocol over IP.

    TCP provides:Connection-oriented

    Reliable

    Full-duplexByte-Stream

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    21/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Connection-OrientedConnection-Oriented

    Connection orientedmeans thata virtual connection is establishedbefore any user data istransferred.

    If the connection cannot beestablished - the user program isnotified.

    If the connection is everinterrupted - the user program(s)is notified.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    22/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    ReliableReliable

    Reliable means that everytransmission of data is

    acknowledged by the receiver. If the sender does not receive

    acknowledgement within a

    specified amount of time, thesender retransmits the data.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    23/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Byte StreamByte Stream

    Stream means that theconnection is treated as a stream

    of bytes.

    The user application does notneed to package data in individualdatagrams (as with UDP).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    24/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    BufferingBuffering

    TCP is responsible for buffering dataand determining when it is time to

    send a datagram.

    It is possible for an application to tell

    TCP to send the data it has buffered

    without waiting for a buffer to fill up.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    25/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Full DuplexFull Duplex

    TCP provides transfer in both directions.

    To the application program these appearas 2 unrelated data streams, althoughTCP can piggyback control and data

    communication by providing controlinformation (such as an ACK) along withuser data.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    26/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP PortsTCP Ports

    Interprocess communication viaTCP is achieved with the use of

    ports (just like UDP).

    UDP ports have no relation to TCPports (different name spaces).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    27/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP SegmentsTCP Segments

    The chunk of data that TCP asks IPto deliver is called a TCP segment.

    Each segment contains:

    data bytes from the byte stream control information that identifies thedata bytes

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    28/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Segment FormatTCP Segment Format

    Destination Port

    Options (if any)

    Data

    1 byte 1 byte

    Source Port

    Sequence Number

    Acknowledgment Number

    1 byte 1 byte

    offset Reser. Control Window

    Checksum Urgent Pointer

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    29/67

    2Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP LingoTCP Lingo

    When a client requests a connection itsends a SYN segment (a special TCP

    segment) to the server port. SYN stands for synchronize. The SYN

    message includes the clients ISN.

    ISN is Initial Sequence Number.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    30/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    More...More...

    Every TCP segment includes aSequence Numberthat refers to the firstbyte ofdata included in the segment.

    Every TCP segment includes anAcknowledgement Numberthatindicates the byte number of the next

    data that is expected to be received. All bytes up through this number havealready been received.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    31/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    And more...And more...

    There are a bunch of control flags: URG: urgent data included.

    ACK: this segment is (among other things)an acknowledgement. RST: error connection must be reset.

    SYN: synchronize Sequence Numbers(setup) FIN: polite connection termination.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    32/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    And more...And more...

    MSS: Maximum segment size (A TCPoption)

    Window: Every ACK includes a Windowfield that tells the sender how many bytesit can send before the receiver will have to

    toss it away (due to fixed buffer size).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    33/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Connection CreationTCP Connection Creation

    Programming details later - for now we areconcerned with the actual communication.

    A serveraccepts a connection. Must be looking for new connections! A clientrequests a connection.

    Must knowwhere the server is!

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    34/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Client StartsClient Starts

    A client starts by sending a SYNsegment with the following information:

    Clients ISN (generated pseudo-randomly) Maximum Receive Window for client. Optionally (but usually) MSS (largest

    datagram accepted). No payload! (Only TCP headers)

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    35/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Server ResponseServer Response

    When a waiting server sees a newconnection request, the server sendsback a SYN segment with:

    Servers ISN (generated pseudo-randomly) Request Number is Client ISN+1 Maximum Receive Window for server.

    Optionally (but usually) MSS

    No payload! (Only TCP headers)

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    36/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    FinallyFinally

    When the Servers SYN is received, theclient sends back an ACK with:

    Acknowledgment Number is ServersISN+1

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    37/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    SYNISN=X

    SYN

    ISN=X

    Client Server

    SYNISN=YACK=X+1

    SYN

    ISN=YACK=X+1

    ACK=Y+1ACK=Y+1

    1

    2

    3

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    38/67

    3Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP 3-way handshakeTCP 3-way handshake

    Client: I want to talk, and Im starting with bytenumberX.

    Server: OK, Im here and Ill talk. My first bytewill be called numberY, and I know your firstbyte will be numberX+1.

    Client: Got it - you start at byte numberY+1.

    Bill: Monica, Im afraid Ill syn and byte yourack

    1

    2

    3

    ?

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    39/67

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    40/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Data and ACKTCP Data and ACK

    Once the connection is established,data can be sent.

    Each data segment includes asequence number identifying the firstbyte in the segment.

    Each segment (data or empty) includesa request number indicating what datahas been received.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    41/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    BufferingBuffering

    Keep in mind that TCP is part of theOperating System. The O.S. takes care

    of all these details asynchronously. The TCP layer doesnt know when the

    application will ask for any received

    data. TCP buffers incoming data so its ready

    when we ask for it.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    42/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP BuffersTCP Buffers

    Both the client and server allocatebuffers to hold incoming and outgoing

    data The TCP layer does this.

    Both the client and server announcewith every ACK how much buffer spaceremains (the Window field in a TCP

    segment).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    43/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Send BuffersSend Buffers

    The application gives the TCP layersome data to send.

    The data is put in a send buffer, whereit stays until the data is ACKd. The TCP layer wont accept data from

    the application unless (or until) there isbuffer space.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    44/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    ACKsACKs

    A receiver doesnt have to ACK everysegment (it can ACK many segments

    with a single ACK segment). Each ACK can also contain outgoing

    data (piggybacking).

    If a sender doesnt get an ACK aftersome time limit, it resends the data.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    45/67

    4Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Segment OrderTCP Segment Order

    Most TCP implementations will acceptout-of-order segments (if there is roomin the buffer).

    Once the missing segments arrive, asingle ACK can be sent for the wholething.

    Remember: IP delivers TCP segments,and IP is not reliable - IP datagrams canbe lost or arrive out of order.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    46/67

    4

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TerminationTermination

    The TCP layer can send a RSTsegment that terminates a connection if

    something is wrong. Usually the application tells TCP to

    terminate the connection politely with a

    FIN segment.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    47/67

    4

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    FINFIN

    Either end of the connection can initiatetermination.

    A FIN is sent, which means theapplication is done sending data. The FIN is ACKd.

    The other end must now send a FIN. That FIN must be ACKd.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    48/67

    4

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    FIN

    SN=X

    FIN

    SN=X

    App1 App2

    ACK=X+1ACK=X+1

    ACK=Y+1ACK=Y+1

    1

    2

    4

    FIN

    SN=Y

    FIN

    SN=Y

    3

    .

    .

    .

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    49/67

    4

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Termination

    1

    2

    3

    4

    App1: I have no more data for you.

    App2: OK, I understand you are done sending.

    dramatic pause

    App2: OK - Now Im also done sending data.

    App1: Roger, Over and Out, Goodbye,

    Hastalavista Baby, Adios, Its been real ...

    camera fades to black ...

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    50/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP TIME_WAITTCP TIME_WAIT

    Once a TCP connection has been terminated(the last ACK sent) there is some unfinishedbusiness:

    What if the ACK is lost? The last FIN will be resentand it must be ACKd.

    What if there are lost or duplicated segments thatfinally reach the destination after a long delay?

    TCP hangs out for a while to handle these

    situations.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    51/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    TCP Sockets ProgrammingTCP Sockets Programming

    Creating apassive mode (server)socket.

    Establishing an application-levelconnection. Sending/receiving data.

    Terminating a connection.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    52/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Establishing a passive modeEstablishing a passive mode

    TCP socketTCP socketPassive mode:

    Address already determined.

    Tell the kernel to accept incoming connection requestsdirected at the socket address.

    3-way handshake

    Tell the kernel to queue incoming connections for us.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    53/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Accepting an incomingAccepting an incoming

    connection.connection. Once we start listening on a socket, the

    O.S. will queue incoming connections

    Handles the 3-way handshake Queues up multiple connections.

    When our application is ready to handle anew connection, we need to ask the O.S.for the next connection.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    54/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Terminating a TCP connectionTerminating a TCP connection

    Either end of the connection can callthe close() system call.

    If the other end has closed theconnection, and there is no buffereddata, reading from a TCP socket returns

    0 to indicate EOF.

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    55/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Client CodeClient Code

    TCP clients can connect to a server,which:

    takes care of establishing an endpointaddress for the client socket.

    dont need to call bind first, the O.S. will takecare of assigning the local endpoint address(TCP port number, IP address).

    Attempts to establish a connection to thespecified server.3-way handshake

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    56/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Reading from a TCP socketReading from a TCP socket

    By default read() will block until data isavailable. Reading from a TCP socket may return

    less than max bytes (whatever isavailable).

    You must be prepared to read data 1byte at a time!

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    57/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Writing to a TCP socketWriting to a TCP socket

    write might not be able to write all bytes(on a nonblocking socket).

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    58/67

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    59/67

    5

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Java Sockets ProgrammingJava Sockets Programming

    The package java.net provides supportfor sockets programming

    Typically you import everything definedin this package with:

    import java.net.*;

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    60/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    ClassesClasses

    InetAddress

    Socket

    ServerSocket

    DatagramSocket

    DatagramPacketUDPUDP

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    61/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Socket classSocket class

    Corresponds to active TCP sockets only! client sockets

    socket returned by accept();

    Passive sockets are supported by a

    different class: ServerSocket

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    62/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Socket ConstructorsSocket Constructors

    Constructor creates a TCP connectionto a named TCP server.

    There are a number of constructors:Socket(InetAddress server, int port);

    Socket(InetAddress server, int port,

    InetAddress local, int localport);

    Socket(String hostname, int port);

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    63/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Socket MethodsSocket Methods

    void close();

    InetAddress getInetAddress();getpeername

    InetAddress getLocalAddress(); getsockname

    InputStream getInputStream();

    OutputStream getOutputStream();

    Lots more (setting/gettting socket options,partial close, etc.)

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    64/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    Socket I/OSocket I/O

    Socket I/O is based on the Java I/Osupport (in the package java.io).

    InputStream and OutputStream areabstract classes

    common operations defined for all kinds ofInputStreams, OutputStreams

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    65/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    ServerSocket ClassServerSocket Class

    (TCP Passive Socket)(TCP Passive Socket)Constructors:

    ServerSocket(int port);

    ServerSocket(int port, int backlog);

    ServerSocket(int port, int backlog,

    InetAddress bindAddr);

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    66/67

    6

    Netprog 2002 TCP/IPNetprog 2002 TCP/IP

    ServerSocket MethodsServerSocket Methods

    Socket accept();

    void close();

    InetAddress getInetAddress();

    int getLocalPort();

    throw IOException, SecurityException

  • 8/14/2019 Transmission Control Protocol / Internet Protocol Based on Notes By

    67/67

    Sample Echo ServerSample Echo Server

    TCPEchoServer.java, EchoClient.java,GenericClient.java

    Simple TCP Echo server.

    Based on code from:

    TCP/IP Sockets in Java, Java OnlineTutorial