Recode Python or Ruby or PHP to Java Chatango Chatroom connection -
i want recode pseudo code java:
- use sockets (low level tool sending raw data on network / server).
- create new tcp socket.
- use socket make connection chatango.
- send authorization string chatango join room.
- send message.
- close connection chatango.
i tried recode it, it's not working or don't know if doing right.
i have code in python, ruby , php, want code in java:
-python-
import socket s = socket.socket(socket.af_inet, socket.sock_stream) s.connect(('s14.chatango.com', 443)) s.send(b'bauth:deinos\x00') s.send(b"bmsg:hi, i'm connecting via python :3\r\n\x00") s.close()
-ruby-
require 'socket' s = tcpsocket.open('s14.chatango.com', 443) s.print "bauth:deinos\x00" s.print "bmsg:hi, i'm connecting via ruby :3\r\n\x00" s.close()
-php-
$socket = socket_create(af_inet, sock_stream, 0); $s = socket_connect($socket, 's14.chatango.com', 443); socket_write($socket, "bauth:deinos\x00"); socket_write($socket, "bmsg:hi, i'm connecting via php :3\r\n\x00"); socket_close($socket);
the output be: hi, i'm connecting via (python or ruby or php); message send chatroom: http://deinos.chatango.com/.
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.inetaddress; import java.net.socket; public class samp { public static void main(string[] args) throws exception, ioexception{ //the server string server = "s14.chatango.com"; //the message string str = "hi,i connecting chatango using java :)"; //the port int port = 443; //the socket connection socket sock = new socket( server, port ); inetaddress addr = sock.getinetaddress(); //print connected system.out.println("connected " + addr); printwriter out =new printwriter(sock.getoutputstream(), true); bufferedreader in = new bufferedreader(new inputstreamreader(sock.getinputstream())); //send authorization deinos out.println("bauth:deinos\0"); //print string out.println(str); //closing connections out.flush(); in.close(); out.close(); sock.close(); } }
but still doesn't posting string http://deinos.chatango.com/ there problem code or missing something?
as per this answer on escape sequence in java correct escape sequence nul character (python \x00
) \0
or \u0000
so equivalent "bauth:deinos\x00"
in java "bauth:deinos\0"
by way have equivalent functionality original code
in java lines
string server = "www.deinos.chatango.com"; string str = "hi,i connecting chatango using java :)"; int port = 80;
should replaced by
string server = "s14.chatango.com"; string str = "hi,i connecting chatango using java :)\r\n\0"; int port = 443;
edit after modification missing \r\n\0
Comments
Post a Comment