日期:2014-05-20  浏览次数:20769 次

关于ServerSocket的一些疑问
一个是客户端,一个是服务端,我在我的eclipse上调试怎么总提示错误呢?我是win7系统,小白先谢谢大家帮忙解围啦。


客户端:

Java code

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;


public class ChatClient extends Frame {

    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();

    public static void main(String[] args) {
        new ChatClient().launchFrame();
    }

    public void launchFrame() {
        setLocation(400, 300);
        this.setSize(300, 300);
        add(tfTxt, BorderLayout.SOUTH);
        add(taContent, BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        tfTxt.addActionListener(new TFListener());
        setVisible(true);
        connect();
    }
    
    public void connect(){
        try {
            Socket s = new Socket("localhost",1069);
System.out.print("connected!");            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    private class TFListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            String s = tfTxt.getText().trim();
            taContent.setText(s);
            tfTxt.setText("");
        }
        
    }
}





下面这个是服务端:

Java code

import java.io.*;
import java.net.*;

public class ChatServer {

    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(1069);
            while(true){
                Socket s = ss.accept();
System.out.println("a client connected!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}




提示错误:
java.net.SocketException: select failed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at ChatServer.main(ChatServer.java:10)


我的是eclipse Version: 3.7.2 



------解决方案--------------------
端口被占用了?
------解决方案--------------------
从代码上没看出什么异样,是不是端口被占用,或者用户权限不够
------解决方案--------------------
netstat查询下你用的端口是否已被占用