DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page How to send and receive byte array through socket connection
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Feb 2009
Default How to send and receive byte array through socket connection - 02-05-2009, 04:07 AM

Hi all,
I need to send an array of byte to other machine and the other side will retrive the array of byte through socket connection. How can I do that? Thank you very much..
Below is the code that I wrote for send the byte array. The bold part is where it writes into DataOutputStream and send it out. However I dont know how to read the array byte on the other side. Thank you very much.

import javax.microedition.io.*;
import java.io.*;

public class ObjectSending extends Thread{

private String hostIP;
private StreamConnection connection;
private byte[] myByte;

public ObjectSending(byte[] myByte)
{
this.myByte = myByte;
}

public void run()
{
try
{

connection = (StreamConnection) Connector.open("socket://" + hostIP + ":3333", Connector.WRITE);
DataOutputStream os = connection.openDataOutputStream();
connection.close();
sendObject(os, myByte);


}catch (IOException ex) {
System.out.println("Problem in connecting with the host.");
ex.printStackTrace();
}
}

private void sendObject (DataOutputStream os, byte[] myByte) throws IOException
{
try
{
os.write(myByte);
os.flush();
}
finally
{
os.close();
}
}

}
Reply With Quote
  (#2 (permalink)) Old
Moderator
 
Posts: 102
Thanks: 0
Thanked 7 Times in 7 Posts
Join Date: Jan 2008
Default 02-05-2009, 07:43 AM

I wonder if you should call connection.close() after sending the byte[] ?
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Feb 2009
Default 02-05-2009, 09:10 AM

Quote:
Originally Posted by jsugrue View Post
I wonder if you should call connection.close() after sending the byte[] ?
Yup, if you see carefully, i've close the connection before the sendObject method is being closed.
Reply With Quote
  (#4 (permalink)) Old
Administrator
 
matt's Avatar
 
Posts: 43
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 02-05-2009, 09:23 AM

This page might help: Reading from and Writing to a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

It seems to outline how to read and write to a socket. The reading should be as easy as creating a server socket and listening on the port you expect and then waiting for data. There are different ways to do it depending on if you want to try using NIO.
Reply With Quote
  (#5 (permalink)) Old
Administrator
 
matt's Avatar
 
Posts: 43
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 02-05-2009, 09:24 AM

This page might help: Reading from and Writing to a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

It seems to outline how to read and write to a socket. The reading should be as easy as creating a server socket and listening on the port you expect and then waiting for data. There are different ways to do it depending on if you want to try using NIO.
Reply With Quote
  (#6 (permalink)) Old
Member
 
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Feb 2009
Default 02-06-2009, 02:36 AM

Thank you for the suggestions....I'v successfully implemented it..Anyway, I got another question to ask, is that possible for j2me to implement the getDeclaredFields() as in j2se?
I need to get the list of fields that declared in a particular class in j2me..How can I do it??It is urgent, thanks for the help.
Reply With Quote
  (#7 (permalink)) Old
Member
 
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Feb 2009
Default 02-06-2009, 02:37 AM

Thank you for the suggestions....I'v successfully implemented it..Anyway, I got another question to ask, is that possible for j2me to implement the getDeclaredFields() as in j2se?
I need to get the list of fields that declared in a particular class in j2me..How can I do it??It is urgent, thanks for the help.
Reply With Quote
  (#8 (permalink)) Old
Member
 
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Feb 2009
Default 02-10-2009, 04:57 AM

Anyone can help pls? this is urgent
Reply With Quote
  (#9 (permalink)) Old
Moderator
 
jwenting's Avatar
 
Posts: 99
Thanks: 0
Thanked 8 Times in 8 Posts
Join Date: Feb 2008
Send a message via MSN to jwenting
Default 02-10-2009, 09:20 AM

It's not urgent to us...
Reply With Quote
  (#10 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Feb 2009
Default thnx alot - 02-10-2009, 01:51 PM

actually<i never get touch with this part b4
but i found that the codeeis need a littele modification
can u simply wait for me
i hope u find my code useful
Reply With Quote
The Following User Says Thank You to samra For This Useful Post:
jay_goh (02-10-2009)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to send object using j2me jay_goh Java 2 02-04-2009 09:08 PM
java socket performance xcolwell Java 1 01-25-2009 08:44 AM
send SMS from MIDlet barak_obama Java 0 07-14-2008 04:27 AM
Can't receive any PDF files... brendancunnie Ajax & Javascript 2 06-04-2008 01:27 PM
Help with queue array please!! vmcg105 Java 1 03-08-2008 02:15 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com