socketProgramming
Download
Report
Transcript socketProgramming
ITEC 350
Homework 2
Socket Programming
API available to programs
• Application
Programming
Interface (API) to
TCP & UDP
– “Sockets”
– Two Modes:
• Datagram (DGRAM)
• Stream (INET)
– A socket is defined by
an IP address and a
port number
Application Program
Stream
TCP
Dgram
|
UDP
Transport Layer
Socket Programming – HW02
• Writing programs in Java can give students
great insight into sockets, IP addresses, ports,
etc.
• If you have had ITEC 120/220
– Then you know all about java stream io
(BufferedReader, BufferedWriter)
– All you need is java.net package
• If you have _not_ had ITEC 120/220
– you may use a programming language like C, C++, etc.
– Or, if you have had no programming language that
supports sockets, come see me in office hours.
java.net
• The “real” .net … not that other M$.NET
• Packages: what Java calls collections of classes
• .net is a package – containing classes:
– Socket, ServerSocket for TCP streams
– DatagramSocket for UDP datagrams
• We will implement the following flowcharts
Client
Open a connection on port 4444
Wait for user to type something
If user types only CR, exit
else
Send client text to server
Wait for server response
Echo server response to
cmdwin for client
Server
Create a ServerSocket object for port 4444
Using ServerSocket.accept,
wait for a client to open a connection
When a client opens a connection,
the accept method has returned a copy
of the client socket. Create a
BufferedReader for this object.
Using getOutputStream() method create
PrintWriter object.
Wait for client to type something
Echo client text to cmdwin for server.
Accept server response from keyboard
Send server text over socket to client
ServerSocket class
Constructor Summary
ServerSocket()
Creates an unbound server socket.
ServerSocket(int port)
Creates a server socket on a specified port.
ServerSocket(int port, int backlog)
Creates a server socket and binds it to the specified local port number,
with the specified backlog.
ServerSocket(int port, int backlog, InetAddress bindAddr)
Create a server with the specified port, listen backlog, and local IP
address to bind to.
Constructor Summary
public
Socket Class
Socket()
Creates an unconnected socket, with the system-default type of SocketImpl.
public
Socket(InetAddress address, int port)
Creates a stream socket and connects it to the specified port number at the
specified IP address.
public
Socket(InetAddress host, int port, boolean stream)
Deprecated. Use DatagramSocket instead for UDP transport.
public
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.
Protected
Socket(SocketImpl impl)
public
Socket(String host, int port)
Creates an unconnected Socket with a user-specified SocketImpl.
Creates a stream socket and connects it to the specified port number on the
named host.
public
Socket(String host, int port, boolean stream)
Deprecated. Use DatagramSocket instead for UDP transport.
public
Socket(String host, int port, InetAddress localAddr, int localPort)
Creates a socket and connects it to the specified remote host on the specified
remote port.
DatagramSocket class
Constructor Summary
public
DatagramSocket()
Constructs a datagram socket and binds it to any available port
on the local host machine.
protected
DatagramSocket(DatagramSocketImpl impl)
Creates an unbound datagram socket with the specified
DatagramSocketImpl.
public
DatagramSocket(int port)
Constructs a datagram socket and binds it to the specified port
on the local host machine.
public
DatagramSocket(int port, InetAddress laddr)
Creates a datagram socket, bound to the specified local address.
public
DatagramSocket(SocketAddress bindaddr)
Creates a datagram socket, bound to the specified local socket
address.
Related classes
Constructor Summary – InetSocketAddress (subclass of SocketAddress)
InetSocketAddress(InetAddress addr, int port)
Creates a socket address from an IP address and a port number.
InetSocketAddress(int port)
Creates a socket address where the IP address is the wildcard
address and the port number a specified value.
InetSocketAddress(String hostname, int port)
Creates a socket address from a hostname and a port number.
InetSocketAddress
public InetSocketAddress(int port)
Creates a socket address where the IP address is the wildcard
address and the port number a specified value. A valid port value is
between 0 and 65535. A port number of zero will let the system pick
up an ephemeral port in a bind operation.
Note
• this is *NOT* the standard Echo server
that is widely found on the web.
• You must have echo going in both
directions.
• Java Programming, but as a last resort C
or C++
• If you only know C, C++
– Let me know via email
Submission
• Save your source codes and a
ReadMe.doc file to RU03 folder of your
dropbox
• The ReadMe.doc file must include:
– Your name
– File names of your source codes and
descriptions of each files.
– Executing Instruction
– Length: Less than 3 pages