Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
阿新 • • 發佈:2017-08-27
next style exception void println clas can color imp
1 import java.io.PrintStream; 2 import java.net.Socket; 3 import java.net.ServerSocket; 4 import java.util.Scanner; 5 6 public class Server 7 { 8 public static void main(String[] args) 9 throws Exception 10 { 11 ServerSocket ss = new ServerSocket(30000); 12 Socket socket = ss.accept();View Code13 PrintStream ps = new PrintStream(socket.getOutputStream()); 14 ps.println("服務器的第一行數據"); 15 ps.println("服務器的第二行數據"); 16 // 關閉socket的輸出流,表明輸出數據已經結束 17 socket.shutdownOutput(); 18 // 下面語句將輸出false,表明socket還未關閉。 19 System.out.println(socket.isClosed());20 Scanner scan = new Scanner(socket.getInputStream()); 21 while (scan.hasNextLine()) 22 { 23 System.out.println(scan.nextLine()); 24 } 25 scan.close(); 26 socket.close(); 27 ss.close(); 28 } 29 }
運行上面代碼,出現下面錯誤:
ServerSocket ss = new ServerSocket(30000);//錯誤在這一行,表明30000端口被占用,原因是可能有程序在占用30000端口,你可以換個端口試一試!
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind