阅读背景:

基于GUI的网络通信程序设计JAVA源代码

来源:互联网 

基于GUI的网络通讯程序设计JAVA源代码

client:

package gui;  //客户端代码

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Client {
    public static void main(String args[]) {
        new WindowClient();
    }
}
class WindowClient extends JFrame implements ActionListener,Runnable {
    JButton connect,say;
    JTextField inputTextIp,inputTextPort,inputTextSay;
    JTextArea show;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;
    
    WindowClient(){
        socket=new Socket();
        connect=new JButton("connect");
        say=new JButton("say");
        say.setEnabled(true);
        inputTextIp=new JTextField(15);
        inputTextPort=new JTextField(15);
        inputTextSay=new JTextField(30);
        show=new JTextArea();
        show.setEditable(false);
        
        JPanel pNorth=new JPanel();
        pNorth.setBorder(BorderFactory.createTitledBorder("客户机设置: "));
        pNorth.add(new JLabel("Server IP:"));
        pNorth.add(inputTextIp);
        pNorth.add(new JLabel("Server Port:"));
        pNorth.add(inputTextPort);
        pNorth.add(connect);
        
        JPanel pSouth=new JPanel();
        pSouth.add(new JLabel("say:"));
        pSouth.add(inputTextSay);
        pSouth.add(say);
        
        add(pNorth,BorderLayout.NORTH);
        add(new JScrollPane(show),BorderLayout.CENTER);
        add(pSouth,BorderLayout.SOUTH);
        
        connect.addActionListener(this);
        say.addActionListener(this);
        thread=new Thread(this);
        setBounds(100,500,600,400);
        setVisible(true);
        setTitle("客户端");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent e) {          
        if(e.getSource()==connect) {
            
            try {
                if(!socket.isConnected()) {              //断定是不是衔接
                    show.append("Connect to server…\n");
                    InetAddress address=InetAddress.getByName(inputTextIp.getText());
                    InetSocketAddress socketAddress=new InetSocketAddress(address,Integer.parseInt(inputTextPort.getText()));
                    socket.connect(socketAddress);
                    in=new DataInputStream(socket.getInputStream());
                    out=new DataOutputStream(socket.getOutputStream());
                    if(!thread.isAlive())
                        thread=new Thread(this);
                    thread.start();
                }
            }
            catch(IOException ee) {
                System.out.println(ee);
                socket=new Socket();
            }
        }
        if(e.getSource()==say) {
            String s_out=inputTextSay.getText();
            show.append("我说:"+s_out+"\n");
            try {
                out.writeUTF(s_out);
            }
            catch(IOException e1) {}
        }
    }
    
    public void run() {
        String s_in=null;
        while(true) {
            try {
                s_in=in.readUTF();
                show.append("服务器:"+s_in+"\n");
                System.out.println();
            }
            catch(IOException e) {
                show.setText("与服务器断开"+e);
                socket=new Socket();
                break;
            }
        }
    }
}package gui; 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: