Thursday 26 September 2013

CLIENT SEVER COMMUNICATION

CLIENT SERVER COMMUNICATION:
EchoClient.java:
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class EchoClient extends JFrame implements ActionListener
{
    JTextField tf=new JTextField(15);
    JTextField tf2=new JTextField(15);
    BufferedReader in;
    PrintWriter out;
    JButton b;
    public EchoClient()
    {
        try
        {
            JFrame f=new JFrame();
            f.setSize(100,100);
            Socket socket=new Socket("localhost",8008);
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            f.setTitle("client"+socket);
            tf.setText(" ");
            tf2.setText(" ");
            JPanel p=new JPanel();
            JLabel l=new JLabel("enter the text to sent to the server");
            JLabel l2=new JLabel("Text echoed from server in uppercase");
            b=new JButton("send");
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.add(l);
            p.add(tf);
            p.add(l2);
            p.add(tf2);
            p.add(b);
            f.add(p);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(this);
            out.flush();
            while(true)
            {
                String str=in.readLine();
                tf2.setText(str);
            }
        }catch(Exception e)
        {}
    }
public void actionPerformed(ActionEvent event)
{
    String str1=tf.getText();
    System.out.println("sending "+str1);
    out.println(str1.toString());
    out.flush();
    tf.setText(" ");
}
public static void main(String args[])
{
    EchoClient f=new EchoClient();
}
}

EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[])throws Exception
{
    try
    {
        ServerSocket s=new ServerSocket(8008);
        while(true)
        {
            Socket incoming=s.accept();
            new ClientHandler(incoming).start();
        }
    }catch(Exception e)
    {}
    }
}
class ClientHandler extends Thread
{
    Socket incoming;
    ClientHandler(Socket incoming)
    {
        this.incoming=incoming;
    }
    public void run()
    {
        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter out=new PrintWriter(new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())));
            out.flush();
            while(true)
            {
                String str=in.readLine();
                System.out.println("receiving from "+incoming+" "+str);
                out.println(str.toUpperCase());
                out.flush();
                if(str.trim().endsWith("BYE"))
                    break;
            }
            incoming.close();
        }catch(Exception e)
        {}
    }
}

No comments:

Post a Comment