Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Socket Programming.

Posted on 2004-09-22
5
Medium Priority
?
443 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 2000 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

Technology Partners: 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 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