Solved

Java Socket Programming.

Posted on 2004-09-22
5
440 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

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

713 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