Rambler's Top100

А Б В Г Д Е Ж З И К Л М Н О П Р С Т У Ф Х Ц Ч Ш Э Ю Я

Все примеры | Примеры по пакетам

Клиент для работы с простейшим сервером, оперирующим с SortedMap


//Клиент для работы с простейшим сервером, оперирующим с SortedMap
//Запись данных в карту на сервере
package server1;

import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MapCachedClient {
     static JButton jb=new JButton("put");
     static JButton jb1=new JButton("get");
     //Вектор которым обмениваются клиент и сервер
     static Vector vect1 =new Vector();
     static JEditorPane je=new JEditorPane();
     static JTextField jtf1=new JTextField();
     static JTextField jtf2=new JTextField();
     static JTextField jtf3=new JTextField();
     static JLabel jlb1=new JLabel();
     static JLabel jlb2=new JLabel();

    public static void main(String[] args) {
      AbstractAction putAction = new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
         Socket client = null;
       ObjectOutputStream outputStream = null;
        ObjectInputStream inputStream = null;
        int portnumber = 1234;
            try {
                String msg = "";

                // Создание клиентского сокета
                client = new Socket(InetAddress.getLocalHost(), portnumber);
              //  System.out.println("Клиентский сокет создан " + client);

                // Выходной поток клиента
                OutputStream clientOut = client.getOutputStream();
                PrintWriter pw = new PrintWriter(clientOut, true);

                // Входной поток клиента
                InputStream clientIn = client.getInputStream();
                BufferedReader br = new BufferedReader(new
                        InputStreamReader(clientIn));
                msg="put "+jtf1.getText()+" "+jtf2.getText();
                pw.println(msg);

                String msgFromServer = br.readLine();
    System.out.println("Сообщение от сервера: " + msgFromServer);
     if(msgFromServer!=null){
        System.out.println(" ! = " + msgFromServer);
                 Vector vect =new Vector();
                vect.add("a");
                vect.add("b");
                vect.add(msgFromServer);
            outputStream = new ObjectOutputStream(client.getOutputStream());

           outputStream.writeObject(vect);
                inputStream = new ObjectInputStream(client.getInputStream());
                 Vector vect1 =new Vector();
            Object obj = null;
            if ((obj = inputStream.readObject()) != null) {
                if (obj instanceof Vector) {
          System.out.println("Объект от сервера "+((Vector)obj).toString());
                }
                }

                }
              inputStream.close();
                outputStream.close();
                pw.close();
                br.close();
                client.close();
                jtf1.setText("");
                jtf2.setText("");
            } catch (ClassNotFoundException ex) {

            } catch (IOException ex) {

            }
        } catch (Exception e1) {
          e1.printStackTrace();
        }
      }
    };
    putAction.putValue(AbstractAction.NAME, "put");

    JButton putButton = new JButton(putAction);

        Box box = new Box(BoxLayout.X_AXIS);
        Box box1 = new Box(BoxLayout.Y_AXIS);

      JFrame jf=new JFrame();

      jf.setVisible(true);
      jf.setSize(533, 333);
      box = Box.createHorizontalBox();
      jtf1.setBounds(50, 20, 200, 22);
      jtf2.setBounds(50, 45, 200, 22);
      jlb1.setBounds(0, 20, 100, 22);
      jlb1.setText("key");
      jlb2.setBounds(0, 45, 100, 22);
      putButton.setBounds(320, 15, 200, 22);
       jlb2.setText("value");
      //box.add(box1);
     JPanel jp=new JPanel();
     jp.setLayout(null);
     jp.add(jlb1);
     jp.add(jlb2);
     jp.add(jtf1);
     jp.add(jtf2);
     jp.add(putButton);

     box.add(jp);


      jf.getContentPane().add(box);
      jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

    }

}

06.02.2009


Файл к примеру: Сервер помещающий данные в карту SortedMap
Rambler's Top100


Ассоциативные ссылки