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