Solved

Java Socket Programming.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

912 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

21 Experts available now in Live!

Get 1:1 Help Now