Vous êtes sur la page 1sur 45

INDEX

1. To find the IP Address of a host.


2. To implement the program for finding the port number.
3. Program to find the URL.
4. To enable 1-1 client server interaction.
5. To enable 1-many client server interaction.
6. To enable 1-1 client server interaction using TCP.
7. To enable 1-1 client server interaction using UDP.
8. To create frames using swings.
9. To write a program for sending a mail.
PROGRAM NO:-1

AIM: To find the IP address of a host.


PROGRAM:

//TO FIND THE IP ADDRESS


import java.net.*;
class InetAddressTest
{
public static void main(Stringargs[])throws
unknownHostException
{
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
address=InetAddress.getByName("it");
System.out.println("InetAddress for it="+address);
InetAddress sw[]=InetAddress.getAllByName("it");
for(int i=0;i<sw.length;i++)
System.out.println(sw[i]);
}
RESULT:
Program is executed successfully
PROGRAM NO:-2

AIM: To implement a program for finding the port number.


PROGRAM:

//Program to find port number


import java.net.*;
import java.io.*;
public class Ports
{
public static void main(String args[])
{
Socket S1;
String host="localhost";
if(args.length>0)
host=args[0];
for(int i=1590;i<1620;i++)
{
try
{
S1=new Socket(host,i);
System.out.println("There is a server on port:"+i+"host");
}
catch(UnknownHostException e)
{
System.err.println(e);
break;
}
catch(IOException e)
{
System.out.println("must not be a server on this port");
break;
}

}
}
}

RESULT:
Program is executed successfully
S:\>java ports
There is a server on port:1166host

S:\>-
PROGRAM NO:-3

AIM: Program to find the URL.


PROGRAM:

import java.io.*;
import java.net.*;
class UrlDemo
{
public static void main(String args[]) throws
MalformedURLException
{
URL hp=new URL("http://www.vrsiddhartha.ac.in/client");
System.out.println("protocol:"+hp.getProtocol());
System.out.println("port:"+hp.getPort());
System.out.println("Host:"+hp.getHost());
System.out.println("File:"+hp.getFile());
System.out.println("Exit:"+hp.toExternalForm());

}
}

RESULT:
Program is executed successfully
S:\cnlab >javac URLDemo

S:\cnlab>java UrllDemo
Protocol:http
Port:1
Host:www.vrsiddhartha.ac.in
File:/client
Exit:http://www.vrsiddhartha.ac.in/client

S:\cnlab>
PROGRAM NO:-4

AIM: To enable 1-1 client server interaction.


PROGRAM:
CLIENT

import java.io.*;
import java.net.*;
class bclient
{
public static void main(String args[])
{
DataInputStream dis;
BufferedReader br;
PrintWriter pw;
Socket cs;
String ss,s;
try
{
dis=new DataInputStream(System.in);
cs=new Socket("localhost",100);
br=new BufferedReader(new
InputStreamReader(cs.getInputStream()));
pw=new PrintWriter(cs.getOutputStream(),true);
while(true)
{
System.out.println("enter for msg for server");
s=dis.readLine();
pw.println(s);
System.out.println("server msg is"+br.readLine());
}
}
catch(Exception e)
{System.out.println(e);
}
}
}
SERVER

import java .io.*;


import java.net.*;
class bserver
{
public static void main(String args[])
{
DataInputStream dis;
PrintWriter pw;
Socket cs;
ServerSocket S;
String s1;
BufferedReader br;
try
{
S=new ServerSocket(100);
cs=S.accept();
dis=new DataInputStream(System.in);
br=new BufferedReader(new
InputStreamReader(cs.getInputStream()));
pw=new PrintWriter(cs.getOutputStream(),true);
while(true)
{
System.out.println("client msg is"+br.readLine());
System.out.println("enter msg for client");
s1=dis.readLine();
pw.println(s1);
System.out.println("client msg is"+br.readLine());
pw.println(dis.readLine());

}
catch(Exception e)
{
System.out.println(e);
}

}}
RESULT:
Program is executed successfully
PROGRAM NO:-5

AIM: To enable 1-many client server interaction.


PROGRAM:
CLIENT
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;

class SocketClient extends JFrame implements ActionListener


{
JLabel text,clicked;
JButton button;
JPanel panel;
JTextField textField;
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;

SocketClient()
{
text=new JLabel("text to send over socket");
textField=new JTextField(20);
button=new JButton("CLICK ME");
button.addActionListener(this);
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.green);
getContentPane().add(panel);
panel.add("North",text);
panel.add("Center",textField);
panel.add("South",button);
}

public void actionPerformed(ActionEvent event)


{
Object source=event.getSource();
if(source==button)
{
String text=textField.getText();
out.println(text);
textField.setText(new String(" "));

try
{
String line=in.readLine();
System.out.println("text received"+line);
}
catch(IOException e)
{
System.out.println("read failed");
System.exit(1);
}
}
}
public void listenSocket()
{
try
{
socket=new Socket("152.152.71.32",4444);
out=new
PrintWriter(socket.getOutputStream(),true);
in=new BufferedReader(new
InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException e)
{
System.out.println("Unknown host:Kq6py.eng");
System.exit(1);
}
catch(IOException e)
{
System.out.println("no i/p");
System.exit(1);
}
}

public static void main(String args[])


{
SocketClient frame=new SocketClient();
frame.setTitle("client progarm");
WindowListener l=new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
SERVER
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;

class SocketServer extends JFrame implements ActionListener


{
JButton button;
JLabel label=new JLabel("Text Received over socket:");
JPanel panel;
JTextArea textArea=new JTextArea();
ServerSocket server=null;
Socket Client=null;
BufferedReader in=null;
PrintWriter out=null;
String line;

SocketServer()
{
button=new JButton("click me");
button.addActionListener(this);

panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.green);
getContentPane().add(panel);
panel.add("North",label);
panel.add("Center",textArea);
panel.add("South",button);
}
public void actionPerformed(ActionEvent event)
{
Object source=event.getSource();
if(source==button)
{
textArea.setText(line);
}
}
public void listenSocket()
{
try
{
server=new ServerSocket(4444);
}
catch(IOException e)
{
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
try
{
Client=server.accept();
}
catch(IOException e)
{
System.out.println("Accept failed:4444");
System.exit(-1);
}
try
{
in= new BufferedReader(new
InputStreamReader(Client.getInputStream()));
out=new
PrintWriter(Client.getOutputStream(),true);
}
catch(IOException e)
{
System.out.println("Accept failed:4444");
System.exit(1);
}

while(true)
{
try
{
line=in.readLine();
out.println(line);
}
catch(IOException e)
{
System.out.println("Read Failed");
System.exit(-1);
}
}
}
protected void finalize()
{
try
{
in.close();
out.close();
server.close();
}
catch(IOException e)
{
System.out.println("could not close");
System.exit(-1);
}
}
public static void main(String args[])
{
SocketServer frame=new SocketServer();
frame.setTitle("Server Program");
WindowListener l= new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
RESULT: Program is executed successfully
PROGRAM NO:-7

AIM: To enable 1-1 client server interaction using UDP


PROGRAM:
CLIENT

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener
{
private JTextField enter;
private JTextArea display;
private DatagramPacket sendpacket,receivepacket;
private DatagramSocket socket;
public Client()
{
super("Client");
enter=new JTextField("Type message here");
enter.addActionListener(this);
getContentPane().add(enter,BorderLayout.NORTH);
display=new JTextArea();
getContentPane().add(new
JScrollPane(display),BorderLayout.CENTER);
setSize(400,300);
show();
try
{
socket=new DatagramSocket();
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(1);
}
}
public void waitForPackets()
{
while(true)
{
try
{
byte data[]=new byte[100];
receivepacket=new DatagramPacket(data,data.length);
socket.receive(receivepacket);
display.append("\nPacket received:" + "\nFrom host: " +
receivepacket.getAddress() + "\nHost port: " +
receivepacket.getPort() + "\nLength:" + receivepacket.getLength()
+"\nContaining:\n\t" + new
String( receivepacket.getData(),0,receivepacket.getLength()));
display.setCaretPosition(display.getText().length());
}
catch( IOException exception)
{
display.append(exception.toString() + "\n");
exception.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
try
{
display.append("\nSendingPacket Containing:"
+e.getActionCommand() +"\n");
String s=e.getActionCommand();
byte data[]=s.getBytes();
sendpacket=new
DatagramPacket(data,data.length,InetAddress.getLocalHost(),5000
);
socket.send(sendpacket);
display.append("packet sent\n");
display.setCaretPosition(display.getText().length());
}
catch(IOException exception )
{
display.append(exception.toString() +"\n");
exception.printStackTrace();
}
}
public static void main(String args[])
{
Client app=new Client();
app.addWindowListener( new WindowAdapter()
{
public void
windowClosing(WindowEvent e)
{
System.exit(0);
}
}
)
app.waitForPackets();
}
}
SERVER

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame
{
private JTextArea display;
private DatagramPacket sendpacket,receivepacket;
private DatagramSocket socket;
public Server()
{
super("Server");
display=new JTextArea();
getContentPane().add(new
JScrollPane( display),BorderLayout.CENTER);
setSize(400,300);
show();
try
{
socket=new DatagramSocket(5000);
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(1);
}
}
public void waitForPackets()
{
while(true)
{
try
{
byte data[]=new byte[100];
receivepacket=new DatagramPacket(data,data.length);
socket.receive(receivepacket);
display.append("\nPacket received:" + "\nFrom host: " +
receivepacket.getAddress() + "\nHost port: " +
receivepacket.getPort() + "\nLength:" + receivepacket.getLength()
+"\nContaining:\n\t" + new
String( receivepacket.getData(),0,receivepacket.getLength()));
display.append("\n\nEcho data to client...");
sendpacket=new
DatagramPacket( receivepacket.getData(),receivepacket.getLength
(),receivepacket.getAddress(),receivepacket.getPort());
socket.send(sendpacket);
display.append("packet sent\n");
display.setCaretPosition(display.getText().length());
}
catch(IOException io)
{
display.append(io.toString() +"\n");
io.printStackTrace();
}
}
}
public static void main(String args[])
{
Server app=new Server();
app.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.waitForPackets();
}
}

RESULT:
Program is executed successfully
PROGRAM NO:-8

AIM: To create frames using Swings


PROGRAM:

import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
//import javax.swing.JTextField;
//import javax.swing.JButton;
//import javax.swing.JTextArea;
//import java.awt.Dimension;
//import java.awt.FlowLayout;

public class swings1 extends JFrame{

public static void main(String[] args)


{

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame1 = new JFrame();
frame1.getContentPane().setLayout(new FlowLayout());
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//String text = " This represents a multiline Textarea for


displaying text.";

JTextArea ta = new JTextArea("This is new text",5,10);


ta.setPreferredSize(new Dimension(100, 100));
ta.setLineWrap(true);
frame1.getContentPane().add(ta);

JTextField jTextField1 = new JTextField();


jTextField1.setText("hai");
frame1.getContentPane().add(jTextField1);
frame1.setSize(300,200);
frame1.setVisible(true);

JLabel label = new JLabel("Hello world");


frame1.getContentPane().add(label); // adds to CENTER
frame1.pack();
frame1.setVisible(true);

JButton jb = new JButton();


jb.setText("Send ");
frame1.getContentPane().add(jb);
frame1.setSize(300,200);
frame1.setVisible(true);
}
}

RESULT:
Program is executed successfully
PROGRAM NO:-9

AIM: To write a program for sending a mail..


PROGRAM:

import java.awt.*;
import javax.swing.*;

public class SendMail extends javax.swing.JFrame

public SendMail()

setTitle("SendMail Example");
getContentPane().setLayout(null);
setSize(736, 312);
setVisible(false);
JLabel1.setText("From:");
getContentPane().add(JLabel1);
JLabel1.setBounds(12, 12, 36, 12);
JLabel2.setText("To:");
getContentPane().add(JLabel2);
JLabel2.setBounds(12, 48, 36, 12);
JLabel3.setText("Subject:");
getContentPane().add(JLabel3);
JLabel3.setBounds(12, 84, 48, 12);
JLabel4.setText("SMTP Server:");
getContentPane().add(JLabel4);
JLabel4.setBounds(12, 120, 84, 12);
getContentPane().add(_from);
_from.setBounds(96, 12, 300, 24);
getContentPane().add(_to);
_to.setBounds(96, 48, 300, 24);
getContentPane().add(_subject);
_subject.setBounds(96, 84, 300, 24);
getContentPane().add(_smtp);
_smtp.setBounds(96, 120, 300, 24);
getContentPane().add(_scrollPane2);
_scrollPane2.setBounds(12, 156, 384, 108);
_body.setText("Enter your message here.");
_scrollPane2.getViewport().add(_body);
_body.setBounds(0, 0, 381, 105);
Send.setText("Send");
Send.setActionCommand("Send");
getContentPane().add(Send);
Send.setBounds(60, 276, 132, 24);
Cancel.setText("Cancel");
Cancel.setActionCommand("Cancel");
getContentPane().add(Cancel);
Cancel.setBounds(216, 276, 120, 24);
getContentPane().add(_scrollPane);
_scrollPane.setBounds(408, 12, 312, 288);
getContentPane().add(_output);
_output.setBounds(408, 12, 309, 285);
SymAction lSymAction = new SymAction();
Send.addActionListener(lSymAction);
Cancel.addActionListener(lSymAction);
_output.setModel(_model);
_model.addElement("Server output displayed here:");
_scrollPane.getViewport().setView(_output);
_scrollPane2.getViewport().setView(_body);
}

public void setVisible(boolean b)

{
if (b)
setLocation(50, 50);
super.setVisible(b);
}

static public void main(String args[])

{
(new SendMail()).show();

public void addNotify()

Dimension size = getSize();

super.addNotify();

if (frameSizeAdjusted)
return;
frameSizeAdjusted = true;
Insets insets = getInsets();
javax.swing.JMenuBar menuBar =
getRootPane().getJMenuBar();
int menuBarHeight = 0;
if (menuBar != null)
menuBarHeight = menuBar.getPreferredSize().height;
setSize(insets.left + insets.right + size.width, insets.top
+ insets.bottom + size.height + menuBarHeight);
}

boolean frameSizeAdjusted = false;


javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
javax.swing.JLabel JLabel4 = new javax.swing.JLabel();
javax.swing.JTextField _from = new javax.swing.JTextField();
javax.swing.JTextField _to = new javax.swing.JTextField();
javax.swing.JTextField _subject = new javax.swing.JTextField();
javax.swing.JTextField _smtp = new javax.swing.JTextField();
javax.swing.JScrollPane _scrollPane2 = new
javax.swing.JScrollPane();
javax.swing.JTextArea _body = new javax.swing.JTextArea();
javax.swing.JButton Send = new javax.swing.JButton();
javax.swing.JButton Cancel = new javax.swing.JButton();
javax.swing.JScrollPane _scrollPane = new
javax.swing.JScrollPane();
javax.swing.JList _output = new javax.swing.JList();
javax.swing.DefaultListModel _model = new
javax.swing.DefaultListModel();
java.io.BufferedReader _in;
java.io.PrintWriter _out;

class SymAction implements java.awt.event.ActionListener

public void actionPerformed(java.awt.event.ActionEvent event)


{
Object object = event.getSource();
if (object == Send)
Send_actionPerformed(event);
else if (object == Cancel)
Cancel_actionPerformed(event);
}
}

protected void send(String s) throws java.io.IOException

if (s != null) {
_model.addElement("C:" + s);
_out.println(s);
_out.flush();
}

String line = _in.readLine();


if (line != null) {
_model.addElement("S:" + line);
}
}
void Send_actionPerformed(java.awt.event.ActionEvent event)

try

java.net.Socket s = new java.net.Socket(_smtp.getText(), 25);


_out = new java.io.PrintWriter(s.getOutputStream());
_in = new java.io.BufferedReader(new
java.io.InputStreamReader(s
.getInputStream()));

send(null);
send("HELO " +
java.net.InetAddress.getLocalHost().getHostName());
send("MAIL FROM: " + _from.getText());
send("RCPT TO: " + _to.getText());
send("DATA");
_out.println("Subject:" + _subject.getText());
_out.println(_body.getText());
send(".");
s.close();

catch (Exception e)
{
_model.addElement("Error: " + e);
}

void Cancel_actionPerformed(java.awt.event.ActionEvent event)

{
System.exit(0);

RESULT:
Program is executed successfully