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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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();

}

相关文档
最新文档