I want to recode this pseudo code into Java:
- Use sockets (low level tool for sending raw data on a network / to a server).
- Create a new TCP socket.
- Use the socket to make a connection to Chatango.
- Send the authorization string to Chatango to join a room.
- Send a message.
- 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?