А Б В Г Д Е Ж З И К Л М Н О П Р С Т У Ф Х Ц Ч Ш Э Ю Я Все примеры | Примеры по пакетам |
|
Клиент1 для работы с простейшим сервером приложений, оперирующим с SortedMap//Клиент1 для работы с простейшим сервером приложений, оперирующим с SortedMap
//Получение данных из карты
package server1;
import java.awt.event.ActionEvent;
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 javax.swing.AbstractAction;
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 MapCachedClient1 {
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();
static JEditorPane jep = new JEditorPane();
public static void main(String[] args) {
AbstractAction getAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Socket client = null;
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
int portnumber = 1234;
try {
String msg = "";
// Создать сокет клиента
client = new Socket(InetAddress.getLocalHost(), portnumber);
// Создать выходной поток
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut, true);
// Создать входной поток
InputStream clientIn = client.getInputStream();
BufferedReader br = new BufferedReader(new
InputStreamReader(clientIn));
msg="get "+jtf1.getText();
pw.println(msg);
String msgFromServer = br.readLine();
jep.setText(msgFromServer);
System.out.println("Сообщение от сервера: " + msgFromServer);
if(msgFromServer!=null){
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) {
jep.setText("Полученный объект: "+((Vector)obj).toString());
}
}
}
inputStream.close();
outputStream.close();
pw.close();
br.close();
client.close();
} catch (ClassNotFoundException ex) {
} catch (IOException ex) {
}
}};
getAction.putValue(AbstractAction.NAME, "get");
JButton getButton = new JButton(getAction);
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();
jep.setBounds(100, 60, 300, 200);
jtf1.setBounds(50, 20, 200, 22);
jlb1.setText("key");
jlb1.setBounds(0, 20, 100, 22);
jlb2.setText("От сервера");
jlb2.setBounds(0, 60, 100, 22);
getButton.setBounds(320, 15, 120, 20);
JPanel jp=new JPanel();
jp.setLayout(null);
jp.add(jlb1);
jp.add(jlb2);
jp.add(jtf1);
jp.add(jep);
jp.add(getButton);
box.add(jp);
jf.getContentPane().add(box);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
}
Файл к примеру: Сервер помещающий данные в карту SortedMap |
|
|
|
|