Bitcoin protocol using socket (java) - java

I develop bitcoin protocol using socket, but met some issues. Firstly, when my code connects to bitcoin node, it doesn't give any response. I send version message, but another side node doesn't re-send anything. I think that problem could be that I implement protocol wrong, but I checked and think that it's fine. (2) Secondly, I think that could be impossible connect to bitcoin node using socket, but it's stupid idea. Yea?
One important thing, I coded only socket connection, version sending to bitcoin node, which I found on https://bitnodes.earn.com/nodes/ , and message receiving from node, but it always responses -1.
One more thing, I don't give response from node, but also don't give connection error. (I just say)
Please help me :)
What is wrong that I send version message, but then doesn't give another one from node?
Also, I attach output (connection phases and protocol structure) of console when try to connect to socket.
Connected
f9beb4d976657273696f6e0000000000000000660a75c8397f1101000100000000000000820b905c00000000010000000000000000000000000000000000ffff340e5966208d010000000000000000000000000000000000ffff7f000001208db910e81a17a5dd79102f417572696d61733a302e31362e332f0000000001
Magic:f9beb4d9
Command:76657273696f6e0000000000
Length:00000066
Checksum:0a75c839
Payload:7f1101000100000000000000820b905c00000000010000000000000000000000000000000000ffff340e5966208d010000000000000000000000000000000000ffff7f000001208db910e81a17a5dd79102f417572696d61733a302e31362e332f0000000001
Version:7f110100
Services:0100000000000000
timestamp:820b905c00000000
addr_recv:010000000000000000000000000000000000ffff340e5966208d
addr_from:010000000000000000000000000000000000ffff7f000001208d
nonce:b910e81a17a5dd79
user_agent:102f417572696d61733a302e31362e332f
start_height:00000000
relay:01
Connection end
Main class:
private static String address = "52.14.89.102";
private static int port = 8333;
public static void main(String [] args) throws IOException {
String line = "";
try {
socket = new Socket(address, port);
System.out.println("Connected");
input = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
VersionMessage message = new VersionMessage();
line = Hex.encodeHexString(message.getMessage());
System.out.println(line);
structure(message.getMessage());
out.writeBytes(line);;
} catch(UnknownHostException u) {
System.out.println(u);
} catch (IOException e) {
e.printStackTrace();
}
...
The part of VersionMessage class:
public VersionMessage() {
super();
this.version = 70015;
this.services = 1; // NODE_NETWORK
this.timestamp = System.currentTimeMillis() / 1000;
this.nonce = new Random().nextLong();
this.userAgent = "/Satoshi:0.16.3/";
this.startHeight = 0;
this.relay = true;
contructPayload();
contructMessage();
}
#Override
public void contructPayload() {
addByteArray(dataParser.toInt32(version));
addByteArray(dataParser.toInt64(services));
addByteArray(dataParser.toInt64(timestamp));
addAddr("52.14.89.102", 8333);
addAddr("127.0.0.1", 8333);
addByteArray(dataParser.toInt64(nonce));
addStr(userAgent);
addByteArray(dataParser.toInt32(startHeight));
addByteArray(dataParser.toInt8(relay ? (byte) 1 : (byte) 0));
payload = new byte[payloadBytes.size()];
IntStream.range(0, payloadBytes.size()).forEach(i -> payload[i] = payloadBytes.get(i).byteValue());
}
BitcoinMessage (parent of VersionMessage):
public BitcoinMessage() {
magic = 0xf9beb4d9;;
payloadBytes = new ArrayList<Integer>();
dataParser = new DataParser();
}
public void contructMessage() {
message = new byte[MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE + CHECKSUM_LENGTH + payload.length];
length = payload.length;
byte[] doubleHashedPayload = MessageUtils.doubleHashWithSha256(payload);
MessageUtils.addToByteArray(MessageUtils.intToBytes(magic, MAGIC_LENGTH), 0, MAGIC_LENGTH, message);
MessageUtils.addToByteArray(getCommandName().getBytes(), MAGIC_LENGTH, COMMAND_LENGTH, message);
MessageUtils.addToByteArray(MessageUtils.intToBytes(length, PAYLOAD_LENGTH_SIZE), MAGIC_LENGTH + COMMAND_LENGTH, PAYLOAD_LENGTH_SIZE, message);
MessageUtils.addToByteArray(doubleHashedPayload, MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE, CHECKSUM_LENGTH, message);
MessageUtils.addToByteArray(payload, MAGIC_LENGTH + COMMAND_LENGTH + PAYLOAD_LENGTH_SIZE + CHECKSUM_LENGTH, payload.length, message);
}

Related

How to check if server is running a method in Java HttpServer; getting java.net.UnknownHostException

I am scheduling tasks on servers but I have no idea how to know if any server is currently doing the task. I am looping over servers like this:
for(int i = 0; i < tasksList.size(); i++){
for(URL url : serverURLsList){
if(InetAddress.getByName(url.toString()).isReachable(0)){ //this gives me exception
HttpURLConnection availableServer = (HttpURLConnection) url.openConnection();
availableServer.setDoOutput(true);
ObjectOutputStream oos = new ObjectOutputStream(availableServer.getOutputStream());
oos.writeObject(tasksList.get(i));
oos.close();
tasksList.remove(i);
break;
}
}
}
This is my server for task handling:
public class Serv{
public static void main(String[] args) throws IOException {
int port = 8080;
String host = "192.168.1.116";
System.out.println("Server 1 is up. Host: " + host + " port: " + port);
HttpServer s= HttpServer.create(new InetSocketAddress(host, port), 0);
s.createContext("/", new RootHandler());
s.setExecutor(null);
s.start();
}
}
class RootHandler implements HttpHandler{
private static int clientCounter = 1;
#Override
public void handle(HttpExchange exchange) throws IOException {
System.out.println("\nRoot handler; \n\tclient no. " + clientCounter++);
Task t;
ObjectInputStream ois = new ObjectInputStream(exchange.getRequestBody());
try {
System.out.println("Recieved object:");
t = (Task) ois.readObject();
ois.close();
System.out.println("Array not sorted:");
int[] arr = (int[]) t.getData();
for (int anArr : arr) {
System.out.print(anArr + " ");
}
TaskSolver.solve(t); // need to check if this is runned on the server or server is doing nothing
System.out.println("\nArray sorted!");
for (int anArr : (int[])t.getData()) {
System.out.print(anArr + " ");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String response = "<h1>Server up!</h1>";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
What I really want to achieve is have the information if any server in the list is currently solving the task (the comment in the snippet above shows that place). No idea why getting the exception mentioned in the title because I can reach the server from the web browser. I suppose the method isReachable() is not good for that purpose (because it should tell if the host given is up or not while I want to know if the proper method on the server is currently running). Does somebody know a good solution to do so?
#Edit
According to the comment I need to say that each of my servers (I will have plenty of them) should be doing only one task, that is the purpose of my simulation.
InetAddress.getByName(url.toString()).isReachable(0)){ //this gives me exception
Of course it does. The argument to InetAddress.getByName() is not a URL. Check the Javadoc. It is a hostname.

Akka stream of objects over http

I have got a piece of code (see bellow) which spawns a server that echoes every stream of ByteString it receives from port 6001. The example also defines a client that connects to the server and sends a stream of ByteString containing a list of characters from letter 'a' to 'z'.
My question at this point is, does akka offer a way to send and receive a Stream of objects instead of ByStreams over http? For instance, objects of class Client.
If so, how could I send and receive such a stream of objects? Could you provide me a snippet that shows how to carry it out?
Akka documentation is not user-friendly for non-toy examples...
Thanks for your help
public class TcpEcho {
/**
* Use without parameters to start both client and server.
*
* Use parameters `server 0.0.0.0 6001` to start server listening on port
* 6001.
*
* Use parameters `client 127.0.0.1 6001` to start client connecting to
* server on 127.0.0.1:6001.
*
*/
public static void main(String[] args) throws IOException {
if (args.length == 0) {
ActorSystem system = ActorSystem.create("ClientAndServer");
InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", 6000);
server(system, serverAddress);
client(system, serverAddress);
} else {
InetSocketAddress serverAddress;
if (args.length == 3) {
serverAddress = new InetSocketAddress(args[1], Integer.valueOf(args[2]));
} else {
serverAddress = new InetSocketAddress("127.0.0.1", 6000);
}
if (args[0].equals("server")) {
ActorSystem system = ActorSystem.create("Server");
server(system, serverAddress);
} else if (args[0].equals("client")) {
ActorSystem system = ActorSystem.create("Client");
client(system, serverAddress);
}
}
}
public static void server(ActorSystem system, InetSocketAddress serverAddress) {
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Sink<IncomingConnection, CompletionStage<Done>> handler = Sink.foreach(conn -> {
System.out.println("Client connected from: " + conn.remoteAddress());
conn.handleWith(Flow.<ByteString> create(), materializer);
});
final CompletionStage<ServerBinding> bindingFuture = Tcp.get(system)
.bind(serverAddress.getHostString(), serverAddress.getPort()).to(handler).run(materializer);
bindingFuture.whenComplete((binding, throwable) -> {
System.out.println("Server started, listening on: " + binding.localAddress());
});
bindingFuture.exceptionally(e -> {
System.err.println("Server could not bind to " + serverAddress + " : " + e.getMessage());
system.terminate();
return null;
});
}
public static void client(ActorSystem system, InetSocketAddress serverAddress) {
final ActorMaterializer materializer = ActorMaterializer.create(system);
final List<ByteString> testInput = new ArrayList<>();
for (char c = 'a'; c <= 'z'; c++) {
testInput.add(ByteString.fromString(String.valueOf(c)));
}
Source<ByteString, NotUsed> responseStream = Source.from(testInput)
.via(Tcp.get(system).outgoingConnection(serverAddress.getHostString(), serverAddress.getPort()));
CompletionStage<ByteString> result = responseStream.runFold(ByteString.empty(), (acc, in) -> acc.concat(in),
materializer);
result.whenComplete((success, failure) -> {
if (failure != null) {
System.err.println("Failure: " + failure.getMessage());
} else {
System.out.println("Result: " + success.utf8String());
}
System.out.println("Shutting down client");
system.terminate();
});
}
}
akka.stream.{javadsl,scaladsl}.Framing contains utilities to help you build consistent messages. For example, you can send your messages through Framing.simpleFramingProtocolEncoder(maxLength) to automatically add length information to them. On the other end, Framing.simpleFramingProtocolDecoder(maxLength) will take care of decoding the message according to its enclosed length information.
If you want to manipulate plain objects, you just have to serialize them into a ByteString before sending them through the encoder, and deserialize them from the ByteString after receiving their representation from the decoder.

How to create multiple peer-to-peer chats within client-server messaging program

I am trying to create a messenger program and have successfully set up client-server connections using sockets. However I am finding it difficult to code the process of having several clients communicating simultaneously. Shown in the code below is the methods for the chats that are held within a ClientThread class that regulates the interaction between client and server using threads stored in a shared ArrayList. How would you implement the code for multiple peer-to-peer chats here?
startChat method:
public void startChat()
{
// start the convo!
// first of all the user chooses who to speak to
// starts a loop until user enters a valid username or 'Group'
String line = "";
boolean validCommand = false;
while(validCommand == false)
{
try {
line = in.readLine();
} catch (IOException e) {
System.out.println("Problem reading reply about user chat");
}
if(line.equalsIgnoreCase("Group"))
{
validCommand = true;
chatAll(); // an integer of negative one starts a chat with everyone
}
else
{
synchronized(this){
// find user
for(int i = 0; i < threads.size(); i++)
{
if(threads.get(i) != null && threads.get(i).username != null)
{
if(threads.get(i).username.equals(line)) // means that we have found the index of the thread that the client wants to speak to
{
/*// START : BETWEEN THESE CAPITALISED COMMENTS IS MY ATTEMPT TO INITIATE TWO WAY CHAT
int thisIndex = -1;
for(int j = 0; j < threads.size(); j++) // gets the index of this thread object in the array
{
if(threads.get(j) == this)
{
thisIndex = j;
// out.println(j);
}
}
if(thisIndex != -1)
{
threads.get(i).out.println(username + " is trying to connect");
threads.get(i).processChat(thisIndex); // this is the line causing the problem!
}
// END : BETWEEN THESE CAPITALISED COMMENTS IS MY ATTEMPT TO INITIATE TWO WAY CHAT */
threads.get(i).out.println(username + " is trying to connect");
out.println("Chat with " + threads.get(i).username);
processChat(i);
validCommand = true;
}
// if the command is not group and not a username, it is not valid and we ask the user to re-enter
else if(i == threads.size() - 1)
{
out.println("This command is not valid, please re-enter");
}
}
}
} // end of synchronised bit
} // end of else statement
} // end of while loop
}
allChat method:
void chatAll()
//for the purpose of group chat
{
out.println("Group chat initiated");
boolean d = true;
while(d == true)
{
String message = "";
try {
message = in.readLine();
} catch (IOException e) {
System.out.println("Can't read line from client");
}
if(message.contains("goodbye") == true)
{
d = false;
}
else
{
synchronized(this)
{
for(int j = 0; j < threads.size(); j++)
{
if(threads.get(j) != null)
{
threads.get(j).out.println(username + ": " + message);
}
}
}
}
}
}
processChat method:
void processChat(int i)
//for the purpose of talking to pre-defined user
{
boolean d = true;
while(d == true)
{
String message = "";
try {
message = in.readLine();
} catch (IOException e) {
System.out.println("Can't read message from client");
}
if(message.contains("goodbye") == true)
{
d = false;
}
else {
if(threads.get(i) != null)
{
threads.get(i).out.println(username + ": " + message);
}
}
}
}
Just for good measure and a reference here is the overall client class (confusingly labelled ThreadedClient as opposed to ClientThread haha)
ThreadedClient class:
import java.net.*;
import java.io.*;
public class ThreadedClient implements Runnable {
// client socket
private static Socket clientSocket = null;
//I/O streams to and from the server
private static BufferedReader in = null;
private static PrintStream out = null;
// Input stream to read user input
private static BufferedReader inputReader = null;
private boolean open = true;
public ThreadedClient(String host, int port)
{
startConnection(host, port);
}
public void startConnection(String host, int port)
{
//open up the socket
try {
clientSocket = new Socket(host, port);
} catch (UnknownHostException e) {
System.out.println("The host name '" + host + "' isn't known");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Cannot create socket");
}
// connect I/O streams
try {
in = new BufferedReader(new InputStreamReader(new DataInputStream(clientSocket.getInputStream())));
out = new PrintStream(clientSocket.getOutputStream());
inputReader = new BufferedReader(new InputStreamReader(System.in));
} catch (IOException e) {
System.out.println("Problem connecting streams");
}
// process the chat itself
// the thread deals with input coming in
Thread thread = new Thread(this);
thread.start();
// the loop deals with output
while(open == true)
{
String message;
try {
message = inputReader.readLine();
out.println(message);
if(message.contains("goodbye") == true)
{
open = false;
}
} catch (IOException e) {
System.out.println("Problem sending messages");
}
}
// chat is done, so we can close resources
try {
in.close();
inputReader.close();
out.close();
clientSocket.close();
} catch (IOException e) {
System.out.println("Problem closing resources");
}
}
// run method for sending input out. I imagine this will not be necessary in the GUI implemented version, as we can use
// an action listener for the send function, e.g. one that reads a text field into a output stream everytime the user clicks enter
public void run() {
while(open == true)
{
try {
String response = in.readLine();
if(response.contains("goodbye") == true)
{
open = false;
}
System.out.println(response);
} catch (IOException e) {
System.out.println("Problem recieving messages");
}
}
}
public static void main(String[] args)
{
ThreadedClient socket = new ThreadedClient("localhost", 50000);
}
}
I know that this code may not be as advanced as some others I have seen on this forum as well as DreamInCode and others but I was trying to build it from scratch and have been stuck here for what feels like a millennia. Trawling through the internet has not helped :(
Any suggestions and criticisms would be an absolute God send!
Thanks in advance guys.
OK.
You can do like this: Im focus on Console Application
- Define a class call Message:
class Message
{
public String username; // the sender that send this message to u.So you can reply back to this user
public boolean groupMessage; // this message is group message or not
public String message;
}
Define a global variable: ArrayList messages; to hold all incomming messages.
So when you start chat with a client --> create new Thread to read message from him.When you receive a message . You have to put that message to the array list: messages ( you have to remember to sync it. because it will be invoked by many thread)
synchorized(messages){
messages.add(....); // new message here
}
Then , you create a new Thread to show message & can reply back to the sender. In this read you will pop a message from array list messages & show it.
while(isrunning)
{
synchorized(messages){
if(messages.size()<=0) messages.wait(); // when you receive a new message you have to notify
}
synchorized(messages){
Message msg = messages.get(0);
messages.remove(0);
showmessage_to_ouput(msg); // something like this.
String s = read from input // to reply to this message.
Reply(....)// here you can check if this message is group message--> reply to all,..etc
}
}
P/S: That's a idea :) good luck
I can give you a solution , but you have to implement it
We have:
- Server A, Client B & C. B & C already connected to Server via TCP connection
- The first, client B want to chat with C. So B have to send a message by UDP to server
- 2nd, Server will receive a UDP messages from B ==> Server know which ip & port of B that B connected to Server by UDP. Then server send to C a message (TCP) that contains info about UDP ip:port of B .
- 3rd: Client C will receive that message from server via TCP . So C know ip:port that B is listenning .--> If C accept chat with B . C have to send a UDP message to Server to tell server that C accept to talk with B.
- 4th: Server will receive that message via UDP . So Server also know ip:port of C in UDP.
- 5th : The server will transfer UDP ip:port of C to B via TCP (or UDP if you want).
- 6th: Client B will receive it & know udp ip:port of C. So they can start to chat via UDP protocol now.
IT is call UDP/TCP Hole punching. You can research more about it to implement.
P/S: But this method doesnt work with Symetric NAT

What could be causing this socket not to find a port?

I am trying to create a server that receives UDP packets and responds with UDP messages. However, I can't seem to get it to bind to a port. Here's my constructor for the class:
public UDPServer() throws IOException {
myGUI = new ServerGUI();
myClientList = new ArrayList<ClientInfo>();
DatagramChannel channel = DatagramChannel.open();
mySocket = channel.socket();
//mySocket = new DatagramSocket(null);
//mySocket.setReuseAddress(true);
//mySocket.bind(new InetSocketAddress("localhost", Constants.SERVER_PORT_NUM));
myPortNum = mySocket.getPort();
myIP = mySocket.getInetAddress();
System.out.println(myPortNum + " " + myIP.toString());
}
I have tried both the channel method and the commented out method, and I even used this method to try and find an open port:
private DatagramSocket createNewSocket() throws IOException {
for (int i = Constants.MIN_PORT_NUM; i < Constants.MAX_PORT_NUM; i++){
try {
System.out.println(i);
return new DatagramSocket(i);
} catch (IOException e){
continue; // try next port
}
}
throw new IOException("No free port found");
}
But I've had no luck so far. The portNum keeps coming up as -1 and the IP is null. Any ideas what could be going on here?
EDIT: When I run the following in debug mode and scroll over mySocket to see what values it contains, it says that:
bound = true
closed = false
connectedAddress = null
connectedPort = -1
But when I run mySocket.getLocalPort(), it returns a valid port number. What's going on here?
Here's the code:
public UDPServer() throws IOException {
myGUI = new ServerGUI();
myClientList = new ArrayList<ClientInfo>();
mySocket = new DatagramSocket(Constants.SERVER_PORT_NUM);
mySocket.setReuseAddress(true);
myPortNum = mySocket.getLocalPort();
myIP = mySocket.getInetAddress();
System.out.println(mySocket);
}
You don't need to do this. Just create it with the non-args constructor, or specify port zero. The system will allocate a free port for you.

cannot get server socket to close

I'm making a simple chat server and just made it so each connection runs on a new thread.
The old version started a single thread for the server, it did a while loop, which would stop when a stop message was sent then close the socket.
The new version loops forever and create a new thread for each new connection. Now I cannot close the socket connection.
If you press a key and the main thread stops, the socket stays open. Thus when I run the program again I need to change the socket number.
code of server
while(true)
{
///////////////////////////////////////////////////
// get a new connection
///////////////////////////////////////////////////
System.out.println("Aceepting connections on port 1030 \r");
try{
// Get New Connection
// wait for ever on accepting new connections
server.setSoTimeout(0);
connection=server.accept();
cConnection thread = new cConnection("thread3", connection);
} catch(IOException ec)
{
System.out.println(ec.getMessage());
}
}
code that starts server
Now each message comes in on a new thread, so I cannot tell it to stop and close the socket.
You need to provide a flag that must be globally accesible, so when some client wants to stop the server then change the variable ans stops the bucle. By example:
class YourServer {
private static boolean execute = true;
public static synchronized void stop() {
execute = false;
}
public void yourMethod() {
while(execute) {
// implement your server here
}
}
}
When a client send the command STOP you must be do
YourServer.stop();
If you want a stop command to stop the server you can call System.exit() to force the program to store or just closing server is likely to be all you need.
Looking into your problem, I understood one thing, that since you are putting
while (true), so your control always gets stuck at connection=server.accept(); listening for a new connection. So in order to stop the sockets you need to first find a way to stop looping in that while loop. Either you can set a Variable, like (int clientsConnected) to check the number of Clients, when that comes to zero stop that while loop. So you can stop your sockets.
Below is my sample code for clients which is doing the same thing for closing the Sockets.
Hopefully this solves your problem.
class GetNamesFromServer implements Runnable
{
private Socket sForName, sForId;
private BufferedReader in, inForName, inForId;
private PrintWriter outForName, outForId;
private static String clientNames;
public GetNamesFromServer(Socket s1, Socket s2)
{
sForName = s1;
sForId = s2;
}
public void run()
{
try
{
outForName = new PrintWriter(sForName.getOutputStream(), true);
outForName.println(Client.clientName);
System.out.println("Send Name : " + Client.clientName);
outForName.flush();
}
catch(IOException e)
{
System.err.println("Error sending Name to the Server.");
}
try
{
inForId = new BufferedReader(new InputStreamReader(sForId.getInputStream()));
Client.clientId = (inForId.readLine()).trim();
System.out.println("Client ID is : " + Client.clientId);
}
catch(IOException e)
{
System.err.println("Error Receiving ID from Server.");
}
try
{
inForName = new BufferedReader(new InputStreamReader(sForName.getInputStream()));
while (true)
{
clientNames = inForName.readLine();
if (clientNames != null && clientNames != "")
{
clientNames = clientNames.substring(1, clientNames.length() - 1);
System.out.println("Names Received : " + clientNames);
String[] names = clientNames.split(", ");
Client.nameClients.clear();
for (String element: names)
Client.nameClients.add(element);
Client.nPane.setText("");
int size = Client.nameClients.size();
System.out.println("Size of list : " + size);
for (int i = 0; i < size; i++)
{
String name = Client.nameClients.get(i);
String colour = Character.toString(name.charAt(0));
name = name.substring(1, name.length()) + "\n";
appendToNamePane(name, ReceiveMessages.getColour(Integer.parseInt(colour)), "Lucida Console");
}
System.out.println("Clients Online : " + Client.nameClients);
}
int index = Client.nameClients.indexOf(Client.clientId + Client.clientName);
**if (index == -1)
{
sForName.close();
break;
}**
}
}
catch(IOException e)
{
System.err.println("Error Receiving Names of Clients from Server");
}
}
NEW EDITION :
You can add a cap to maximum number of clients that can connect, once that reaches your while loop will not go to connection = server.accept(); and hence when they are done chatting (after some time) i.e. totalClients = 0, you can stop your sockets as well, to stop the program.
if (totalClients == 0)
{
socket.close();
serverSocket.close();
}
Regards

Categories

Resources