0% found this document useful (0 votes)
40 views4 pages

Lab - 1c - Socket Programming in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Lab - 1c - Socket Programming in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Faculty of Computer Science and Engineering – HCMC University of Technology

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.

Ex.: You can test your program with: www.google.com


Faculty of Computer Science and Engineering – HCMC University of Technology

2. Develop a simple chat application using client-server model

Server application Client application


Create a server socket
Listen and wait for incoming connection Identify server IP and port
request
Create a TCP socket to the server
Establish a connection to the server
Accept the connection request
Send/Receive data Send/Receive data
Close connection
Close connection

Exercise 2: Design the user interface for the chat application

3. Multithread in Java

Create an application that has multiple threads running concurrently:

1. class PrimeRun implements Runnable {


2. long minPrime;
3. PrimeRun ( long minPrime ) {
4. this.minPrime = minPrime;
5. }
6. public void run() {
7. // compute primes larger than minPrime
Faculty of Computer Science and Engineering – HCMC University of Technology

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.

public void stop() {


running = false;
this.interrupt();
}
public void run() {
running = true;
while (running){
Socket s = socket.accept();
...
}
}

public void stop() {


running = false;
this.interrupt();
}
public void run() {
running = true;
while (running){
Socket s = socket.accept();
...
}
}

Multithread in Server Application:


Faculty of Computer Science and Engineering – HCMC University of Technology

Multithread in Client Application:

Exercise 3:

Using multithread programming model to make the chat application can talk to many
different users concurrently.

You might also like