• 0

found my java problem need help solving!


Question

ok so i have found my java error! the real issue! now all i need to do is to solve it!!!

I need to send bytes over a socket connection ... simple as ... they have to be sent and received as bytes ... if someone could write me up a lil dummy program I can work with ? just convert a fixed string to bytes and send it as bytes and receive it as bytes the other end ... would help me out ALOT! then I can see it in action and fiddle with it

13 answers to this question

Recommended Posts

  • 0

ok so i have found my java error! the real issue! now all i need to do is to solve it!!!

I need to send bytes over a socket connection ... simple as ... they have to be sent and received as bytes ... if someone could write me up a lil dummy program I can work with ? just convert a fixed string to bytes and send it as bytes and receive it as bytes the other end ... would help me out ALOT! then I can see it in action and fiddle with it

You just need a server to listen on a port and client to connect to that port. Use this guide:

http://www.oracle.com/technetwork/java/socket-140484.html

But instead of doing "new Socket("kq6py", 4321)" on the client side, you need to pass an InetAddress object as the first parameter. The InetAddress is created by calling InetAddress.getByName() or InetAddress.getByAddress() static methods

  • 0

You just need a server to listen on a port and client to connect to that port. Use this guide:

http://www.oracle.co...ket-140484.html

But instead of doing "new Socket("kq6py", 4321)" on the client side, you need to pass an InetAddress object as the first parameter. The InetAddress is created by calling InetAddress.getByName() or InetAddress.getByAddress() static methods

I already have all the connections :p but I have been using printwriter I need to use bytearrayoutputstream ... but I am finding it difficult to find a tutorial which uses it over sockets

  • 0

I already have all the connections :p but I have been using printwriter I need to use bytearrayoutputstream ... but I am finding it difficult to find a tutorial which uses it over sockets

You don't use a ByteArrayOutputStream. That's something you can write to to build an in-memory byte array without having to grow it yourself.

You want to use the regular output stream provided by the socket and use the byte write methods: http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]) http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[], int, int) http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int)

String has a built-in getBytes() method that you can use to write as a byte array.

  • 0

You don't use a ByteArrayOutputStream. That's something you can write to to build an in-memory byte array without having to grow it yourself.

You want to use the regular output stream provided by the socket and use the byte write methods: http://docs.oracle.c...html#write(byte[]) http://docs.oracle.c...html#write(byte[], int, int) http://docs.oracle.c....html#write(int)

String has a built-in getBytes() method that you can use to write as a byte array.

ahh i see ... and what about the recieving end ? i tried the char writer like you previously gave me ... but for the decryption it has to be sent in bytes i cannot convert to string at any point until after it has been decrypted or i will get an error I set up a small single class test which encrypted and decrypted using only bytes ( which worked) then one which followed the same method of conversion as my current program and that just caused it to kill its self

  • 0

ahh i see ... and what about the recieving end ? i tried the char writer like you previously gave me ... but for the decryption it has to be sent in bytes i cannot convert to string at any point until after it has been decrypted or i will get an error I set up a small single class test which encrypted and decrypted using only bytes ( which worked) then one which followed the same method of conversion as my current program and that just caused it to kill its self

For that you could replace the CharArrayWriter with a ByteArrayOutputStream. Use the straight InputStream from the Socket though and use the read methods from that.

  • 0

For that you could replace the CharArrayWriter with a ByteArrayOutputStream. Use the straight InputStream from the Socket though and use the read methods from that.

could you give me an example code on how to implement it ? I really only learn via example sorry to be such a pain you have been a really really big help to me

  • 0

could you give me an example code on how to implement it ? I really only learn via example sorry to be such a pain you have been a really really big help to me

Pretty much the same as before except without Readers and such:

