2017-07-26 70 views
1

我寫了這兩個類,一個用於客戶端,另一個用於服務器。當我運行它們兩個時,出現以下錯誤:JAVA:java.net.BindException:已在使用的地址:JVM_Bind

java.net.BindException: Address already in use: JVM_Bind... 

問題是什麼?另外我使用TCPview軟件,並且只有兩個使用相同端口的java.exe。這兩個java.exe進程屬於應用程序。

下面是代碼:

服務器代碼

/** 
* 
* @author casinoroyal 
*/ 
public class server { 
    public static ServerSocket socket1; 
    public static void main(String[] args) { 
     try { 
      socket1 = new ServerSocket(1254); 
      String request=""; 
      Socket mylink=socket1.accept(); 
      System.out.println("server feels====="); 
      DataInputStream input= new DataInputStream(mylink.getInputStream()); 
      DataOutputStream output=new DataOutputStream(mylink.getOutputStream()); 
      Scanner chat=new Scanner(System.in); 

      while(!request.equals("QUIT")){ 
       request=input.readUTF(); 
       output.writeUTF(chat.next()); 
      } 

      socket1.close(); 
     } catch (IOException ex) { 
      System.out.println(ex); 
     } 
    } 
} 

客戶端代碼

package javaapplication9; 
import java.net.*; 
import java.io.*; 
import java.util.*; 
public class client { 
    //main 
    public static void main(String[] args) { 
     System.out.println("client want to be connected"); 
     try { 
      Socket mysock = new Socket(InetAddress.getLocalHost(),1254);    
      System.out.println("client has been connected"); 
      DataInputStream input = new DataInputStream(mysock.getInputStream()); 
      DataOutputStream output = new DataOutputStream(mysock.getOutputStream()); 
      String reque=""; 
      Scanner scan1=new Scanner(System.in); 
      String sendmsg=scan1.next(); 

      while(!reque.equals("QUIT")){ 
       output.writeUTF (sendmsg); 
       reque=input.readUTF(); 
      } 

      mysock.close(); 
     } catch (IOException ex) { 
      System.out.println("client rejected"+ex); 
     } 
    } 
} 
+0

可能也綁定您的服務器應用程序原因可能是有兩臺服務器正在運行。如果需要,使用tcpview本身並關閉兩個java.exe實例。然後重試,首先運行服務器,然後再運行客戶端。 「 – Dota2

回答

-1

What is the problem? Also I use TCPview software and there were just two java.exe that use the same port. These two java.exe processes belong to the apps.

這是你的問題。

您試圖在計算機的同一端口綁定2個套接字,並且無法在同一臺計算機的同一端口上綁定2個套接字。

這可能是因爲你有一個現有的過程,是在端口1254(也許您的服務器應用程序的實例)聽,而你試圖運行它試圖在端口1254

+0

」您不能在同一臺計算機的同一端口綁定2個套接字。「其實你可以,只要他們都綁定到不同的IP地址,並且這些都不是0.0.0.0。 – EJP

+0

我認爲你的權利..當我只運行服務器代碼(無需運行客戶端代碼),我看到兩個java.exe運行在同一端口...我能做些什麼?當我殺死這些文件之一wth TCPview兩個java.exe都被殺死了......我該怎麼辦? –

+0

這個代碼有沒有更正? –

相關問題