Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Udp服务器客户端java_Java_Network Programming_Udp_Client Server - Fatal编程技术网

Udp服务器客户端java

Udp服务器客户端java,java,network-programming,udp,client-server,Java,Network Programming,Udp,Client Server,我已经编写了UDP服务器客户端程序。我面临的问题是,当我运行服务器程序时,它并没有等待客户端连接。整个代码在运行后一直执行到结束。当我在服务器端执行的中间运行客户端时,客户端从其执行点接收数据。这是我的服务器代码- public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(4321); byte[] sendDa

我已经编写了UDP服务器客户端程序。我面临的问题是,当我运行服务器程序时,它并没有等待客户端连接。整个代码在运行后一直执行到结束。当我在服务器端执行的中间运行客户端时,客户端从其执行点接收数据。这是我的服务器代码-

public static void main(String args[]) throws Exception
  {
     DatagramSocket serverSocket = new DatagramSocket(4321);
        byte[] sendData;
        String sentence = null;
        FileInputStream file = new FileInputStream(new File("E:\\Deepak.txt"));
        InetAddress IPAddress=InetAddress.getByName("localhost");
        BufferedReader in = new BufferedReader(new InputStreamReader(file));
     do{
              while((sentence = in.readLine()) != null)
              {
              Thread.sleep(3000);
              System.out.println(sentence);
              sendData = sentence.getBytes();
              DatagramPacket sendPacket =new DatagramPacket(sendData, sendData.length,IPAddress,9876);
              serverSocket.send(sendPacket);
             }
           }while(true);
  }
这是我的客户端代码-

public static void main(String args[]) throws SocketException, UnknownHostException, IOException 
{
    DatagramSocket clientSocket = new DatagramSocket();
    byte[] receiveData = new byte[1024];
    String sentence ;
    while(true) 
           {                 
             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
             receivePacket.getLength();
             System.out.println(receivePacket.getLength());
             clientSocket.receive(receivePacket);
             sentence = new String( receivePacket.getData());                  
           }
  }

UDP是无连接的,因此其中没有连接。 您只能发送一个数据包(在java中,以一种先发后忘的方式,
send
将数据发送到
Datagram
中指定的端口)或在端口上接收数据包(java中的
receive
将阻塞,直到收到数据包)

所以,如果您希望服务器只在客户端“连接”到UDP时发送数据,则需要在UDP之上实现自己的连接

综上所述:

  • send
    不会等待任何东西,它只会将数据报发送到线路上
  • receive
    将等待数据报到达

有了以上信息,您需要编写自己的协议来保持连接。

如果我想要一个只有在客户端“连接”到UDP时才发送数据的服务器,请告诉我如何在UDP上实现自己的连接。我对socket编程还不熟悉。是否可以将服务器更改为执行
接收
?这是最简单的方法。我的特定应用程序需要实现服务器作为发送方,客户端作为接收方。好的,因此您的客户端需要有两个线程,一个线程将执行
receive
,第二个线程将
send
循环(直到
receive
接收数据报)。服务器将执行相反的操作,它将等待
接收
,只有在获得初始数据报后,才会开始执行
发送