java编写客户_java编写服务器和客户端.docx
java编写服务器和客户端.docx
还剩
4页未读,
继续阅读
下载文档到电脑,马上远离加班熬夜!
亲,喜欢就下载吧,价低环保!
内容要点:
}}运行结果:最后输入信息,如:你好吗? super("客户端");setSize(250, 250);panel.add(label);panel.add(tf);tf.addActionListener(this);add("West", panel);add("Center", ta);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});show();try {client = new Socket(InetAddress.getLocalHost(), 4000);ta.append("您服务器是:" + client.getInetAddress().getHostName() + "\n\n");in = client.getInputStream();out = client.getOutputStream();} catch (IOException ioe) {}while (true) {try {byte[] buf = new byte[256];in.read(buf);String str = new String(buf);ta.append("服务器端:" + str + "\n");} catch (IOException e) {}}}public void actionPerformed(ActionEvent e) {try {String str = tf.getText();byte[] buf = str.getBytes();tf.setText(null);out.write(buf);ta.append("客户端:" + str + "\n");} catch (IOException iOE) {}}public static void main(String args[]) {new client(); tf.setText(null);out.write(buf);ta.append(":" + str + "\n");} catch (IOException ioe) {}}public static void main(String[] args) {new aaa();}}运行结果:2、客户端程序代码如下:import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class client extends Frame implements ActionListener {Label label = new Label("交谈内容");Panel panel = new Panel();TextField tf = new TextField(10);TextArea ta = new TextArea();Socket client;InputStream in;OutputStream out;public client() {1、服务器程序代码如下:import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class aaa extends Frame implements
发表评论
暂无评论,赶快抢占沙发吧。