import java.io.*;
import java.net.*;
public class chatServer
{
public static void main (String[] args) throws IOException
{
  ServerSocket serverSocket = null;
  try
  {
   serverSocket = new ServerSocket (4444);
  }
  catch (IOException e)
  {
   System.err.println ("Could not listen on port: 4444.");
   System.exit (1);
  }
  System.out.println ("Server - Listening on port 4444");
  Socket clientSocket = null;
  try
  {
   clientSocket = serverSocket.accept ();
  }
  catch (IOException e)
  {
   System.err.println ("Accept failed."); System.exit(1);
  }
  OutputStream out = clientSocket.getOutputStream();
  BufferedReader in = clientSocket.getInputStream();
  String toClient, fromClient;
  toClient = "Hello";
  System.out.println ("Server Message: " + toClient);
  out.write(toClient.getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int read;
while ((read=in.read(buf, 0, 4096))>0) {
System.out.println("x");
   os.write(buf, 0, read);

}
fromClient = os.toString(); //Or for bytes use toByteArray()
  System.out.println ("Client Message: " + fromClient);
  out.close ();
  in.close ();
  clientSocket.close ();
  serverSocket.close ();
}
}

It would definitely benefit you to learn to read the javadocs: http://docs.oracle.com/javase/7/docs/api/

  • 0

Pretty much the same as before except without Readers and such:

import java.io.*;
import java.net.*;
public class chatServer
{
public static void main (String[] args) throws IOException
{
  ServerSocket serverSocket = null;
  try
  {
   serverSocket = new ServerSocket (4444);
  }
  catch (IOException e)
  {
   System.err.println ("Could not listen on port: 4444.");
   System.exit (1);
  }
  System.out.println ("Server - Listening on port 4444");
  Socket clientSocket = null;
  try
  {
   clientSocket = serverSocket.accept ();
  }
  catch (IOException e)
  {
   System.err.println ("Accept failed."); System.exit(1);
  }
  OutputStream out = clientSocket.getOutputStream();
  BufferedReader in = clientSocket.getInputStream();
  String toClient, fromClient;
  toClient = "Hello";
  System.out.println ("Server Message: " + toClient);
  out.write(toClient.getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int read;
while ((read=in.read(buf, 0, 4096))>0) {
System.out.println("x");
   os.write(buf, 0, read);

}
fromClient = os.toString(); //Or for bytes use toByteArray()
  System.out.println ("Client Message: " + fromClient);
  out.close ();
  in.close ();
  clientSocket.close ();
  serverSocket.close ();
}
}

It would definitely benefit you to learn to read the javadocs: http://docs.oracle.c...ase/7/docs/api/

thanks i will try this and observe ... I know it would help to learn to read them ... but I honestly do not understand them ... I can only ever do stuff when I have seen it used before I need a context and I dunno why I cant grasp the docs probs not reading it completely (I skim)

