LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-12-2012, 09:28 AM   #1
l_long_island
LQ Newbie
 
Registered: Feb 2008
Posts: 25

Rep: Reputation: 0
Java Client not sending bytearray via sockets


Hello all,
I am having a problem sending a byte array to a server via sockets. The Server logic (a separate thread from the client) waits for the data which apparently never comes (both client and server threads run on the same Windows PC).
The suspect client code is as follows:



ByteArrayOutputStream byte_sender;

public void send_Data_Via_Socket (byte [] array_containing_message )
{
try {
byte_sender = new ByteArrayOutputStream ();
byte_sender.write(array_containing_message);
byte_sender.flush();
} catch (IOException e) {
e.printStackTrace();
}

} // end method


For Completeness the Server code is as follows (The “data arrived” line is never reached, but the read is executed)

public byte[] Server_Wait_for_Message_Data()
{

byte[] Arriving_Message = new byte[20];

try {

InputStream byte_stream = ( Server_Rcv_Socket.getInputStream());
byte_stream.read(Arriving_Message);
System.out.println ("data arrived ?? ");

} catch (IOException e) { e.printStackTrace();}
return Arriving_Message ;


} // end method


Prior to running the above code the following took place:

The server was connected to port 5000 (apparently successful)
The client was connecte to “localhost”, port 5000 (apparently successful)
The client loaded a 20-byte array with 20 bytes of non-textual data.
The array is sent by the client every 5 seconds
Thanks for your time.

l_long_island

Last edited by l_long_island; 01-12-2012 at 12:19 PM. Reason: added definition of byte_sender
 
Old 01-16-2012, 05:36 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
You may want to run wireshark or similar to determine whether the data is actually being sent by the client
 
Old 01-16-2012, 11:20 PM   #3
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 86
I could be missing something here, but I don't quite see how the code you've shown us will work.

You are expecting the data to be sent out a socket, yes?

In send_Data_Via_Socket you are creating a new ByteArrayOutputStream.

Even though you are storing the reference to that new Object globally, any
association that may have been made previously, between some ByteArrayOutputStream
and a socket, does not apply to the new Object. So calling the flush method on
OutputStream should have no effect on the data in the new ByteArrayOutputStream,
yes?

In order for the data from the new ByteArrayOutputStream to be sent out a socket,
you would seemingly either have to establish a relationship between that new Object and
a socket before calling flush(), or call writeTo() and specify an
OutputStream that is associated with a socket.
 
1 members found this post helpful.
  


Reply

Tags
clientserver, sockets


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sending binary data through sockets in C Carlos2313 Programming 1 11-29-2010 06:47 AM
How to detect broken connection before sending data with sockets Fabio Paolini Linux - Networking 3 11-03-2010 05:59 AM
Sending and receiving data simultaneously using BSD sockets nagaraju1024 Linux - Networking 2 06-27-2010 11:17 PM
Java Sockets: Help with a simple chat client/server Mega Man X Programming 6 07-15-2009 06:16 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 12:49 PM


All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration