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

1 篇文章 0 订阅
1 篇文章 0 订阅

基于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;
            }
        }
    }
}

server:

package gui;  //服务器代码

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

public class Server {
    public static void main(String args[]) {
        new WindowServer();
    }
}

class WindowServer extends JFrame implements Runnable,ActionListener{
    JButton start,say;
    JTextField inputTextPort,inputTextSay;
    JTextArea show;
    ServerSocket serversocket=null;
    Socket socket=null;
    DataOutputStream out=null;
    DataInputStream in=null;
    Thread thread;
    
    WindowServer(){
        socket=new Socket();
        start=new JButton("Start");
        say=new JButton("Say");
        say.setEnabled(true);
        inputTextPort=new JTextField(30);
        inputTextSay=new JTextField(30);
        show=new JTextArea();
        show.setEditable(false);
        
        JPanel pNorth=new JPanel();
        pNorth.setBorder(BorderFactory.createTitledBorder("服务器设置: "));
        pNorth.add(new JLabel("Port:"));
        pNorth.add(inputTextPort);
        pNorth.add(start);
        
        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);
        
        start.addActionListener(this);
        say.addActionListener(this);
        thread=new Thread(this);
        
        setBounds(10,10,600,400);
        setVisible(true);
        setTitle("服务器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent e) {         
        
        if(e.getSource()==start) {
            show.append("Server starting…\n");
            
            try {
                if(!socket.isConnected()) {
                ServerSocket serversocket=new ServerSocket(Integer.parseInt(inputTextPort.getText()));
                socket=serversocket.accept();
                    show.append("Client connected…\n");
                    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");
            }
            catch(IOException e) {
                show.setText("客户离开");
                socket=new Socket();
                break;
            }
        }
    }
}


推荐一个代码:https://blog.csdn.net/weixin_43869657/article/details/101109075

  • 11
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值