当前位置:文档之家› java实现客户端与服务端之间的通信

java实现客户端与服务端之间的通信

import .*;import java.io.*;import java.util.Calendar;import java.awt.*;import java.awt.event.*;class EchoClient extends Frame implements ActionListener,Runnable{ Thread t;static Calendar T;static int H;static int M;static int S;static String l;private Button b1 = new Button("发送");private Button b2 = new Button("关闭");private TextField t1 = new TextField(30);private TextArea t2 = new TextArea();private int m;private String n;Socket connection;DataInputStream in;DataOutputStream out;private class window extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(0);}}private void focusEvt(java.awt.event.WindowEvent evt) {t1.requestFocus();}public void clock(){T=Calendar.getInstance();H=T.get(Calendar.HOUR_OF_DAY);M=T.get(Calendar.MINUTE);S=T.get(Calendar.SECOND);l=String.valueOf(H)+":"+String.valueOf(M)+":"+String.valueOf(S); }public EchoClient(String i,int j){super("客户端");t = new Thread(this);m=j;n=i;t.start();}public void run(){Panel p1 = new Panel();p1.setLayout(new FlowLayout());p1.add(b2);p1.add(t1);p1.add(b1);setLayout(new BorderLayout());add("South",p1);add("Center",t2);b1.addActionListener(this);b2.addActionListener(this);t1.addActionListener(this);addWindowListener(new window());addWindowListener(new java.awt.event.WindowAdapter() {public void windowOpened(java.awt.event.WindowEvent evt) {focusEvt(evt);}});setSize(400,300);setVisible(true);try{connection = new Socket(n,m);in = new DataInputStream(connection.getInputStream());out = new DataOutputStream(connection.getOutputStream());String link = new String("");while(!link.toUpperCase().equals(".QUIT")){link = in.readUTF();clock();t2.append("服务端"+l+"\n"+link+"\n\n");} }catch(UnknownHostException uhe){System.err.println("Unknown Host:127.0.0.1");}catch(IOException ioe){System.err.println("IOException:"+ioe);}}public void actionPerformed(ActionEvent e){if(e.getSource()==b1||e.getSource()==t1){String line = t1.getText();clock();t2.append("客户端"+l+"\n"+line+"\n\n");t1.setText("");try{out.writeUTF(line);}catch(IOException ioe){System.err.println("IOException:"+ioe);}}else if(e.getSource()==b2){System.exit(0);}}public static String readString(){String string = new String();BufferedReader in = new BufferedReader(new InputStreamReader(System.in));try{string = in.readLine();}catch(IOException e){System.out.println("Console.readString:Unknown error...");System.exit(-1);}return string;}}class stat extends Frame implements ActionListener{private Label l1 = new Label("请输入IP地址:");private Label l2 = new Label("端口号:");private TextField q1 = new TextField(10);private TextField q2 = new TextField(10);private Button r1 = new Button("确定");private Button r2 = new Button("关闭");private int j;private String i,k;private class window extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(0);}}private void focusEvt(java.awt.event.WindowEvent evt) {q1.requestFocus();}public stat(){Panel y = new Panel();y.setLayout(new GridLayout(4,1));y.add(l1);y.add(q1);y.add(l2);y.add(q2);Panel x = new Panel();x.setLayout(new FlowLayout());x.add(r2);x.add(r1);setLayout(new BorderLayout());add("South",x);add("Center",y);r1.addActionListener(this);r2.addActionListener(this);q1.addActionListener(this);q2.addActionListener(this);addWindowListener(new java.awt.event.WindowAdapter() {public void windowOpened(java.awt.event.WindowEvent evt) {focusEvt(evt);}});addWindowListener(new window());setSize(200,150);setVisible(true);}public void actionPerformed(ActionEvent e){if(e.getSource()==q1){q2.requestFocus();}else if(e.getSource()==r1||e.getSource()==q2){i=q1.getText();k=q2.getText();j=Integer.parseInt(k);new EchoClient(i,j);dispose();}else if(e.getSource()==r2){System.exit(0);}}public static void main(String args[]){stat a = new stat();}}import .*;import java.io.*;import java.util.Calendar;import java.awt.*;import java.awt.event.*;class EchoServer{private static boolean running = true;public static void main(String args[]){try{ServerSocket server = new ServerSocket(7777);while(running){Socket connection = server.accept();EchoServer1 handler = new EchoServer1(connection);}}catch(IOException ioe){System.err.println("IOException:"+ioe);}}}class EchoServer1 extends Frame implements ActionListener,Runnable{ Thread t;static Calendar T;static int H;static int M;static int S;static String l;private static boolean running = true;private Button b1 = new Button("发送");private Button b2 = new Button("关闭");private TextField t1 = new TextField(30);private TextArea t2 = new TextArea();DataInputStream in;DataOutputStream out;Socket connection;private class window extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(0);}}private void focusEvt(java.awt.event.WindowEvent evt) {t1.requestFocus();}public void clock(){T=Calendar.getInstance();H=T.get(Calendar.HOUR_OF_DAY);M=T.get(Calendar.MINUTE);S=T.get(Calendar.SECOND);l=String.valueOf(H)+":"+String.valueOf(M)+":"+String.valueOf(S);}public EchoServer1(Socket _connection){super("服务端");connection = _connection;Panel p1 = new Panel();p1.setLayout(new FlowLayout());p1.add(b2);p1.add(t1);p1.add(b1);setLayout(new BorderLayout());add("South",p1);add("Center",t2);b1.addActionListener(this);b2.addActionListener(this);t1.addActionListener(this);addWindowListener(new window());addWindowListener(new java.awt.event.WindowAdapter() {public void windowOpened(java.awt.event.WindowEvent evt) {focusEvt(evt);}});setSize(400,300);setVisible(true);t = new Thread(this);t.start();}public void run(){try{in = new DataInputStream(connection.getInputStream());out = new DataOutputStream(connection.getOutputStream());String line = new String("");while(!line.toUpperCase().equals(".QUIT")){line = in.readUTF();clock();t2.append("客户端"+l+"\n"+line+"\n\n");}}catch(IOException ioe){System.err.println("IOException:"+ioe);}}public void actionPerformed(ActionEvent e){if(e.getSource()==b1||e.getSource()==t1){String link = t1.getText();clock();t2.append("服务端"+l+"\n"+link+"\n\n");t1.setText("");try{out.writeUTF(link);}catch(IOException ioe){System.err.println("IOException:"+ioe);}}if(e.getSource()==b2)System.exit(0);}}。

相关主题