Java--Socket通訊(雙向,有介面)

zlQ_發表於2013-04-04

 

服務端:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
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.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class ChatFrameServer{
	
	private PrintWriter pw;
	private JFrame frame;
	private JPanel pane_buttom;
	private JSplitPane pane_center;

	//顯示內容的文字框,輸入內容的文字框,傳送內容按鈕
	private JScrollPane pane_showWindow;
	private JScrollPane pane_inputWindow;
	private JTextArea area_showWindow;
	private JTextArea area_inputWindow;
	
	private JButton btn_send;

	private Dimension dimension;//用於設定area_showWindow可拖拉的大小

	//初始化
	public ChatFrameServer() {
		frame = new JFrame();
		pane_buttom = new JPanel();
		pane_showWindow = new JScrollPane();
		pane_inputWindow = new JScrollPane();
		area_showWindow = new JTextArea();
		area_inputWindow = new JTextArea();
		pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, pane_showWindow, pane_inputWindow);
		btn_send = new JButton("傳送");

		dimension = new Dimension(50, 300);

	}

	//呼叫方法顯示視窗
	public void showFrame(){
		initFrame();
		initChatTextArea();
		initButton();
		btn_send();
		socket();
	}

	//主窗體
	public void initFrame(){
		frame.setTitle("服務端");
		int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setBounds(width / 2, height / 2, 400, 450);
		frame.setVisible(true);
	}

	//內容顯示文字框和輸入內容文字框
	private void initChatTextArea(){
		//取得檢視焦點
		pane_showWindow.getViewport().add(area_showWindow);
		pane_inputWindow.getViewport().add(area_inputWindow);
		//將顯示文字域設定為不可編輯
		area_showWindow.setEditable(false);
		//設定顯示文字域可拖拉的大小 
		pane_showWindow.setMinimumSize(dimension);
		frame.add(pane_center, BorderLayout.CENTER);
	}

	//傳送檔案,傳送內容按鈕
	public void initButton(){
		pane_buttom.add(btn_send);
		frame.add(pane_buttom, BorderLayout.SOUTH);
	}


	private void btn_send(){
		btn_send.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String info = area_inputWindow.getText();
				area_showWindow.append("服務端:"+info+"\r\n");
				pw.println(info);
				area_inputWindow.setText("");
			}
		});
	}
	private void socket(){
		ServerSocket ss;
		try {
			ss = new ServerSocket(9988);
			//等待連線 客戶端
			Socket s=ss.accept();
			InputStreamReader isr=new InputStreamReader(s.getInputStream());
			BufferedReader br=new BufferedReader(isr);
			//PrintWriter必須和socket有密切的關係
			pw=new PrintWriter(s.getOutputStream(),true);

			//讀取從客戶端法發來的資訊
			while(true) {
				//讀取從客戶端發來的資訊
				String info=br.readLine();
				//在文字欄裡顯示
				area_showWindow.append("客戶端:"+info+"\r\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ChatFrameServer chat = new ChatFrameServer();
		chat.showFrame();
	}
}


客戶端:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
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.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class ChatFrame{
	private PrintWriter pw;
	private JFrame frame;
	private JPanel pane_buttom;
	private JSplitPane pane_center;

	//顯示內容的文字框,輸入內容的文字框,傳送內容按鈕
	private JScrollPane pane_showWindow;
	private JScrollPane pane_inputWindow;
	private JTextArea area_showWindow;
	private JTextArea area_inputWindow;

	private JButton btn_send;

	private Dimension dimension;//用於設定area_showWindow可拖拉的大小


	//初始化
	public ChatFrame() {
		frame = new JFrame();
		pane_buttom = new JPanel();
		pane_showWindow = new JScrollPane();
		pane_inputWindow = new JScrollPane();
		area_showWindow = new JTextArea();
		area_inputWindow = new JTextArea();
		pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, pane_showWindow, pane_inputWindow);
		btn_send = new JButton("傳送");

		dimension = new Dimension(50, 300);
	}

	//呼叫方法顯示視窗
	public void showFrame(){
		initFrame();
		initChatTextArea();
		initButton();
		btn_send();
		socket();
	}

	//主窗體
	public void initFrame(){
		frame.setTitle("客戶端");
		int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setBounds(width / 2, height / 2, 400, 450);
		frame.setVisible(true);
	}

	//內容顯示文字框和輸入內容文字框
	private void initChatTextArea(){
		//取得檢視焦點
		pane_showWindow.getViewport().add(area_showWindow);
		pane_inputWindow.getViewport().add(area_inputWindow);
		//將顯示文字域設定為不可編輯
		area_showWindow.setEditable(false);
		//設定顯示文字域可拖拉的大小 
		pane_showWindow.setMinimumSize(dimension);
		frame.add(pane_center, BorderLayout.CENTER);
	}

	//傳送檔案,傳送內容按鈕
	public void initButton(){
		pane_buttom.add(btn_send);
		frame.add(pane_buttom, BorderLayout.SOUTH);
	}

	private void btn_send(){
		btn_send.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String info = area_inputWindow.getText();
				area_showWindow.append("客戶端: "+info+"\r\n");
				pw.println(info);
				area_inputWindow.setText("");
			}
		});
	}
	
	private void socket(){
		try {
			Socket s = new Socket("127.0.0.1",9988);
			InputStreamReader isr=new InputStreamReader(s.getInputStream());
			BufferedReader br=new BufferedReader(isr);
			pw=new PrintWriter(s.getOutputStream(),true);
			while(true){
				//不停地讀取從伺服器端發來的資訊
				String info=br.readLine();
				area_showWindow.append("服務端: "+info+"\r\n");
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ChatFrame chat = new ChatFrame();
		chat.showFrame();
	}
}


 

相關文章