Rest of Java
Rest of Java
new ServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
String text;
while ((text = reader.readLine()) != null) {
System.out.println("Received from client: " + text);
writer.println("Echo: " + text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCP/IP Client Socket
A TCP/IP client socket connects to a server socket to communicate with the server.
Steps to create a simple TCP/IP client socket in Java:
1. Create a Socket object to connect to the server on a specific IP address and
port.
2. Communicate with the server using input and output streams.
Example Code:
import java.io.*;
import java.net.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
});
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
3. Delegation Event Model
The delegation event model in Java involves three components: event source
(generates events), event object (describes events), and event listener (handles
events). The process: the event source generates an event, creates an event object,
and sends it to the event listener, which then processes the event.
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
4. Explain panel, frame, canvas, windows and container.
ANS-
Panel
A lightweight container used to group other components within a window.
Often used for organizing components in an applet or frame.
Panel panel = new Panel();
panel.add(new Button("Click Me"));
Frame
Description: A top-level window with a title and border, capable of containing
other components.
Usage: Commonly used as the main window for a standalone application.
Frame frame = new Frame("My Frame");
frame.setSize(300, 300);
frame.setVisible(true);
Canvas
Description: A blank rectangular area where custom drawing can be done and
images can be displayed.
Usage: Useful for creating custom graphics or for drawing shapes.
Canvas canvas = new Canvas() {
public void paint(Graphics g) {
g.drawString("Hello, Canvas!", 20, 20);
}
};
Window
Description: A top-level window without borders or menubar, usually used as a
pop-up or secondary window.
Usage: Can serve as a dialog or an additional window to the main application
frame.
Window window = new Window(frame);
window.setSize(200, 200);
window.setVisible(true);
Container
Description: A generic component that can hold other AWT components.
Usage: Acts as a base class for components like Panel and Frame that contain other
components.
Container container = frame.getContentPane();
container.add(new Button("Click Me"));