CyberCodes

Reminders sa mga rehistradong myembro:

•ang lengwahe na gagamitin ay pawang english at tagalog lamang

•Bawal po dito ang pakikipag away

•Bawal ang mga shorcut na mga words
Example: txt, Pwd, at iba pa

•Bawal mababastos at masasakit na salita o anu mang makakasakit
ng damdamin ng isang myembro ng forum na ito

•use the "like" button if the post of the following user you think are usefull


Sample Socket

Go down

Admin Post Sample Socket

Post by princeterror on Sat Sep 22, 2012 4:39 am

Code:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class JavaApplication2 {
        private static Socket socket = new Socket();
        private static InputStream is;
        private static OutputStream os;
       
        public static void main(String[] args) {
                try {
                        //check if socket is connected, close if true
                        if(socket.isConnected()) socket.close();
                        /*create a socket
                                parameters: host, port
                        */
                        socket = new Socket("connect.kolipri.com", 5222);

                        is = socket.getInputStream();
                        os = socket.getOutputStream();

                        System.out.println("Connected to server");
                        //send packet
                        os.write("<stream to='amazilia' xmlns='jabber:client' version='1.0'>..".getBytes());
                        os.flush();
                       
                        StringBuilder sb = new StringBuilder();
                        int c = 0;
                        //read packets
                        while (((c = is.read()) != '\n') && (c != -1) && (is.available() != 0)) {           
                           sb.append((char)c);
                        }
                        System.out.println("Received:");
                        //print receive packet to console
                        System.out.println(sb.toString()+">");
                        socket.close(); //close socket
                } catch (Exception ex) {
                        System.out.println("not connected.");
                }
        }
}

output:
Connected to server
Received:
<?xml version="1.0"?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="amazilia" version="1.0" id="9b1ba7205caaaf96"><stream:features xmlns:stream='http://etherx.jabber.org/streams'><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism></mechanisms></stream:features>

pangita na lang kamo xml parser para sa pag process sang packets
avatar
princeterror
Universal Moderator
Universal Moderator

Posts : 272
Credits : 11465
Fame : 81
Join date : 2012-06-07
Age : 27

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum