在Java UDP套接字客户端中为Memcached

时间:2017-06-01 15:35:46

标签: java sockets networking udp memcached

我有一个本地Memcached实例,并尝试通过基本的Java TCP和UDP套接字API访问它。 TCP客户端工作正常,但UDP客户端总是抛出异常。

java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
    at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:144)
    at java.net.DatagramSocket.receive(DatagramSocket.java:812)
    at test.Test.udp(Test.java:71)
    at test.Test.main(Test.java:10)

无论我设置什么超时。

这是我正在运行的代码。我是套接字编程的新手请帮助解决问题。

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class Test {
    public static void main(String [] args) {
        //tcp();
        udp();
    }

    private static void tcp(){
        String serverName = "127.0.0.1";
        int port = 11211;
        try {
            System.out.println("Connecting to " + serverName + " on port " + port);
            Socket client = new Socket(serverName, port);

            System.out.println("Just connected to " + client.getRemoteSocketAddress());
            OutputStream outToServer = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outToServer);

            out.writeBytes("stats\r\n");
            InputStream inFromServer = client.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);

            System.out.println("Server says " + in.readLine());

            client.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private static void udp(){
        DatagramSocket socket = null ;

        try
        {
            InetAddress host = InetAddress.getByName("localhost") ;
            socket = new DatagramSocket() ;
            byte [] data = "stats\r\n".getBytes() ;
            DatagramPacket packet = new DatagramPacket( data, data.length, host, 11211 ) ;
            socket.send( packet ) ;
            socket.setSoTimeout( 2000 ) ;
            packet.setData( new byte[1024] ) ;
            socket.receive( packet ) ;
            System.out.println( new String(packet.getData()) ) ;

        }
        catch( Exception e )
        {
            System.out.println( e ) ;
        }
        finally
        {
            if( socket != null )
                socket.close() ;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

根据memcached文档(https://github.com/memcached/memcached/blob/master/doc/protocol.txt),在使用udp协议发送数据时,应发送8字节标头,然后发送数据。 与TCP协议格式相同。标题结构如下:

  • 0-1申请ID
  • 2-3序号
  • 4-5此消息中的数据报总数
  • 6-7保留供将来使用;必须为0

发送数据时添加标头字节。像这样:

    try
    {
        InetAddress host = InetAddress.getByName("localhost") ;
        socket = new DatagramSocket() ;

        byte [] data = "stats\r\n".getBytes() ;
        ByteBuffer buffer = ByteBuffer.allocate(50);
        buffer.putShort((short)0);
        buffer.putShort((short) 0x0000;);
        buffer.putShort((short) 0x0001;);
        buffer.putShort((short) 0x0000;);
        buffer.put(data);
        DatagramPacket packet = new DatagramPacket( buffer.array(), buffer.array().length, host, 11211 ) ;
        socket.send( packet) ;
        socket.setSoTimeout( 2000 ) ;
        packet.setData( new byte[1024] ) ;
        socket.receive( packet ) ;
        System.out.println( new String(packet.getData()) ) ;

    }
    catch( Exception e )
    {
       e.printStackTrace();
    }
    finally
    {
        if( socket != null )
            socket.close() ;
    }