Lab - 1c - Socket Programming in Java
Lab - 1c - Socket Programming in Java
Computer Networks 1
Lab 4
Socket Programming in Java: Chat Application
Names: ....................................................................
Student No.: .................... ……………………………
I. Objectives
1. Practice with Socket programming in Java
2. Build a simple chat application using client-server model
3. Multithreaded application in Java
II. Content
1. Socket programming in Java
Consider the following code:
try {
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1");
// Get hostname by a byte array containing the IP address
byte[] ipAddr = new byte[]{127, 0, 0, 1};
addr = InetAddress.getByAddress(ipAddr);
// Get the host name
String hostname = addr.getHostName();
// Get canonical host name
String hostnameCanonical = addr.getCanonicalHostName();
}
catch (UnknownHostException e) {
}
Exercise 1:
Create a program that connects to a web server and downloads the homepage of this
website to local computer.
3. Multithread in Java
8. . . .
9. }
10. }
11. PrimeRun p = new PrimeRun(143);
12. new Thread(p).start();
Stop a thread:
Try to use Thread.interrupt() as in the following code and see what happens.
Exercise 3:
Using multithread programming model to make the chat application can talk to many
different users concurrently.