Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Frenchtopus

TCP connexion with JAVA server

Question

Hi ! 

My project is te get my AE Inventory send to a SQL Database using a JAVA TCP server (because JAVA is the language I'm more familiar with).

My issue is when I'm done sending my packet, my java program crash (I give you details below). I've tested with a JAVA TCP client and it work fine.

My OC program is very basic. It's just to test my TCP server :

local net = require("internet")
local con = net.open("my ip",my port)

if(con) then
  con:write("Hi ! I'm your OpenComputer ! \r\n")
  con:write("Test Sentence 2")
  con:close
end

This is my Class wich handle the input informations :

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

public class ClientProcessor implements Runnable{

   private Socket sock;
   private BufferedInputStream reader = null;
   
   
   public ClientProcessor(Socket pSock){
      sock = pSock;
   }

   public void run(){
      System.err.println("Client Inbound");
      boolean closeConnexion = false;
      while(!sock.isClosed()){
         try {
            reader = new BufferedInputStream(sock.getInputStream());           
            String response = read();
            InetSocketAddress remote = (InetSocketAddress)sock.getRemoteSocketAddress();

            String debug = "";
            debug = "Thread : " + Thread.currentThread().getName() + ". ";
            debug += "Demande de l'adresse : " + remote.getAddress().getHostAddress() +".";
            debug += " Sur le port : " + remote.getPort() + ".\n";
            debug += "\t -> Commande reçue : " + response + "\n";
            System.err.println("\n" + debug);
                       
         }catch(SocketException e){
            System.err.println("LA CONNEXION A ETE INTERROMPUE ! ");
            break;
         } catch (IOException e) {
            e.printStackTrace();
         }         
      }
   }
   
   //Function to read the inbound data
   private String read() throws IOException{      
      String response = "";
      int stream;
      byte[] b = new byte[4096];
      stream = reader.read(b);
      response = new String(b, 0, stream);
      return response; 
   }
}

So. I start my JAVA server, and then launch my OC test program. Here is the result :

Thread : Thread-1. Demande de l'adresse : my ip. Sur le port : 60152.
     -> Commande reçue : Hi ! I'm your OpenComputer !
Test Sentence 2 


Hi ! I'm your OpenComputer !
Test Sentence 2

Exception in thread "Thread-1" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.checkBounds(String.java:381)
    at java.lang.String.<init>(String.java:545)
    at server.opencomputer.perso.ClientProcessor.read(ClientProcessor.java:71)
    at server.opencomputer.perso.ClientProcessor.run(ClientProcessor.java:36)
    at java.lang.Thread.run(Thread.java:748)

I already started digging on the issue. For me, the "String index out of range: -1" is due to an "empty" packet while trying to build the answer in the "response" variable with "response = new String(b, 0, stream);". Since "stream" is equal to -1, it make it crash. But I don't know if it come from OpenComputer or my Java server since with a Java Client it work fine. I've also analyzed the inbound packets and I don't see empty or faulty one.

I already have a program for listing my AE inventory, and a Class to input things into a SQL database (Mariadb server is ready too). Only a small thing before it's done :(

Is there something I'm missing ?

Thanks for your help

The Frenchtopus.

 

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.