Socket Classes - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Socket Classes

Description:

This class implements client sockets (also called just 'sockets' ... join a Multicast group and send the group salutations. String msg = 'Hello' ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 24
Provided by: Bar4162
Category:

less

Transcript and Presenter's Notes

Title: Socket Classes


1
Socket Classes
  • Socket
  • ServerSocket
  • DatagramSocket
  • MulticastSocket

2
java.net.Socket
  • public class Socket extends Object
  • This class implements client sockets (also called
    just "sockets").
  • A socket is an endpoint for communication between
    two machines.

3
Constructor Summary
  • Socket()
  • Socket(InetAddress address, int port)
  • Socket(InetAddress host, int port,
    boolean stream)
  • Socket(InetAddress address, int port,
    InetAddress localAddr, int localPort)
  • Socket(String host, int port)
  • Socket(String host, int port, boolean stream)
  • Socket(String host, int port, InetAddress localAdd
    r, int localPort)

4
Method Summary
  • void close()
  • InetAddress getInetAddress()
  • InetAddress getInputStream()
  • InetAddress getLocalAddress()
  • Int getLocalPort()
  • OutputStream getOutputStream()

5
Method Summary
  • Int getPort()
  • Int getReceiveBufferSize()
  • Int getSendBufferSize()
  • Void setReceiveBufferSize(int size)
  • Void setSendBufferSize(int size)

6
java.net.ServerSocket
  • public class ServerSocket extends Object
  • This class implements server sockets.
  • A server socket waits for requests to come in
    over the network
  • It performs some operation based on that request,
    and then possibly returns a result to the
    requester.

7
Constructor Summary
  • ServerSocket(int port)
  • ServerSocket(int port, int backlog)
  • ServerSocket(int port, int backlog,
    InetAddress bindAddr)

8
Method Summary
  • Socket accept()
  • Void close()
  • InetAddress getInetAddress()
  • Int getLocalPort()
  • Int getSoTimeout()
  • Void setSoTimeout(int timeout)
  • String toString()

9
java.net.DatagramSocket
  • public class DatagramSocket extends Object
  • A datagram socket is the sending or receiving
    point for a packet delivery service
  • Each packet sent or received on a datagram socket
    is individually addressed and routed

10
Constructor Summary
  • DatagramSocket()
  • DatagramSocket(int port)
  • DatagramSocket(int port, InetAddress laddr)

11
java.net.MulticastSocket
  • public class MulticastSocket extends
    DatagramSocket
  • The multicast datagram socket class is useful for
    sending and receiving IP multicast packets
  • A MulticastSocket is a (UDP) DatagramSocket, with
    additional capabilities for joining "groups" of
    other multicast hosts on the internet.

12
MulticastSocket
  • One would join a multicast group by first
    creating a MulticastSocket with the desired port,
    then invoking the joinGroup(InetAddress
    groupAddr) method
  • // join a Multicast group and send the group
    salutations
  • ...
  • String msg "Hello"
  • InetAddress group InetAddress.getByName("228.5.6
    .7")
  • MulticastSocket s new MulticastSocket(6789)
  • s.joinGroup(group)
  • DatagramPacket hi new DatagramPacket(msg.getByte
    s(), msg.length(), group, 6789)
  • s.send(hi)
  • // get their responses!
  • byte buf new byte1000
  • DatagramPacket recv new DatagramPacket(buf,
    buf.length)
  • s.receive(recv)
  • ...
  • // OK, I'm done talking - leave the group...
  • s.leaveGroup(group)

DatagramPacket(byte buf, int length,
InetAddress address, int port)           Construc
ts a datagram packet for sending packets of
length length to the specified port number on the
specified host buf - the packet data. length -
the packet length. address - the destination
address. port - the destination
port number.
13
Constructor Summary
  • MulticastSocket()
  • Create a multicast socket.
  • MulticastSocket(int port) Create a multicast
    socket and bind it to a specific port.

14
Reading from and Writing to a Socket
  • NwSocket new Socket(machine",1200)
  • in new BufferedReader(new InputStreamReader (
    NwSocket.getInputStream()))
  • out new PrintWriter(NwSocket.getOutputStream(),
    true)

15
java.io.BufferedReader
  • Read text from a character-input stream,
    buffering characters so as to provide for the
    efficient reading of characters, arrays, and
    lines.
  • CONSTRUCTORS
  • BufferedReader(Reader in)
  • Create a buffering character-input stream
    that uses a default-sized input buffer.
  • BufferedReader(Reader in, int sz)
  • Create a buffering character-input stream that
    uses an input buffer of the specified size.

16
public class BufferedReader extends Reader Read
text from a character-input stream
  • in new BufferedReader(new InputStreamReader (
    NwSocket.getInputStream()))

public class InputStreamReader extends Reader An
InputStreamReader is a bridge from byte streams
to character streams It reads bytes and
translates them into characters according to a
specified character encoding.
public InputStream getInputStream() throws
IOException Returns an input stream for this
socket. IOException - if an I/O error occurs
when creating the output stream.
17
java.io.PrintWriter
  • Print formatted representations of objects to a
    text-output stream.
  • CONSTRUCTORS
  • PrintWriter(OutputStream out) Create a new
    PrintWriter, without automatic line flushing,
    from an existing OutputStream.
  • PrintWriter(OutputStream out, boolean autoFlush)
    Create a new PrintWriter from an existing
    OutputStream.

18
  • out new PrintWriter(NwSocket.getOutputStream(),
    true)

public PrintWriter(OutputStream out,
boolean autoFlush) Create a new PrintWriter from
an existing OutputStream. This convenience
constructor creates the necessary intermediate
OutputStreamWriter, which will convert characters
into bytes using the default character encoding.
Parameters out - An output stream autoFlush -
A boolean if true, the println() methods will
flush the output buffer
public OutputStream getOutputStream() throws
IOException Returns an output stream for this
socket. IOException - if an I/O error occurs when
creating the output stream.
19
Client-Server Example- Server Program
20
(No Transcript)
21
(No Transcript)
22
Client-Server Example- Client Program
  • The client program establishes a connection to
    the server program on a particular host and port
    number in its listenSocket method
  • sends the data entered by the end user to the
    server program in its actionPerformed method.
  • The actionPerformed method also receives the data
    back from the server and prints it to the command
    line.

23
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com