请问在SWT中如何处理ServerSocket对象的中的accept函数
代码如下:/**
 * 
 */
package Test;import java.io.IOException;
import java.net.ServerSocket;import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;/**
 * @author bentu
 * 
 */
public class ThreadTest extends ApplicationWindow { private Text text; /**
 * Create the application window
 */
public ThreadTest() {
super(null);
createActions();
addToolBar(SWT.FLAT | SWT.WRAP);
addMenuBar();
addStatusLine();
} /**
 * Create contents of the application window
 * 
 * @param parent
 */
@Override
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE); final Button button = new Button(container, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) { // TODO Auto-generated method stub
try {
final ServerSocket serverSocket = new ServerSocket(9000);
Display.getDefault().asyncExec(new Runnable() { public void run() {
// TODO
// Auto-generated
// method stub
try {
serverSocket.accept(); } catch (IOException e) {
// TODO
// Auto-generated
// catch block
e.printStackTrace();
}
} }); } catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} }
});
button.setText("button");
button.setBounds(296, 139, 48, 22); text = new Text(container, SWT.BORDER);
text.setBounds(51, 88, 80, 25);
//
return container;
} /**
 * Create the actions
 */
private void createActions() {
// Create the actions
} /**
 * Create the menu manager
 * 
 * @return the menu manager
 */
@Override
protected MenuManager createMenuManager() {
MenuManager menuManager = new MenuManager("menu");
return menuManager;
} /**
 * Create the toolbar manager
 * 
 * @return the toolbar manager
 */
@Override
protected ToolBarManager createToolBarManager(int style) {
ToolBarManager toolBarManager = new ToolBarManager(style);
return toolBarManager;
} /**
 * Create the status line manager
 * 
 * @return the status line manager
 */
@Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
statusLineManager.setMessage(null, "");
return statusLineManager;
} /**
 * Launch the application
 * 
 * @param args
 */
public static void main(String args[]) {
try {
ThreadTest window = new ThreadTest();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Configure the shell
 * 
 * @param newShell
 */
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("New Application");
} /**
 * Return the initial size of the window
 */
@Override
protected Point getInitialSize() {
return new Point(500, 375);
}}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【bentu610】截止到2008-07-13 17:07:19的历史汇总数据(不包括此帖):
    发帖的总数量:11                       发帖的总分数:630                      每贴平均分数:57                       
    回帖的总数量:15                       得分贴总数量:4                        回帖的得分率:26%                      
    结贴的总数量:5                        结贴的总分数:500                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:6                        未结的总分数:130                      
    结贴的百分比:45.45 %               结分的百分比:79.37 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    楼主该结一些帖子了
      

  2.   

    程序逻辑还有点问题,自己再研究研究线程那一部分如下
            button.addSelectionListener(new SelectionAdapter()
            {
                public void widgetSelected(SelectionEvent e)
                {
                    Thread thread = new Thread()
                    {
                        public void run()
                        {
                            try
                            {
                                final ServerSocket serverSocket = new ServerSocket(9000);
                                serverSocket.accept();                            //这里如果要更新界面组件的值,那就必须把代码放在下面的run()方法里
                                //如果不需要更新界面组件的值,那下面的部分就直接删掉没用
                                Display.getDefault().asyncExec(new Runnable()
                                {
                                    public void run()
                                    {
                                        
                                    }
                                });                        }
                            catch(IOException ex)
                            {
                                ex.printStackTrace();
                            }                    }
                    };                thread.setDaemon(true);
                    thread.start();
                }
            });
      

  3.   

    因为accept函数,一直都在等待数据。
      
       所以应该放在一个死循环里面,这样比较符合实际...