Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-12-2012, 08:28 AM   #1
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 ();
} catch (IOException e) {

} // 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());;
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.


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

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

Rep: Reputation: 87
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,

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.


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 05:47 AM
How to detect broken connection before sending data with sockets Fabio Paolini Linux - Networking 3 11-03-2010 04:59 AM
Sending and receiving data simultaneously using BSD sockets nagaraju1024 Linux - Networking 2 06-27-2010 10:17 PM
Java Sockets: Help with a simple chat client/server Mega Man X Programming 6 07-15-2009 05:16 PM
Sending files over udp sockets in C raszagal Programming 9 11-27-2004 11:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 PM.

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