Professional Documents
Culture Documents
Antony Java Network Programming (Ii) Networking Basics
Antony Java Network Programming (Ii) Networking Basics
programming (II)
A. Ngorora
• TCP is stream-based
• Stream-based: lossless communication
• We need IP address/domain name
• We need port number (> 1024)
Summary networking
basics 2
Network programming
• Ipconfig
• ping
Summary: Java
networking 6
Network programming
• Could wrap bytes to have higher level data
representation
• (int/double/String…. or objects)
• Examples:
• DataInputStream
• DataInputStream
• BufferedReader
• PrintWriter
Connections 8
Network programming
• Client and server on same computer
• Server and client on different computers
• Differentiate when creating Socket object:
• Localhost (same computer)
• host name (other)
Scenarios 9
Network programming
• BindException
• Attempting to create a server socket on a port
already in use
• UnknownHostException
• Host cannot be found
• ConnectionException
• Client program terminates
Important exceptions 10
Network programming
• Package: java.net
• Flush
• Clears the buffer for sending the data
through
InetAddress class 12
Network programming
• Use the inetAddress object to get the host
name/address
• Get host name
• inetAdress.getHostName()
• Get address
• inetAdress.getHostAddress()
Getting host
name/address 13
Network programming
• Multiple clients through threads
• Each client one thread
• while(true)
{
Socket socket = serverSocket.accept();
Thread thread = new ThreadClass (socket);
thread.start();
}
While loop 15
Network programming
• Objects must be Serializable
• Class should thus have implements
Serializable
• readObject()
• writeObject()
Summary 17
Network programming
• Listing 33.4…. Change to create separate class for the
thread…..
• HOMEWORK: Exercise 33.9, 33.10 (will be done in
tutorial classes)
Practical 18
Network programming