2016-11-21 26 views
0

在我的服務器客戶端程序中,UDP服務器收到消息,將消息顯示給控制檯並從客戶端再次發送給客戶端。UDP聯網中的綁定錯誤

這裏是我的服務器,它需要在命令行它的名字輸入

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

class Stest implements Runnable { 
private String str[]; 
DatagramSocket sock; 
DatagramPacket pack; 
String Sname; 

Stest(String str[]) throws SocketException, UnsupportedEncodingException, IOException { 
    this.str = str; 
    this.Sname = str[0]; 
    System.out.println(Sname); 
    new Thread(this).start(); 
} 

public void run() { 
    while (true) { 
     byte[] data = new byte[1024]; 
     try { 
      sock = new DatagramSocket(5050); 
     } catch (SocketException ex) { 
      Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     pack = new DatagramPacket(data, data.length); 
     try { 
      sock.receive(pack); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String s = null; 
     try { 
      s = new String(pack.getData(), "UTF-8"); 
      System.out.println(s); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     DatagramPacket dp = null; 
     try { 
      dp = new DatagramPacket(s.getBytes("UTF-8"), s.getBytes("UTF-8").length); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     dp.setPort(1); 
     dp.setAddress(pack.getAddress()); 
     try { 
      sock.send(dp); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

} 

public class test{ 
public static void main(String[] args) { 
    try { 
     Stest stest= new Stest(args); 

    } catch (IOException ex) { 
     Logger.getLogger(test.class 
       .getName()).log(Level.SEVERE, null, ex); 
    } 
} 
} 

這裏是我的客戶是它的名字,監聽端口,服務器IP地址和服務器名稱併發送消息給服務器再次從服務器接收該消息並將該消息顯示給控制檯。

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

class UDPClient { 
String username; 
String listening_port; 
String serverAddress; 
String servername; 
public UDPClient(String username, String listening_port, String serverAddress, String servername) { 
    this.username = username; 
    this.listening_port = listening_port; 
    this.serverAddress = serverAddress; 
    this.servername = servername; 
} 
} 
class ClientListen extends UDPClient implements Runnable { 
Thread t; 
DatagramSocket clientlistensocket; 
DatagramPacket clientlistenpacket; 
byte[] data; 
public ClientListen(String username, String listening_port, String serverAddress, String servername) { 
    super(username, listening_port, serverAddress, servername); 
    try { 
     clientlistensocket = new DatagramSocket(Integer.parseInt(listening_port)); 

    } catch (Exception e) { 
     System.out.println(e + " ClientListen"); 
    } 
    t = new Thread(this, "ClientListen"); 
    t.start(); 
} 

public void go() { 
    String messag = new String(clientlistenpacket.getData()); 
    System.out.println(messag); 
} 

void processing() { 
    data=new byte[1024]; 

    clientlistenpacket = new DatagramPacket(data,data.length); 

    try { 
     clientlistensocket.receive(clientlistenpacket); 
     go(); 
    } catch (Exception e) { 
     System.out.println(e + " ClientlistenProcessing"); 
    } 
} 
public void run() { 
    while (true) { 
     try { 
      processing(); 
     } catch (Exception e) { 
      System.out.println(e + "Processing+run"); 
     } 
    } 
} 

} 
class ClientSpeak extends UDPClient { 

DatagramPacket clientspeakpacket; 
DatagramSocket clientspeaksocket; 
InetAddress ipAddress; 
Thread t; 

public ClientSpeak(String username, String listening_port, String serverAddress, String servername) { 
    super(username, listening_port, serverAddress, servername); 

    try { 

     clientspeaksocket = new DatagramSocket(); 

    } catch (Exception e) { 
     System.out.println(e + "ClientSpeak"); 
    } 
    try { 

     String firstmessage = "Via:" + servername + "To:" + servername + "From:" + username + "Port:" + listening_port; 
     byte[] firstdata = firstmessage.getBytes(); 
     ipAddress = InetAddress.getByName(serverAddress); 

     clientspeakpacket = new DatagramPacket(firstdata, firstdata.length, ipAddress, 5050); 
     clientspeaksocket.send(clientspeakpacket); 
    } catch (Exception e) { 
     System.out.println(e + "Clientspeak"); 
    } 
} 


} 

public class testC { 

public static void main(String[] args) { 
    if (args.length == 4) { 
     String username = args[0].trim(); 
     String listening_port = args[1].trim(); 
     String serverAddress = args[2].trim(); 
     String servername = args[3].trim(); 

     ClientSpeak clientSpeak = new ClientSpeak(username, listening_port, serverAddress, servername); 
     ClientListen clientListen = new ClientListen(username, listening_port, serverAddress, servername); 
    } 
} 
} 

但它始終是顯示錯誤,如:

S 
Via:STo:SFrom:nahPort:1                                                                                                                                                                                                                                                           
Nov 21, 2016 7:20:34 PM Stest run 
SEVERE: null 
java.net.BindException: Address already in use: Cannot bind 
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method) 
at java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:84) 
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93) 
at java.net.DatagramSocket.bind(DatagramSocket.java:392) 
at java.net.DatagramSocket.<init>(DatagramSocket.java:242) 
at java.net.DatagramSocket.<init>(DatagramSocket.java:299) 
at java.net.DatagramSocket.<init>(DatagramSocket.java:271) 
at Stest.run(test.java:41) 
at java.lang.Thread.run(Thread.java:745) 

我應該怎麼辦?

+0

您省略了最重要的細節,這是堆棧跟蹤的第一部分。沒有這個,我們無法幫助你。請編輯您的帖子幷包含完整的堆棧跟蹤。 –

+0

應該有一些消息以及堆棧跟蹤。像「端口已經在使用」可能? – bradimus

回答

1

的問題是,在while循環內的服務器,你不斷嘗試的一個新的數據報套接字綁定到端口5050

try { 
    sock = new DatagramSocket(5050); 
} catch (SocketException ex) { 
    Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
} 

第二次,服務器讀取並處理UDP消息後,它會嘗試綁定,它會得到一個異常。

您需要在while循環之前創建並綁定UDP套接字一次,然後在while循環中使用該套接字進行整個通信。