NguyenTanBaoLe Lab1c
NguyenTanBaoLe Lab1c
CO3094
COMPUTER NETWORK (LAB)
LAB 1
SOCKET PROGRAMMING IN JAVA:
CHAT APPLICATION
Table of contents
1. Socket programming in Java....................................................................................3
Exercise 1: Create a program that connects to a web server and downloads the
homepage of this website to a local computer........................................................... 3
2. Develop a simple chat application using client-server model................................4
Exercise 2: Design the user interface for the chat application...................................4
3. Multithread in Java................................................................................................... 6
Exercise 3: Using a multi-thread programming model to make the chat application
can talk to many different users concurrently............................................................ 6
2
Ho Chi Minh City University of Technology - HCMUT
try {
// Get the InetAddress of the website
InetAddress inetAddress = InetAddress.getByName(website);
3
Ho Chi Minh City University of Technology - HCMUT
are over
while ((line = in.readLine()) != null) {
if (contentStarted) {
// Skip lines that are purely numeric (e.g., "5314")
if (!line.matches("\\d+") && !line.isEmpty()) {
writer.write(line);
writer.newLine();
}
}
// Check for the empty line indicating the end of headers
if (line.isEmpty()) {
contentStarted = true;
}
}
} catch (UnknownHostException e) {
System.out.println("UnknownHostException: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
4
Ho Chi Minh City University of Technology - HCMUT
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public ChatApplicationUI() {
frame = new JFrame("Chat Application");
5
Ho Chi Minh City University of Technology - HCMUT
// Send button
sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Here, you can define the action for sending the message
String message = messageInputField.getText();
if (!message.isEmpty()) {
messageDisplayArea.append("You: " + message + "\n");
messageInputField.setText(""); // Clear input field after
sending
}
}
});
// Connection status
connectionStatus = new JLabel("Connected");
// Layout setup
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(inputPanel, BorderLayout.SOUTH);
panel.add(connectionStatus, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
3. Multithread in Java
Exercise 3: Using a multi-thread programming model to make the chat
application can talk to many different users concurrently.
The application includes two user interface: server and client.
First, we implement the server:
6
Ho Chi Minh City University of Technology - HCMUT
import java.io.*;
import java.net.*;
import java.util.*;
7
Ho Chi Minh City University of Technology - HCMUT
@Override
public void run() {
try {
// Set up input and output streams for the client
in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
out = new
PrintWriter(clientSocket.getOutputStream(), true);
String message;
while ((message = in.readLine()) != null) {
System.out.println("Received: " + message);
// Broadcast the message to all clients
ChatServer.broadcastMessage(message);
}
} catch (IOException e) {
System.out.println("Client disconnected: " +
e.getMessage());
} finally {
try {
clientSocket.close();
8
Ho Chi Minh City University of Technology - HCMUT
} catch (IOException e) {
System.out.println("Error closing the socket:
" + e.getMessage());
}
ChatServer.removeClient(this);
}
}
The server can create a port for clients joining in and sending messages into one
boxchat.
Next, we implement the client interface with the chat application user interface:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
9
Ho Chi Minh City University of Technology - HCMUT
messageDisplayArea.setLineWrap(true);
messageDisplayArea.setWrapStyleWord(true);
JScrollPane scrollPane = new
JScrollPane(messageDisplayArea);
// Send button
sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
// Connection status
connectionStatus = new JLabel("Not connected");
// Layout setup
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(inputPanel, BorderLayout.SOUTH);
panel.add(connectionStatus, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
10
Ho Chi Minh City University of Technology - HCMUT
connectionStatus.setText("Connected to " +
serverAddress);
11
Ho Chi Minh City University of Technology - HCMUT
messageDisplayArea.append(message + "\n");
}
} catch (IOException e) {
connectionStatus.setText("Connection lost: " +
e.getMessage());
}
}
}
In this way, the server handles multiple clients, and each client can send and receive
messages in real-time.
12