Java實現服務器與客戶端網絡通信

只能實現客戶端和服務器之間的網絡通信,多客戶端的暫時還不會,等待更新吧

1.Server.java

package hahaha;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Server {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WindowServer win = new WindowServer();
		win.setTitle("服務器");
	}

}

class WindowServer extends JFrame implements ActionListener {
	JButton start, send;
	JTextField port;
	JTextField inputText;
	JTextArea showResult;
	Socket socket = null;
	PrintWriter out = null;
	BufferedReader in = null;
	ServerThread thread;

	WindowServer() {
		setLayout(new FlowLayout());
		add(new JLabel("Port:"));
		port = new JTextField("8888", 25);
		add(port);
		start = new JButton("start");
		add(start);
		showResult = new JTextArea(15, 35);
		JScrollPane scroll = new JScrollPane(showResult);
		add(scroll);
		add(new JLabel("Say:"));
		inputText = new JTextField(26);
		add(inputText);
		send = new JButton("發送");
		send.setEnabled(false);
		add(send);
		start.addActionListener(this);
		send.addActionListener(this);
		showResult.append("Server starting……\n");
		setBounds(400, 100, 400, 400);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == start) {
			try {
				ServerSocket s = new ServerSocket(Integer.parseInt(port.getText()));
				socket = s.accept();
				showResult.append("Client connected……\n\n");
				// PrintWriter
				out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
				// BufferedReader
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				thread = new ServerThread();
				thread.start();
				send.setEnabled(true);
			} catch (IOException e1) {
			}
		}

		if (e.getSource() == send) {
			if (socket.isConnected()) {
				String s = inputText.getText();
				if (!s.isEmpty()) {
					out.println("服務器:" + s);
					showResult.append("服務器:" + s + "\n");
					inputText.setText(null);
					out.flush();
				}
			}
		}

	}

	class ServerThread extends Thread {
		String s = null;

		public void run() {
			while (true) {
				if (socket.isConnected()) {
					try {
						s = in.readLine();
						showResult.append(s + "\n");
					} catch (IOException e) {
						showResult.append("與服務器已斷開\n");
						break;
					}
				} else {
					showResult.append("與服務器已斷開\n");
				}
			}
		}
	}
}


2.Client.java

package hahaha;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WindowClient win = new WindowClient();
		win.setTitle("客戶端");
	}

}

class WindowClient extends JFrame implements Runnable, ActionListener {
	JTextField ip, port;
	JButton connection, send;
	JTextField inputText;
	JTextArea showResult;
	Socket socket = null;
	PrintWriter out = null;
	BufferedReader in = null;
	Thread thread;

	WindowClient() {
		socket = new Socket();
		setLayout(new FlowLayout());
		add(new JLabel("Serve IP:"));
		ip = new JTextField("127.0.0.1", 8);
		add(ip);
		add(new JLabel("Serve Port:"));
		port = new JTextField("8888", 8);
		add(port);
		connection = new JButton("Connect");
		add(connection);
		showResult = new JTextArea(15, 35);
		add(showResult);
		JScrollPane scroll = new JScrollPane(showResult);
		add(scroll);
		add(new JLabel("Say:"));
		inputText = new JTextField(25);
		add(inputText);
		send = new JButton("發送");
		send.setEnabled(false);
		add(send);

		connection.addActionListener(this);
		send.addActionListener(this);
		thread = new Thread(this);
		setBounds(800, 100, 410, 400);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == connection) {
			try {
				// 請求和服務器建立連接
				if (socket.isConnected()) {
				} else {
					InetAddress address = InetAddress.getByName("127.0.0.1");
					InetSocketAddress socketAddress = new InetSocketAddress(address, Integer.parseInt(port.getText()));
					socket.connect(socketAddress);
					showResult.append("Connect to server……\n\n");
					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					out = new PrintWriter(socket.getOutputStream());
					send.setEnabled(true);
					if (!(thread.isAlive())) {
						thread = new Thread(this);
					}
					thread.start();
				}
			} catch (IOException e1) {
				System.out.println(e1);
				socket = new Socket();
			}
		}

		if (e.getSource() == send) {
			if (socket.isConnected()) {

				String s = inputText.getText();
				if (!s.isEmpty()) {
					out.println("客戶端:" + s);
					showResult.append("客戶端:" + s + "\n");
					inputText.setText(null);
					out.flush();
				}
			}
		}

	}

	public void run() {
		String s = null;
		while (true) {
			try {
				s = in.readLine();
				showResult.append(s + "\n");
			} catch (Exception e) {
				showResult.append("服務器已斷開\n");
				break;
			}
		}
	}
}


3.界面展示



發佈了119 篇原創文章 · 獲贊 16 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章