Solved

Java Socket Programming.

Posted on 2004-09-22
5
441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 57
java example issue 5 66
Print Rhino Java Array in Javascript 1 58
Java class and jar 3 61
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

738 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