0% found this document useful (0 votes)
2 views

Sample Program

The document contains Java source code for a simple server-client application that facilitates communication between two clients using sockets. It also includes examples of basic Swing applications, demonstrating the creation of a JFrame with a button and a menu with menu items. The code illustrates fundamental concepts of networking and GUI development in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sample Program

The document contains Java source code for a simple server-client application that facilitates communication between two clients using sockets. It also includes examples of basic Swing applications, demonstrating the creation of a JFrame with a button and a menu with menu items. The code illustrates fundamental concepts of networking and GUI development in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SAMPLE PROGRAM

PROGRAM 4:

SOURCE CODE:

import java.io.*;

import java.net.*;

class Server {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(1234);

System.out.println("Server is running...");

Socket socket1 = serverSocket.accept();

Socket socket2 = serverSocket.accept();

new ClientHandler(socket1, socket2).start();

class ClientHandler extends Thread {

private Socket socket1;

private Socket socket2;

private BufferedReader in1;

private BufferedReader in2;

private PrintWriter out1;

private PrintWriter out2;

public ClientHandler(Socket socket1, Socket socket2) throws IOException {

this.socket1 = socket1;

this.socket2 = socket2;

in1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));

out1 = new PrintWriter(socket1.getOutputStream(), true);

in2 = new BufferedReader(new InputStreamReader(socket2.getInputStream()));

out2 = new PrintWriter(socket2.getOutputStream(), true);

public void run() {


try {

while (true) {

String message1 = in1.readLine();

if (message1 == null) {

break;

System.out.println("Client 1: " + message1);

out2.println("Client 1: " + message1);

String message2 = in2.readLine();

if (message2 == null) {

break;

System.out.println("Client 2: " + message2);

out1.println("Client 2: " + message2);

} catch (IOException e) {

System.out.println("Client disconnected");

} finally {

try {

socket1.close();

socket2.close();

} catch (IOException e) {

e.printStackTrace();

class Client {

public static void main(String[] args) throws IOException {

Socket socket = new Socket("localhost", 1234);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);


BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));

while (true) {

System.out.println("Enter message: ");

String message = consoleInput.readLine();

out.println(message);

String response = in.readLine();

System.out.println(response);

OUTPUT:
PROGRAM 6:

SOURCE CODE:

import javax.swing.*;

public class FirstSwingExample {

public static void main(String[] args) {

JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton

b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height

f.setLayout(null);//using no layout managers

f.setVisible(true);//making the frame visible

}
OUTPUT:
PROGRAM 6:

SOURCE CODE:

import javax.swing.*;

class MenuExample

JMenu menu, submenu;

JMenuItem i1, i2, i3, i4, i5;

MenuExample(){

JFrame f= new JFrame("Menu and MenuItem Example");

JMenuBar mb=new JMenuBar();

menu=new JMenu("Menu");

submenu=new JMenu("Sub Menu");

i1=new JMenuItem("Item 1");

i2=new JMenuItem("Item 2");

i3=new JMenuItem("Item 3");

i4=new JMenuItem("Item 4");

i5=new JMenuItem("Item 5");

menu.add(i1); menu.add(i2); menu.add(i3);

submenu.add(i4); submenu.add(i5);

menu.add(submenu);

mb.add(menu);

f.setJMenuBar(mb);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[])

new MenuExample();

}}
OUTPUT:

You might also like