Beruflich Dokumente
Kultur Dokumente
}
}
}
RESULT:
Program is executed successfully
S:\>java ports
There is a server on port:1166host
S:\>-
PROGRAM NO:-3
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
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
}
catch(Exception e)
{
System.out.println(e);
}
}}
RESULT:
Program is executed successfully
PROGRAM NO:-5
import java.io.*;
import java.net.*;
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);
}
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);
}
}
import java.io.*;
import java.net.*;
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
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
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;
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame1 = new JFrame();
frame1.getContentPane().setLayout(new FlowLayout());
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RESULT:
Program is executed successfully
PROGRAM NO:-9
import java.awt.*;
import javax.swing.*;
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);
}
{
if (b)
setLocation(50, 50);
super.setVisible(b);
}
{
(new SendMail()).show();
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);
}
if (s != null) {
_model.addElement("C:" + s);
_out.println(s);
_out.flush();
}
try
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);
}
{
System.exit(0);
RESULT:
Program is executed successfully