Solved

Java Socket Programming.

Posted on 2004-09-22
5
434 Views
Last Modified: 2012-06-27
Hi Guys,
I have the following problem:


1) I have a client.
2) I have a proxy server which basically relays messages between a real server and the client.
3) The proxy server when requested by the client can connect to say port 21(ftp) on the real server .


My problem is....i have connected the proxy server to the real server and the client is connected to the proxy server.....however when i try to send the messages from the proxy server to the client i am not getting all the messages sometimes i am getting only half the message some times i am getting the entire message and so on...i tried flushing to the stream but to no effect.
The following is part of my client and server code

client.java

DataOutputStream outToServer = new
                                DataOutputStream(clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
 byte[] ins = new byte[300];  // i think this is creating a problem  is there any way to know how many bytes the server is sending so that
// we can allocate that much to the byte array????
 inFromServer.readFully(ins);
mod = new String(ins);
 outToServer.writeBytes(sentenceToSend + "\n");
                    outToServer.flush();




Server.java basically does the same thing while writing to the client.
I have tried using both readfully and read in java.io.BufferedReader .

Please kindly help.
This is urgent.
Thanks a lot
s.


0
Comment
Question by:srik27
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12130131
// i think this is creating a problem  is there any way to know how many bytes the server is sending so that
// we can allocate that much to the byte array????

no. you need to build that into your protocol.
eg. use fixed size packets, or include the number of bytes as part of the packet.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12130481
It's a reconstruction problem.
Your data arrive/goes to/from the proxy fragmented. They must be recomposed.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 12130566
What protocol are you using? Is it FTP or it is your own?
Regarding the size of the byte array you use:
ByteArrayOutputStream out = new ByteArrayOutputStream();
int r;
while ((r=inFromServer.read())!=-1) [
  out.write(r);
}
byte[] ins = out.toByteArray();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12132691
What you should do is to open a Socket then close it when you've finished using it or you may run into problems. The reading end should read until end of stream, then there's no doubt.

final int BUF_SIZE = 8192;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) > -1) {
      out.write(buffer, 0, bytesRead));
}
in.close();
out.close();
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
ID: 12133641
Hi srik27,

You should directly use the InputStream and OutputStream of the socket to avoid buffer problems.
You can also change the socket parameters ( http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html )


0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now