简体   繁体   中英

Send message using sockets and XMPP

I'm learning Java Sockets and want to try implement XMPP library using only Sockets, but I can't understand how to do it. I read RFC, but didn't understand anything.

I want to implement those features:

  • Send/receive messages
  • Status of users
  • All contacts

As I know this means that I can successfully proceed sending messages to the server, but HOW?

public static void main(String[] args) {
        String connect = "<?xml version='1.0'?> "
                + "<stream:stream "
                + "to='jabber.ru' version='1.0' "
                + "xmlns='jabber:client' "
                + "xmlns:stream='http://etherx.jabber.org/streams'>";

        String msg = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\">";

        try {
            InetAddress address = InetAddress.getByName(host);
            Socket socket = new Socket(address, port);
            socket.setKeepAlive(true);

            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(connect);
            bw.flush();
            System.out.println("Message sent to the server : " + connect);

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String message = br.readLine();
            System.out.println("Message received from the server : " + message);

            bw.write(msg);
            bw.flush();
            System.out.println("Message sent to the server : " + msg);

            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            message = br.readLine();
            System.out.println("Message received from the server : " + message);

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

Note: I don't want to use any libraries! So Smack and others are not helpful.

After receiving the confirmation of the switch to TLS, you need to switch your socket to TLS and not use clear text anymore. When done, you can simply open the stream and start the negotiation sequence. The server will send your stream features and you will be able to authenticate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM