2017-10-13 121 views
-2

我想创建一个使用UDP的客户端和服务器聊天程序。我遵循一个使用TCP编写类似程序的教程,并尝试将我的知识转化为使用UDP以类似的方式创建一个。在Java中的UDP聊天

我已经完成了一个客户端和服务器端都显示没有错误,并会运行,但一旦运行既不会消息对方或接收消息...有人可以帮助我看看我做错了什么?用于发送消息

服务器端:

try{ 
     //creates the packet to be sent 
     byte[] buf = new byte[256]; 
     String msgout = serverText.getText().trim(); 
     buf = msgout.getBytes(); 

     //uses the socet.receive method to get the packet to retrieve information to send 
     DatagramPacket packet = new DatagramPacket(buf, buf.length); 
     ss.receive(packet); 
     InetAddress address = packet.getAddress(); 
     int port = packet.getPort(); 

     //uses packet information to create and send packet 
     DatagramPacket packetSend = new DatagramPacket(buf, buf.length, address, port); 
     ss.send(packetSend); 

     //Displays the message in the chat area and clears the text area 
     serverArea.setText(serverArea.getText().trim()+"\n Server: "+msgout); 
     serverText.setText(""); 

    }catch (Exception e){ 

    } 

,然后主要用于设置插座和接收/打印:

String msgin = ""; 
    try{ 

     ss = new DatagramSocket(1420); // Sets socket at 1420 
     byte[] buf = new byte[256]; 
     DatagramPacket packet = new DatagramPacket(buf, buf.length); 
     ss.receive(packet); //Receives the packet from the socket 


     //Converts the byte array into a string 
     String clientMsg = new String(packet.getData(), 0, packet.getLength()); 

     while(!msgin.equals("exit")){ 
      //Displays the message 
      msgin = clientMsg; 
      serverArea.setText(serverArea.getText().trim()+"\n Client: "+msgin); //displays client message 

     } 

    }catch(Exception e){ 

    } 

下面是客户端代码,生病结合其发送和接收区域合并为一个区块:

try{ 
     //Creates the message out using the known socket that the Server creates and the known local address 
     String msgout = clientText.getText().trim(); 
     sendBuf = msgout.getBytes(); 
     InetAddress address = InetAddress.getLocalHost(); 
     DatagramPacket sp = new DatagramPacket(sendBuf, sendBuf.length, address, 1420); 
     s.send(sp); 

     //Displays the text and clears the text field 
     clientchat.setText(clientchat.getText().trim()+"\n Server: "+msgout); 
     clientText.setText(""); 


    }catch (Exception e){ 

    } 
String msgin = ""; 

    try{ 
     //Creates a socket 
     DatagramSocket s = new DatagramSocket(); 

     //Receives the message from the server 
     byte[] buf = new byte[256]; 
     DatagramPacket rp = new DatagramPacket(buf, buf.length); 
     s.receive(rp); 

     //Converts byte array to message 
     String clientMsg = new String(rp.getData(), 0, rp.getLength()); 

     while(!msgin.equals("exit")){ 
      //Displays the message 
      msgin = clientMsg; 
      clientchat.setText(clientchat.getText().trim()+"\n Server: "+msgin); //displays client message 

     } 


    }catch (Exception e){ 

    } 

任何帮助和提示将不胜感激!

+1

如果你有空的catch块,当然你不会看到错误。 – Kayaman

回答

0

如果没有任何事情发生,并且您无法发送/接收消息,则可能是有异常正在生成。

但是,由于您有一个try-catch块来捕获所有异常,并且什么都不做,所以如果抛出异常,您将不知道抛出了什么异常。

不是简单地忽略例外,你至少应该打印他们的原因。

在您的catch语句中添加以下内容,您将能够更轻松地进行调试。

e.printStackTrace(); 
+0

不幸的是,这没有做任何事情,我的程序,它仍然没有任何东西。即时消息只是不得不放弃它,我想,并尝试从新开始。 – MDSasquatch