Jump to content

Question

Posted

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

Share this post


Link to post
Share on other sites

13 answers to this question

  • 0

Posted

[quote name='SPEhosting' timestamp='1354944185' post='595376792']
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
[/quote]

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 "[color=#000000][size=3]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[/size][/color]

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='psyko_x' timestamp='1355064750' post='595379110']
You just need a server to listen on a port and client to connect to that port. Use this guide:
[url="http://www.oracle.com/technetwork/java/socket-140484.html"]http://www.oracle.co...ket-140484.html[/url]

But instead of doing "[color=#000000][size=3]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[/size][/color]
[/quote]

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='SPEhosting' timestamp='1355078551' post='595379452']
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
[/quote]
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.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355083645' post='595379626']
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: [url="http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte"]http://docs.oracle.c...html#write(byte[/url][]) [url="http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte"]http://docs.oracle.c...html#write(byte[/url][], int, int) [url="http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int"]http://docs.oracle.c....html#write(int[/url])

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

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='SPEhosting' timestamp='1355098486' post='595380104']
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
[/quote]
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.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355112533' post='595380424']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='SPEhosting' timestamp='1355113549' post='595380460']
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
[/quote]

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

[code]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 ();
}
}[/code]

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355156326' post='595381282']
Pretty much the same as before except without Readers and such:

[code]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 ();
}
}[/code]

It would definitely benefit you to learn to read the javadocs: [url="http://docs.oracle.com/javase/7/docs/api/"]http://docs.oracle.c...ase/7/docs/api/[/url]
[/quote]

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)

Share this post


Link to post
Share on other sites
  • 0

Posted

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

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355180011' post='595382242']
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.
[/quote]


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% )

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355180011' post='595382242']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='kjordan2001' timestamp='1355180011' post='595382242']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.