0

I want to recode this pseudo code into Java:

  1. Use sockets (low level tool for sending raw data on a network / to a server).
  2. Create a new TCP socket.
  3. Use the socket to make a connection to Chatango.
  4. Send the authorization string to Chatango to join a room.
  5. Send a message.
  6. Close the connection to Chatango.

I already tried to recode it, but it's not working or I don't know if I am doing it right.

I already have code in Python, Ruby and PHP, but I want to code it 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 would be: Hi, I'm connecting via (Python or Ruby or PHP); that message will be send to this 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 am connecting to 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 to " + addr);



        PrintWriter out =new PrintWriter(sock.getOutputStream(), true);
        BufferedReader in =  new BufferedReader(new           InputStreamReader(sock.getInputStream()));
        //Send Authorization to deinos
        out.println("bauth:deinos\0");
        //Print the String      
        out.println(str);

        //Closing all Connections        
            out.flush();
            in.close();
            out.close();
            sock.close();

        }
    }

but it still doesn't posting string to http://deinos.chatango.com/ are there problem with my code or am I missing something?

mathematics
  • 13
  • 1
  • 7

1 Answers1

1

as per this answer on escape sequence in java the correct escape sequence for NUL character (python \x00) is \0 or \u0000

so the equivalent "bauth:deinos\x00" would be in java "bauth:deinos\0"

by the way to have equivalent functionality to the original code

in java the lines

String server = "www.deinos.chatango.com";
String str = "Hi,I am connecting to chatango using java :)";
int port = 80;

should be replaced by

String server = "s14.chatango.com";
String str = "Hi,I am connecting to chatango using java :)\r\n\0";
int port = 443;

edit after the modification missing also \r\n\0

Community
  • 1
  • 1
Xavier Combelle
  • 10,968
  • 5
  • 28
  • 52
  • Hello Mr.Xavier thanks for answering my question ! :) I already edited my code like you instructed and I change my code. But it still doesn't posting message to http://deinos.chatango.com/ the only output to my IDE eclipse would be this Connected to s14.chatango.com/208.93.230.131 – mathematics Mar 21 '15 at 11:33
  • I modified my answer to solve yourcurrent problem (which is quite different to your original one) it is because you don't follow the same protocol (missing `\r\n\0`) – Xavier Combelle Apr 10 '15 at 05:57
  • Thanks Mr.Xavier , I just found a way how to receive message to chatango still I can't post the message I am analyzing my code and if I can't do it I will just going to update my current code here.Your Advice was really helpfull specially the escape sequence :) – mathematics Apr 10 '15 at 08:35