天天看点

JAVA实现基于GUI的网络通信程序 源代码

Client:

package firststudy;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class Client{
	public static void main(String[] args) {
		cframe cf = new cframe();
	}
}
class cframe {
	Socket socket;
	JFrame f1;
	JPanel p1;
	JPanel p2;
	JPanel p3;
	JPanel p4;
	JLabel label1;
	JLabel label2;
	JLabel label3;
	JTextField ip;
	JTextField port;
	JTextField say;
	JButton button1;
	JButton button2;
	JTextArea area;
	JScrollPane scroll;
	cframe(){
		init();
		button1.addActionListener(new connectListener());
		button2.addActionListener(new sayListener());
	}
	void init()
	{
         f1 = new JFrame("客户端");
		 p1 = new JPanel();
		 p2 = new JPanel();
		 p3 = new JPanel();
		 p4 = new JPanel();
		 label1 = new JLabel("Server IP:");
		 label2 = new JLabel("Server Port");
		 label3 = new JLabel("say");
		 ip = new JTextField("127.0.0.1",12);
		 port = new JTextField("8888",12);
		 say = new JTextField("hi",50);
		 button1 = new JButton("connect");
		 button2 = new JButton("say");
		 area = new JTextArea(16,58);
		 scroll = new JScrollPane(area);
		 area.add(new JScrollPane());
			f1.getContentPane().setLayout(new FlowLayout());
			f1.setSize(700, 600);
			f1.setLocation(400, 100);
			p1.add(new JLabel("客户机设置"));
			p2.add(label1);
			p2.add(ip);
			p2.add(label2);
			p2.add(port);
			p2.add(button1);
			p3.add(area);
			p3.add(scroll);
			p4.add(label3);
			p4.add(say);
			p4.add(button2);
			f1.getContentPane().add(p1);
			f1.getContentPane().add(p2);
			f1.getContentPane().add(p3);
			f1.getContentPane().add(p4);
			f1.setVisible(true);
	}
	class connectListener implements ActionListener{
		int portNum = 0;
		@Override
		public void actionPerformed(ActionEvent e) {
			button1.setEnabled(false);
			try {
				area.append("connecting>>>>......"+"\n");
				portNum = Integer.parseInt(port.getText());
				socket = new Socket(ip.getText(),portNum);
				gthread ct = new gthread();
				ct.start();
		}catch(Exception e1) {
			e1.printStackTrace();
		}
	}
}
	class gthread extends Thread{
		public void run() {
			String str = "";
			try {
				BufferedReader in = new BufferedReader(new 
					InputStreamReader(socket.getInputStream()));
				while(true) {
					str = in.readLine();
					area.append(str + "\n");
				}
			}catch(Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	class sthread extends Thread{
		public void run() {
			String str = "";
			try {
				PrintWriter out = new PrintWriter(new BufferedWriter(new 
						OutputStreamWriter(socket.getOutputStream())),true); 
				str = say.getText();
				if(!str.isEmpty()) {
					out.println(new Date().toLocaleString()+"\n"+str);
			area.append(new Date().toLocaleString() + "\n me:" +str+"\n");
					out.flush();
				}
				say.setText("");
			}catch(Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	class sayListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			sthread st = new sthread();
			st.start();
		}
	}
}
           

Server:

package firststudy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server{
	public static void main(String[] args) {
		frame f = new frame();	
	}
}
class frame{
	Socket socket;
	JFrame f1 ;
	JPanel p1;
	JPanel p2;
	JPanel p3;
	JPanel p4;
	JLabel label1;
	JLabel label2;
	JTextField port;
	JTextField say;
	JButton button1;
	JButton button2;
	TextArea area;
	JScrollPane scroll;
	frame(){
		init();
		button1.addActionListener(new portListen());
		button2.addActionListener(new sayListen());
	}
	void init() {
        JFrame f1 = new JFrame("服务器");
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		label1 = new JLabel("port");
		label2 = new JLabel("say");
		port = new JTextField("8888",50);
		say = new JTextField("hi",50);
		button1 = new JButton("start");
		button2 = new JButton("say");
		area = new TextArea(15,58);
		JScrollPane scroll = new JScrollPane(area);
	f1.getContentPane().setLayout(new FlowLayout());
	f1.setSize(700, 600);
	f1.setLocation(400, 100);
	p1.add(new JLabel("服务器设置"));
	p2.add(label1);
	p2.add(port);
	p2.add(button1);
	p3.add(area);
	p3.add(scroll);
	p4.add(label2);
	p4.add(say);
	p4.add(button2);
	f1.getContentPane().add(p1);
	f1.getContentPane().add(p2);
	f1.getContentPane().add(p3);
	f1.getContentPane().add(p4);
	f1.setVisible(true);
	f1.setResizable(false);
	f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	class portListen implements ActionListener{  //监听点击事件
		@Override
		public void actionPerformed(ActionEvent e) {
		    button1.setEnabled(false);
			try {
				ServerSocket s = new ServerSocket(Integer.parseInt(port.getText()));
			       Socket socket = s.accept();
			       area.append("connect successful"+"\n");
			gthread gthread = new gthread(socket);
			gthread.start();
		 }
			catch(Exception e1) {
				e1.getStackTrace();
			}
		}
	}
	class sayListen implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			sthread st = new sthread();
			st.start();
		}
	}
  class sthread extends Thread{
		String st="";
		public void run() {
				try {
					PrintWriter out = new PrintWriter(new 
							BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
					st = say.getText();
					if(!st.isEmpty()) {
						out.println(new Date().toLocaleString() +"\n" + st);
			area.append(new Date().toLocaleString() +"\n" +"me:" + st + "\n" );
				        out.flush();
					}
							say.setText("");	
				}catch(Exception e1) {
					e1.printStackTrace();
				}	
	}
  }
  
  
  
	class gthread extends Thread{
		Socket so;
		public gthread(Socket so) {
			super();
			this.so = so;
		}
		public void run() {
			try {
				PrintWriter out = new PrintWriter(new BufferedWriter(new
					OutputStreamWriter(so.getOutputStream())),true);
          //创建一个打印输出流对象,其行参从套接字socket对象获取输出流		
		out.println("connected"+"\n");
				BufferedReader in
				= new BufferedReader(new 
					InputStreamReader(so.getInputStream()));
				String str;
				boolean flag = true;
				while(flag) {
					str = in.readLine();
					area.append(str + "\n");
				}
				out.close();
				in.close();
				so.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
			String sr="";
			try {
				PrintWriter out = new PrintWriter(new 
						BufferedWriter(new OutputStreamWriter(so.getOutputStream())));
				sr = say.getText();
				if(!sr.isEmpty()) {
					out.println(new Date().toLocaleString() +"\n" + sr);
					area.append(new Date().toLocaleString() +"\n" +"me:" + sr + "\n" );
			        out.flush();
				}
						say.setText("");	
			}catch(Exception e1) {
				e1.printStackTrace();
			}	
		}
	}
}
           

继续阅读