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
TCP | UDP
Transport Layer
Stream Dgram
Application Program
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
Send client text to server
Wait for server response
Echo server response to cmdwin for client
If user types only CR, exitelse
ServerCreate 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 createPrintWriter 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 classConstructor 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.
Socket ClassConstructor Summarypublic 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.
Pro-tecte
d
Socket(SocketImpl impl) Creates an unconnected Socket with a user-specified SocketImpl.
public Socket(String host, int port)
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 classConstructor Summary
publicDatagramSocket()
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.
publicDatagramSocket(int port, InetAddress laddr)
Creates a datagram socket, bound to the specified local address.
publicDatagramSocket(SocketAddress bindaddr)
Creates a datagram socket, bound to the specified local socket address.
Related classesConstructor 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.
InetSocketAddresspublic 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