2012-01-01 38 views
2

**以下是兩個對等體之間聊天的簡單代碼。據我所知,代碼做它應該有的,但我面臨着解決這個SocketException錯誤的困難。 這段代碼工作正常,直到我做了最後的修改。現在我發現很難追查這個錯誤。查找隱藏導致java.net.SocketException的錯誤:無法識別的Windows套接字錯誤:0:無法綁定

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import java.util.*; 
import java.net.*; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class UDPchat extends Thread implements ActionListener 
{ 
    JFrame f; 
    JPanel p; 
    JTextField text; 
    JButton b; 
    JTextArea ta; 
    private final static String newline = "\n"; 
    private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

    int porttor=7777; // port to send/receive datagrams on 
    //int porttos=5555; 
    String remoteIPaddress= ("127.0.0.1");  // IP to send datagrams 

    public UDPchat() throws Exception 
    { 
     start();  // start thread to receive and display datagrams 
     f=new JFrame("CHAT WINDOW"); 
     p= new JPanel(); 
     text = new JTextField(); 
     text.setColumns(25); 

     b = new JButton("SEND"); 

     b.addActionListener(this); 

     ta = new JTextArea("Chat messages",20,50); 

     ta.setEditable(false); 

     f.setLayout(new FlowLayout()); 

     p.add(text,BorderLayout.NORTH); 

     p.add(b,BorderLayout.SOUTH); 

     f.setLayout(new BorderLayout()); 

     f.add(ta,BorderLayout.NORTH); 

     f.add(p,BorderLayout.SOUTH); 

     f.setSize(400, 400); 

     f.setVisible(true); 
    } 

    @Override 

    public void actionPerformed(ActionEvent e) 
    { 

     try 
     { 

      String s = text.getText(); // read a String 

      text.setText(" "); 

      ta.append(newline); 

      ta.append(s); 

      //System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s); 

      byte[] data = s.getBytes(); // convert to byte array 

      DatagramSocket theSocket = new DatagramSocket(); // create datagram socket and the datagram 

      DatagramPacket theOutput = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5555); 

      theSocket.send(theOutput);  

     } 

     catch(Exception et) 
     { 

      System.out.println("Error sending datagram" + et); 

     } 

    } 

    // thread run method, receives datagram and display contents as a string 
    public void run()     
    { 

     try 
     { 

      // open DatagramSocket to receive 

      DatagramSocket ds = new DatagramSocket(7777); 

      // loop forever reading datagrams from the DatagramSocket 

      while (true) 
      { 

       byte[] buffer = new byte[65507]; // array to put datagrams in 

       DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // DatagramPacket to hold the datagram 

       try { 

        ds.receive(dp); 

       } catch (IOException e) { 

        // TODO Auto-generated catch block 

        System.err.println("chat error2 " + e); 

       } // wait for next datagram 

       String s = new String(dp.getData(),0,dp.getLength()); // get contents as a String 

       System.out.println("UDP datagram length " + s.length()+ " from IP " + dp.getAddress() + " received: " + s); 

       ta.append(newline); 

       ta.append(s); 

      } 

     } 

     catch (SocketException se) 
     { 

      System.err.println("chat error1 " + se); 

     } 

     //catch (IOException se) {System.err.println("chat error2 " + se);} 

     //System.exit(1); // exit on error 

    } 

    public static void main(String args[]) throws Exception 
    { 

     UDPchat c=new UDPchat(); 

    } 

} 
+0

你可能真的要改善的錯誤處理,COS,現在你是隱藏的堆棧跟蹤 – Dapeng 2012-01-01 08:55:29

+0

也許你應該更改端口 – olyanren 2012-01-01 09:03:51

+0

請提供堆棧跟蹤,以便更好地理解錯誤。問候 – 2012-01-01 09:05:44

回答

1

選中此項,在代碼中創建兩種類型的DatagramSockets,嘗試提供兩個相同的7777端口。一個在您的actionPerformed()方法爲:

DatagramSocket theSocket = new DatagramSocket(); // create datagram socket and the datagram 

和一個運行中的方法爲:

// open DatagramSocket to receive 

DatagramSocket ds = new DatagramSocket(7777); 

奕構造函數創建一個數據報套接字並將其綁定到任何可用的端口上的本地主機,第二個嘗試到端口7777.嘗試初始化兩個相同的端口號,這將爲您排序的東西。

希望這可能有助於某種方式。

問候

1

你確定有其他應用程序綁定到該端口(7777)嗎? 您是否嘗試過其他端口具有相同的結果?

而且請確保沒有殺毒軟件或防火牆運行,這也可以拒絕綁定到端口。

+0

感謝您的幫助。 – 2012-01-06 07:20:27