我用NetBeans建立了一个jframe窗口!
添加了,一个按钮,一个文本,一个label
程序大概:
import java.io.*;
import java.net.*;
//类
Socket socket;
ServerSocket server;
DataInputStream in;
DataOutputStream out;
//接着是触发事件
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
    int port;
    port=Integer.valueOf(tf3.getText());
        try {
            server = new ServerSocket(port);                                        
             socket=server.accept(); 
             in=new DataInputStream(socket.getInputStream());
             out=new DataOutputStream(socket.getOutputStream());
            l8.setText("连接状态:主机建立!");
            
        } catch (IOException ex) {
            l8.setText("连接状态:失败!");
        }
}
但是不能建立,也不抛出错误,我很困惑,求解答,在线等,100分

解决方案 »

  1.   

    这是主机端,一个程序里的,两个按键,这个建主机,然后在点击“连接主机”和主机端连在一起!
    不过建立主机的时候就已经开始出现问题了,客户端倒是没什么问题!
    好像是socket=server。accpet()
    那有问题,可不监听,有没办法,传递信息!
      

  2.   

    while(true){ 
     server = new ServerSocket(port);   
      socket=server.accept();  
      in=new DataInputStream(socket.getInputStream());
      out=new DataOutputStream(socket.getOutputStream());
      l8.setText("连接状态:主机建立!");
    }
    试下
      

  3.   

    写错了
      server = new ServerSocket(port);
      while(true){   
      socket=server.accept();  
      in=new DataInputStream(socket.getInputStream());
      out=new DataOutputStream(socket.getOutputStream());
      l8.setText("连接状态:主机建立!");
    }
      

  4.   

    这是全部的代码了!我觉得不是很难,可是还是很有问题啊!package 网络课程设计2;
    import java.net.*;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;public class game extends javax.swing.JFrame {
    Socket socket;
    ServerSocket server;
    DataInputStream in;
    DataOutputStream out;
        
        public game() {
            initComponents();
        }   
       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        int port;
        port=Integer.valueOf(tf3.getText());
            try {
                server = new ServerSocket(port);                                        
                while(true) {
                socket=server.accept(); 
                 in=new DataInputStream(socket.getInputStream());
                 out=new DataOutputStream(socket.getOutputStream());
                l8.setText("连接状态:主机建立!");
                }
            } catch (IOException ex) {
                l8.setText("连接状态:失败!");
            }
    }private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        int port=Integer.valueOf(tf5.getText());
            String s=tf4.getText();
            try {
                InetAddress address = InetAddress.getByName(s);
                //InetSocketAddress link=new InetSocketAddress(address,port);
                try {
                     socket=new Socket(address,port);
                     in=new DataInputStream(socket.getInputStream());
                     out=new DataOutputStream(socket.getOutputStream());
                    l8.setText("连接状态:已连接");
                } catch (IOException ex) {
                    l8.setText("连接状态:连接失败");
                }
            } catch (UnknownHostException ex) {
             l8.setText("连接状态:找不到主机");
            }
        
        
    }                                        private void formComponentShown(java.awt.event.ComponentEvent evt) {                                    
    // TODO add your handling code here:
        InetAddress f;
            try {
                f = InetAddress.getLocalHost();
                tf2.setText(f.getHostAddress());
                tf1.setText(f.getHostName());
            } catch (UnknownHostException ex) {
                Logger.getLogger(game.class.getName()).log(Level.SEVERE, null, ex);
            }
               
    }                                   private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        String s;
        s=tz2.getText();
            try {
                out.writeUTF(s);                                        
            } catch (IOException ex) {
                Logger.getLogger(game.class.getName()).log(Level.SEVERE, null, ex);
            }
    }private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        String s;
        int a,b;
            try {
                s = in.readUTF();
                tz1.setText(s);
               
            } catch (IOException ex) {
               t.append("对方还没有出招!");
            }
        
    }                                            /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new game().setVisible(true);
                }
            });
        }              }
      

  5.   

    这个我试过了,不行啊!
    在逻辑上我实在想不出我错在哪了!输入端口号,建立主机,监听,阻塞等待,然后从开一个同样的程序,输入地址,端口,连接!然后用输入输出流读写!
    我吧socket=server。accept删了,就能够建立,可是删了,我还怎么传信息!
      

  6.   

    你好像没写时间响应
    给你个例子public void actionPerformed(ActionEvent e) {
     if    (e.getActionCommand() == "新建") {
      newFiel();
     } else if  (e.getActionCommand() == "打开") {
       &nbsp,白洁; openFile();
     } else if  (e.getActionCommand() == "保留") {
       saveFiel();
    &nbsp,白洁;} else if  (e.getActionCommand() == "退出") {
      System.exit(0);
     }
    }
      

  7.   

    这个应该和我写的应该没差:只是换了一种写法的样子!当按下=“新建”执行xxx,按下=“打开”执行xxx
    我想应该不是事件的问题,还是我建主机有问题!
      

  8.   

    我不用线程,我不会!
    窗口的接口不是接上了吗??extends javax.swing.JFrame 
    监视器是什么??求解答,谢谢!
      

  9.   

    一个小例子import java.awt.*;
    import java.awt.event.*;
    public class TestActionEvent1
    {
    public static void main(String[] args) 
    {
       Frame f=new Frame("test");
       f.setBounds(300,300,200,200);
       Button b1=new Button("start");
       Button b2=new Button("stop");
       Monitor bh=new Monitor();//自定义的类
       b1.addActionListener(bh);
       b2.addActionListener(bh);
       b2.setActionCommand("game over");
       f.add(b1,BorderLayout.CENTER);
       f.add(b2,BorderLayout.NORTH);
      
       f.pack();//Frame.pack()这个方法的作用就是根据窗口里面的布局及组件的preferedSize来确定frame的最佳大小。
       f.setVisible(true);
      
    }
    }
    class Monitor implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
       System.out.println("don't press me!!!!!!!!!!!!"+e.getActionCommand());
    }
    }
    http://blog.hi.mop.com/GetEntry.do?id=7731367&owner=27610992
      

  10.   

    监听是有了,NetBeans都集成帮我写好了!然后
    那个我刚刚不用gui试了一下,我发现即使单纯的写:
    ublic class f2 {
    public static void main(String arg[])
    {
            try {
                ServerSocket server = new ServerSocket(8000);
                Socket socket;
                socket=server.accept();
                DataInputStream in=new DataInputStream(socket.getInputStream());
                DataOutputStream out=new DataOutputStream(socket.getOutputStream());
                System.out.println("建立成功");
            } catch (IOException ex) {
                System.out.println("建立失败");
            }
        
    }
    }这个也抛出了错误!我也不是第一次写这东西,这段时间也都在用,虽说我自己对自己的要求是一降再降,可现在我发现我好像连主机都建立不对!
      

  11.   

    是吗?
    有问题:运行结果:会显示连接失败!
    抛出了IOException错误!
      

  12.   

    当客户端没有clientSocket连接时,服务器端的socket.accept()方法就会保持监听状态。
    当客户端有个clientSocket访问服务器时,就会产生如下
    while(true){
    Socket clientSocket=socket.accept();
    ...开个线程去处理clientSocket,让socket继续保持监听.
    }
    这个clientSocket与客户端的主机是相关的,
    然后要为这个clentSocket开一个线程去处理。
    你代码的错误在你写clientSocket时写成同步了,Socket编程要写成异步。
    在异步线程中,读写操(服务器端)作要在一个循环中进行,这样服务器端对应的socket才会不停的接收客户端的数据。
      

  13.   

    算了,没人管,我自己在看看吧!分就给zfq642773391,陪我那么久,谢谢你了!
      

  14.   

         你没客户端吧,只有客户端发起连接,socket=server.accept();  后面才会执行,要不是只能阻塞在那里!!你可以在写个类运行客户端,先运行服务端进程,在运行客户端进程。这下应该没问题了!!