⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 view.java

📁 一个基于eclipse插件开发的聊天程序
💻 JAVA
字号:
package talkClient;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 java.net.UnknownHostException;import javax.swing.JFrame;import javax.swing.JOptionPane;import org.eclipse.jface.dialogs.InputDialog;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.window.Window;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.part.ViewPart;public class View extends ViewPart {	public static final String ID = "TalkClient.view";	Text text;	Button sendButton;	public ServerSocket server_sock;	int port = 1861;	public void createPartControl(Composite parent) {		Composite top = new Composite(parent, SWT.NONE);		top.setLayout(new FillLayout(-1));		// message contents		text = new Text(top, SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL);		text.setText("Listening ...");		text.setLayoutData(new GridData(GridData.FILL_BOTH));		sendButton = new Button(top, SWT.NONE);		sendButton.setText("Send message");		sendButton.setLayoutData(new GridData(GridData.FILL_BOTH));		sendButton.addSelectionListener(new SelectionAdapter() {			public void widgetSelected(SelectionEvent e) {				String ip_str = "";				String message = "";				// DESTINATION IP INPUT				InputDialog dlg = new InputDialog(Display.getCurrent()						.getActiveShell(), "Destination", "IP Address", "",						null);				if (dlg.open() == Window.OK) {					ip_str = dlg.getValue().trim();					// MESSAGE INPUT					InputDialog dlg1 = new InputDialog(Display.getCurrent()							.getActiveShell(), "Destination", "Message", "",							null);					if (dlg1.open() == Window.OK) {						message = dlg1.getValue().trim();						// SOCKET CONNECTION						Socket req_socket = null;						try {							req_socket = new Socket(ip_str, port);						} catch (UnknownHostException e1) {							MessageDialog.openError(Display.getCurrent()									.getActiveShell(), "Error",									"Connection failed!");						} catch (IOException e1) {							MessageDialog.openError(Display.getCurrent()									.getActiveShell(), "Error",									"Connection failed!");						}						PrintWriter pw = null;						try {							pw = new PrintWriter(req_socket.getOutputStream(),									true);						} catch (IOException e2) {							e2.printStackTrace();						}						// SENT THE PACKAGE						pw.println(message);					}				}			}		});		// START LISTENING THREAD		new ListenThread().start();	}	public void setFocus() {	}	// LISTENING THREAD	class ListenThread extends Thread {		public void run() {			try {				server_sock = new ServerSocket(port); // Server				// socket.			} catch (IOException e1) {				e1.printStackTrace();			}			while (true) {				Socket accepted_sock = null;				String ip_peer = "";				try {					accepted_sock = server_sock.accept();					ip_peer = accepted_sock.getInetAddress().toString();					System.out.println(ip_peer);				} catch (IOException e2) {					e2.printStackTrace();				}				// GET DATA FROM THE RECEIVED SOCKET AND SHOW ON GUI				String data_str = "";				BufferedReader br_in = null;				try {					br_in = new BufferedReader(new InputStreamReader(							accepted_sock.getInputStream()));					data_str = br_in.readLine();					System.out.println(data_str);				} catch (IOException e2) {					e2.printStackTrace();				}								JFrame jframe = new JFrame();				JOptionPane.showMessageDialog(jframe, "Message From: " + ip_peer + " >> " + data_str);				//				MessageDialog//						.openInformation(Display.getCurrent().getActiveShell(),//								"Message From: " + ip_peer, data_str);			}		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -