473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

socket problem,,,,, plz help

7
my problem is

I have socket between server and a client
the client sending data to the server, so the server respond to the client with the response
this my code for receive the response data
----------------------------------------------------------
os.write(getAuthenticationRedGreenInfoString());
os.flush();
Thread.sleep(3000);
DataInputStream is = new DataInputStream(mySocket.getInputStream());
int rr;
int nn = is.available();
if(nn > 0)
{
byte[] bb = new byte[100000];
rr = is.read(bb); // rr is Number of read bytes
}

look to the code, u can notice that i have 2 variables nn and rr
nn is the avialbe bytes in the InbutStream and
rr is the nubmer of byes did read from the InbutStream.

when i make a debug , nn = 8192 and rr = 22233 !!!!!!!!!!!!!


the question is HOW can I read _ALLl _ the received data from the InbutStream

waiting ur help
Oct 21 '07 #1
3 1153
JosAH
11,448 Expert 8TB
the question is HOW can I read _ALLl _ the received data from the InbutStream
You can't know that because you have no control of the sending party. The available()
method returned 8192 because that seemed to be the size of the underlying buffer
and it was full of data already. Then you gave it a 100,000 byte size buffer and 22,000
and then some were filled. That's the amount of bytes your read() call managed to
read.

You have to come up with some sort of 'protocol' between the sending party and the
receiving party, e.g. first the number of bytes to be send is send and then the data is
pushed over the line. Otherwise your receiving end doesn't know how many bytes are
being send over the line.

kind regards,

Jos
Oct 21 '07 #2
green
7
many thanks for u and for ur valuable time
---------------------------------------------------------------
so I tried to get all the data by looping with the InputStream, but I get another problem


Expand|Select|Wrap|Line Numbers
  1. os.write(getAuthenticationRedGreenInfoString());
  2. os.flush();
  3. Thread.sleep(3000);          // must sleep a while to get the server response
  4. DataInputStream is = new DataInputStream(mySocket.getInputStream());        
  5.           int i,r;
  6.             byte [] finalByteArray = new byte[70000];
  7.             int finalLength;
  8.             int nn = is.available();
  9.             if(nn > 0)
  10.             {
  11.                 byte[] bb = new byte[30000];
  12.                 r = is.read(bb);//,0,bb.length);
  13.                 System.arraycopy(bb, 0, finalByteArray, 0, r);
  14.                 finalLength = r;
  15.                 r = is.read(bb);
  16.                 if(r >0)
  17.                 {                    
  18.                     System.arraycopy(bb, 0, finalByteArray, finalLength , r);
  19.                     finalLength += r;
  20.                     r = is.read(bb);
  21.                 }
  22.             }            
  23.             returnString = decryptReturnedResponse(finalByteArray);
  24.             System.out.println(returnString);
this code get all the received data..
but in the second receive I get a socket write error Exception

so
plz help me
Oct 21 '07 #3
JosAH
11,448 Expert 8TB
many thanks for u and for ur valuable time
---------------------------------------------------------------
so I tried to get all the data by looping with the InputStream, but I get another problem


Expand|Select|Wrap|Line Numbers
  1. os.write(getAuthenticationRedGreenInfoString());
  2. os.flush();
  3. Thread.sleep(3000);          // must sleep a while to get the server response
  4. DataInputStream is = new DataInputStream(mySocket.getInputStream());        
  5.           int i,r;
  6.             byte [] finalByteArray = new byte[70000];
  7.             int finalLength;
  8.             int nn = is.available();
  9.             if(nn > 0)
  10.             {
  11.                 byte[] bb = new byte[30000];
  12.                 r = is.read(bb);//,0,bb.length);
  13.                 System.arraycopy(bb, 0, finalByteArray, 0, r);
  14.                 finalLength = r;
  15.                 r = is.read(bb);
  16.                 if(r >0)
  17.                 {                    
  18.                     System.arraycopy(bb, 0, finalByteArray, finalLength , r);
  19.                     finalLength += r;
  20.                     r = is.read(bb);
  21.                 }
  22.             }            
  23.             returnString = decryptReturnedResponse(finalByteArray);
  24.             System.out.println(returnString);
this code get all the received data..
but in the second receive I get a socket write error Exception

so
plz help me
That code doesn't make sense. As I wrote in my previous reply: your receiver
can't know if it read all the data. A simple second attempt to read some more
doesn't solve it either. You'd need a third, fourth etc. attempt and you still
wouldn't know if you'd read it all. You definitely need a little 'protocol', Make
your sender send the number of bytes it wants to send first, followed by those
bytes. Your receiver can anticipate on that: first it receives a number indicating
the number of bytes it should read afterwards.

kind regards,

Jos
Oct 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: José Carlos | last post by:
Hi. I am starting a server socket program. I send data from client socket but the server don´t recived anything. This is my python code: Server: import socket
1
by: dexter15820 | last post by:
I am having a socket problem after upgrading my .net framework 1.1 to the latest patch level. After the upgrade I can establish a connection but I cannot read from the socket. If I remove the...
2
by: Rekkie | last post by:
Hi, I am trying to implement a ping client that is multithreading. The approach I have used is to create a ping class which I instantiate from the main thread and which contains a method...
2
by: Jason Hurder | last post by:
Hi folks, I am writing a small network application that uses TCP sockets. My application is the accepts connections via the AcceptConnection function of a server socket and creates a client...
1
by: Thomas Lerchner | last post by:
Hi! I have a problem with an UDP socket. I create socket and call BeginReceiveFrom to use the socket async. But the callback methode is being called several times with the same packet from the...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
1
by: rxgmoral | last post by:
dll problem,help me:) i create dll project ,the dll is Use MFC in a Static Library i hope exe call ListCtrl class from dll ==================DLL====================...
7
by: =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?= | last post by:
hi all! I got a problem. I declared a SOCKET var in my C program but when i compiled the program it displayed like *--------------------------------------------------------------* *'SOCKET':...
3
by: Clement | last post by:
Please help me....... I am getting blocked in bind() system call....... i don't know why can you please any one tell me why........ #include<stdio.h> #include<sys/un.h>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.