Exception in thread “main” java.net.BindException: Address already in use - Error in Netbeans only

前端 未结 5 1366
不知归路
不知归路 2021-01-18 14:44

On my machine, the following code compiles within Eclipse but throws an exception within Netbeans. The error message says \"Exception in thread \"main\" java.net.BindExcept

5条回答
  •  既然无缘
    2021-01-18 14:51

    Server.java

    public class SocServer {
    
        public static void main(String[] args) {
            try {
                ServerSocket server = new ServerSocket(5001);
                Socket client = server.accept();
                DataOutputStream os = new DataOutputStream(client.getOutputStream());
                os.writeBytes("Hello Sockets\n");
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Client.java

    public class SocClient {
    
        public static void main(String[] args) {
            try {
                Socket socClient = new Socket("localhost", 5001);
                InputStream is = socClient.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String receivedData = br.readLine();
                System.out.println("Received Data: " + receivedData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    refer above code and it works for me..

提交回复
热议问题