Client/server application (Help me)
I have a client server application.In the client part i send data to the server and the server receive data. After receiveing data server also send some data but the client can't capture data. Anyone please tell me what is the problem?
I am giving the sample code here......
I am thinking actually the problem is in the client side.......
-
Server
out = new DataOutputStream(client.getOutputStream());
input = this.client.getInputStream();
byte[] data=new byte[input.available()];
input.read(data);
byte[] sdata=new byte[3];
sdata[0]=1;
sdata[1]=2;
sdata[2]=3;
out.write(sdata);
--
Client
--
How can i implement my client to send and receive at a time?
[762 byte] By [
Iftia] at [2007-11-27 7:37:57]

# 1
I haven't understood your code, but if you want create a client/server application you can use this code
//-- Server
ServerSocket server= new ServerSocket(port);
Socket connection= server.accept();
.
.
.
.
BufferedReader netIn= new BufferedReader(
new InputStreamReader(connection.getInputStream()));
PrintWriter netOut= new PrintWriter(connection.getOutputStream());
netOut.println("Welcome on the server");
// Client
Socket connection= new Socket(host,port);
BufferedReader netIn= new BufferedReader(
new InputStreamReader(connection.getInputStream()));
PrintWriter netOut= new PrintWriter(connection.getOutputStream());
String messageReceived= netIn.readLine();
in this way the server sends a message to client and the client stores this data in messageReceived.
Message was edited by:
PremierITA
# 2
> byte[] data=new byte[input.available()];
> input.read(data);
In the first line you are assuming that the length returned by input.available() represents a whole message, which is not what it does and not what it is for, and in the second line you are assuming that you filled 'data' with that amount of data, which is not an infallible assumption. Consider what happens if input.available() keeps returning zero or 1, which it is perfectly entitled to do.
ejpa at 2007-7-12 19:18:36 >

# 3
Thanks all of u.
I have solved the problem.
Now i have another problem.In the Client side I have used DataOutput
Stream(for sending) and InputStream(for receiving).
With the InputStream i am capturing data by this way.....
int dataSize=is.available();
int kkk=0;
boolean hasSize=true;
while (kkk<=0) {
dataSize = is.available();
if (dataSize != 0 && hasSize) {
byte[] dat = new byte[dataSize];
is.read(dat);
String s = new String(dat);
System.out.println("Capture:" + s);
System.out.println("data ssss: "+dataSize);
hasSize=false;
kkk=9;
}//End of if
}//End of while
Here is the problem. If I am sending from the server data size less 4000 byte then the client can easily capture the data. But when i am sending the data which is very very large such as 70,000 bytes then it creates a problem.
My target is simple. I want to capture the whole data that is send by the server . then want to keep the data in a byte array which size will depend upon the receiving data.
How can i do that?
Iftia at 2007-7-12 19:18:36 >

# 4
The problem here consists of exactly the same mistakes as already commented on above.
The only way you can know you've received all the data is if the server sends a length word before the transmission, or if you've read to EOS (-1 return).
Why do you want to accumulate the entire response in a byte array? Are you sure it will always fit into memory? If you really want to do that, have a look at writing what you receive into a ByteArrayOutputStream.
ejpa at 2007-7-12 19:18:36 >

# 5
Thanks.
My client know how much byte is sending the server by inspecting the first 4 byte that has been send by the server. Now i know the total size(Number of byte) of the message that has been send by the server, now can i receive the total data that has been send by the server ? Receiving the data in a bytearray will make my other calculation easy , for this reason i am doing so.
Thank u agin for giving answer .
NOw please tell me how can i do it. If necessary give me some sample code. I also want to do this using InputSteam in the client side.
Ifti
>>
Iftia at 2007-7-12 19:18:36 >

# 6
Nobody is going to give you any sample code, at least I'm not, unless you pay me what you are being paid to produce whatever it is you are being paid to produce.
ou need to read bytes from the server and process them until you have read as many bytes as indicated by the length prefix.
This is Grade 3 arithmetic, not even computer programming.
ejpa at 2007-7-12 19:18:36 >

# 7
ejp,
Why r u so much angry with me. I don't understand your reply. Ok thats fine. Tell me that u r not interested in answering me. That will be fine. Whether it is a grade 3 arithmatic or not , you need not tell that.
I will try it myself or from anybody. You need not pay attention in this post for future. Ok?
bye forever........................
Iftia at 2007-7-12 19:18:36 >

# 8
> bye forever........................... walks off into the sunset with sagging shoulders and dragging his feet ...
# 9
You should see me when I really am angry. This isn't anything of the kind, just a little straight talk.
ejpa at 2007-7-12 19:18:36 >