  • 0

when I change it around it keeps saying

^

required: BufferedReader

found: InputStream

chatServer.java:36: error: no suitable method found for read(byte[],int,int)

while ((read=in.read(buf, 0, 4096))>0) {

^

method BufferedReader.read(char[],int,int) is not applicable

  • 0

Replace BufferedReader with InputStream.

The docs aren't all that hard to read. It's a listing of methods and fields from classes with (hopefully) a description of them and some even with examples. Of course finding the class you need if you don't know what to look for might be a little tricky.

  • 0

Replace BufferedReader with InputStream.

The docs aren't all that hard to read. It's a listing of methods and fields from classes with (hopefully) a description of them and some even with examples. Of course finding the class you need if you don't know what to look for might be a little tricky.

I got it working ish haha just working through the bugs (only got it working as in no errors lol.... and sending data... yet to get everything sending 100% )

  • 0

Replace BufferedReader with InputStream.

The docs aren't all that hard to read. It's a listing of methods and fields from classes with (hopefully) a description of them and some even with examples. Of course finding the class you need if you don't know what to look for might be a little tricky.

ok new problem haha... I am sending the bytes... but when the bytes arrive they are different? I think its byting my bytes?

  • 0

Replace BufferedReader with InputStream.

The docs aren't all that hard to read. It's a listing of methods and fields from classes with (hopefully) a description of them and some even with examples. Of course finding the class you need if you don't know what to look for might be a little tricky.

I got it working! its a christmas miracle! 100% working encryption decryption !! :D

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Just for anyone reading, AdGuard (the free, standalone MV3 extension) is quite good now, a direct competitor to uBlock Origin Lite and much more built-out than it.
    • Microsoft Edge 149.0.4022.62 by Razvan Serea Microsoft Edge is a super fast and secure web browser from Microsoft. It works on almost any device, including PCs, iPhones and Androids. It keeps you safe online, protects your privacy, and lets you browse the web quickly. You can even use it on all your devices and keep your browsing history and favorites synced up. Built on the same technology as Chrome, Microsoft Edge has additional built-in features like Startup boost and Sleeping tabs, which boost your browsing experience with world class performance and speed that are optimized to work best with Windows. Microsoft Edge security and privacy features such as Microsoft Defender SmartScreen, Password Monitor, InPrivate search, and Kids Mode help keep you and your loved ones protected and secure online. Microsoft Edge has features to keep both you and your family protected. Enable content filters and access activity reports with your Microsoft Family Safety account and experience a kid-friendly web with Kids Mode. The new Microsoft Edge is now compatible with your favorite extensions, so it’s easy to personalize your browsing experience. Download: Microsoft Edge (64-bit) | 193.0 MB (Freeware) Download: Microsoft Edge (32-bit) | 170.0 MB Download: Microsoft Edge (ARM64) | 188.0 MB View: Microsoft Edge Website | Release History Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, when I saw that, I wanted to find the nearest nose. You can't find a good nose these days when you need one.
    • Anthropic launches Claude Fable 5, a state-of-the-art AI model that beats OpenAI's GPT-5.5 by Pradeep Viswanathan Back in April, Anthropic announced Claude Mythos Preview, a frontier model with state-of-the-art coding capabilities. Due to the cybersecurity implications that would occur due to the availability of such a powerful model, Anthropic made it available to only a select set of companies around the world. The company's plan was to prepare appropriate guardrails before releasing such a powerful model to everyone. Now, after nearly two months, Anthropic announced Claude Fable 5, its most capable AI model yet for general users. The company also announced Claude Mythos 5, the same underlying model as Fable 5, but with safeguards lifted, making it more suitable for selected cybersecurity and biology use cases. Claude Fable 5 sits a tier above its Opus models and it beats most other generally available models across areas including software engineering, knowledge work, vision, scientific research, and long-running autonomous tasks. To prevent model misuse, when Claude Fable 5 detects certain requests related to cybersecurity, biology, chemistry, or model distillation, the request will be routed to the Claude Opus 4.8 model. Anthropic claims that these safeguards trigger in less than 5% of sessions on average. However, for large organizations working on critical software, Claude Mythos 5 can be availed through Project Glasswing. Later, Anthropic has plans to expand access through a broader trusted access program. As you can notice in the benchmarks above, Fable 5 and Mythos 5 are state-of-the-art on most key AI benchmarks and they are well ahead of OpenAI's frontier model, GPT-5.5. For example, Fable 5 is the new state-of-the-art model for vision tasks. Also, Mythos 5 has the strongest cybersecurity capabilities of any model in the world. Claude Fable 5 and Claude Mythos 5 are priced at $10 per million input tokens and $50 per million output tokens, which is less than half the price of Claude Mythos Preview. Another big change is that Anthropic is making a change to the way they handle business customer data for both Fable 5 and Mythos 5 models. The company will now require 30-day retention for all traffic on both first- and third-party surfaces. Anthropic promises that it won't use the data to train Claude models, instead it will use it against complex and novel attacks. Claude Fable 5 is available today on the Claude API and consumption-based Enterprise plans. It is also included at no extra cost for Pro, Max, Team, and seat-based Enterprise customers from today through June 22. After that, users on those plans will need usage credits to continue using Fable 5, unless Anthropic extends the included access window based on capacity. Developers can access Fable 5 through the Claude API using the claude-fable-5 model name.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      525
    2. 2
      PsYcHoKiLLa
      232
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!