Socket listener

Hi people,

I am implementing a web server, but it seems that I am facing some concurency problems.

I have the following page which my webserver is going to "serve":

<html>

<head>

<!--GLOBAL SCRIPT FILES -->

<script type="text/javascript" src="/globalscripts/BrowserUtils.js"></script>

</head>

<body>

<input type="button" id="btnSubmit" value="Submit" onclick="makeServerRequest(1,'')">

</body>

</html>

but what is download into the browser is something like this:

<html>

<head>

<!--GLOBAL SCRIPT FILES -->

<script type="text/javascript" src="/globalscripts/BrowserUtils.js"></script>

</head>

<body>

<input type="button" id="btnSubmit" value="Submit" onclick="

The page is not sent completelly. Just before sending the content thru the socket I also saved it into a file, and everything is there. The odd thing here is that if I make the page do only one request "eg by changing the src atribute to something else "the page is downloaded normally.

Here is a peace of the java code that creates the socket threads:

publicstaticvoid start()throws Exception

{

//SessionHandler objSessionHandler = new SessionHandler();

ServerSocket objServerSocket =new ServerSocket(WebServerConstants.PORT,100);

while(serverStatus)

{

Socket objSocket = objServerSocket.accept();

class NewWorkerThreadimplements Runnable

{

Socket objSocket;

public NewWorkerThread(Socket pSocket)

{

this.objSocket = pSocket;

}

publicvoid run()

{

Worker objectWorker =new Worker(objSocket, objSocket.getInetAddress().getAddress());

}

}

Thread objThread =new Thread(new NewWorkerThread(objSocket));

objThread.start();

}

}

Buy looking into this, can you guys have any hints about anything I might be doing wrong.

Many thanks,

MeTitus>

[3154 byte] By [Me_Titusa] at [2007-11-27 4:39:43]
# 1
Do you have a PrintStream or something that you are forgetting to close() (which does a flush()) before closing the socket?
sjasjaa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi sjasja thanks for you reply,

this is where the stream in written

private void processIncludedFiles(OutputStream pOutputStream, File pObjFile) throws IOException

{

pOutputStream = new BufferedOutputStream(pOutputStream);

Vector objVector = getFileDirectories(pObjFile);

InputStream objInputS = new FileInputStream(pObjFile.getAbsolutePath());

byte[] fileContent = new byte[objInputS.available()];

byte[] byteResult = new byte[objInputS.available()*2];

objInputS.read(fileContent);

int j = 0;

for(int iter=0;iter<fileContent.length;iter++)

{

outer:

if(fileContent[iter] == (byte)'s' && fileContent[iter+1] == (byte)'r' && fileContent[iter+2] == (byte)'c')

{

byteResult[j++] = fileContent[iter];

byteResult[j++] = fileContent[iter + 1];

byteResult[j++] = fileContent[iter + 2];

for(int innerIter=iter+3;innerIter<fileContent.length;innerIter++)

{

if(fileContent[innerIter] == (byte)'.' && fileContent[innerIter+1] == (byte)'.' && fileContent[innerIter+2] == (byte)'/')

{

int backDirectories = 0;

for(int innerIter1 = innerIter+1;innerIter1<fileContent.length;innerIter1++)

{

if(fileContent[innerIter1] == (byte)'/')

{

if(!(fileContent[innerIter1 + 1] == (byte)'.'))

{

try

{

byte[] byteTMP = ((String)objVector.elementAt(backDirectories)).getBytes();

System.arraycopy(byteTMP, 0, byteResult, j++, ((String)objVector.elementAt(backDirectories)).getBytes().length);

j = j + ((String)objVector.elementAt(backDirectories)).getBytes().length - 1;

iter = innerIter1+1;

}

catch(Exception ex)

{

}

break outer;

}

backDirectories++;

}

}

}

else if(fileContent[innerIter] != (byte)'.' && fileContent[innerIter] != (byte)'"' && fileContent[innerIter] != (byte)'\'' && fileContent[innerIter] != (byte)'=' && fileContent[innerIter] != (byte)' ')

{

try

{

byte[] byteTMP = ((String)objVector.elementAt(objVector.size()-1)).getBytes();

System.arraycopy(byteTMP, 0, byteResult, j++, ((String)objVector.elementAt(objVector.size()-1)).getBytes().length);

j = j + ((String)objVector.elementAt(objVector.size()-1)).getBytes().length-1;

iter = innerIter;

}

catch(Exception ex)

{

}

break;

}

byteResult[j++] = fileContent[innerIter];

}

}

byteResult[j++] = fileContent[iter];

}

System.arraycopy(IIOConstants.EOL, 0, byteResult, j++, IIOConstants.EOL.length-1);

j = j + IIOConstants.EOL.length;

fileContent = new byte[j];

System.arraycopy(byteResult, 0, fileContent, 0, j);

pOutputStream.write(fileContent);

pOutputStream.flush();

pOutputStream.close();

}

as far as I know everything seems to be al right all right, but if a close the socket do I still need to close the stream? I am doing that anyway...

MeTitus>

Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 3

I changed the method where I create the threads, just in case one threads were smashing eachother:

public static void start() throws Exception

{

//SessionHandler objSessionHandler = new SessionHandler();

ServerSocket objServerSocket = new ServerSocket(WebServerConstants.PORT,100);

while(serverStatus)

{

new Worker(objServerSocket.accept()).start();

}

}

still the problems persists... any idea what I might be doing wrong?

MeTitus

Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 4

I don't know what all this code is supposed to be doing, or why the target size is double the source size, or what byteTMP is for given that you don't do anything with it, or whether all the looping and indexing code is correct, but there are several problems evident:

byte[] fileContent = new byte[objInputS.available()];

byte[] byteResult = new byte[objInputS.available()*2];

Here you are assuming that available() returns the total length of the file, which is not what it is for and not what it is specified to return.

objInputS.read(fileContent);

and here you are ignoring the result returned by the read call so you don't know how many bytes were actually read, i.e. how much of fileContent[] is actually valid data.

ejpa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 5

Thank you very much for your reply ejp and sorry to be replying only now, but I have been enjoying my holidays ;)

I've always used available to check the file size, but it seems I was doing it wrongly. Unfortunately I am not allowed to do this:

byte[] fileContent;

objInputS.read(fileContent);

Because the read method does not accept an byte[] which was not initialized before, thus I have to give it an initial size value, and if available is not the correct way of doing it, can you tell me how.

Once again thanks,

MeTitus

Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 6
File.length()
ejpa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks ejb ;)MeTitus
Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 8
But what if I'm not using a File object. The inputStream doesn't have the length method, the only method that gives me the length would be the available method. What can I do in this case?Thanks,MeTitus
Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 9

I went through the thread several times and I missed the part where you explained what you were trying to do and why you were using a File object in the first place in your web application.

Anyway, if you want to copy something from one place to another there's all kinds of code on the web. First stop thinking you have to read the entire data set into memory and then write it back out again. Instead read a bit, write a bit. Wouldn't be surprised if the the I/O tutorial doesn't have that as an example.

http://java.sun.com/docs/books/tutorial/essential/io/

DrClapa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 10

> Instead read a bit, write a bit.

Note to the OP: "Bit" here is in the general sense of "a small piece", not "binary digit," just in case that wasn't clear. If the context is line-based text, then a readLine/println pair or something similar is a fair choice. Otherwise buffered streams/readers/writers and a buffer size of maybe 1-16k is probably ok.

jverda at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...
# 11

> I went through the thread several times and I missed

> the part where you explained what you were trying to

> do and why you were using a File object in the first

> place in your web application.

>

> Anyway, if you want to copy something from one place

> to another there's all kinds of code on the web.

> First stop thinking you have to read the entire data

> set into memory and then write it back out again.

> Instead read a bit, write a bit. Wouldn't be

> surprised if the the I/O tutorial doesn't have that

> as an example.

>

> http://java.sun.com/docs/books/tutorial/essential/io/

Hi DrClap,

Fistful I undestand how difficult it is to help when one doens't know what the other part is doing, so here is what I am doing:

I am implementing a web server for a device that runs CDLC. I don't know whether what I am doing is right but I didn't really know how to implement a web server. What this function in particular does is reading a page requested and parsing it in order to replace values of the "scr" attribute which have relative paths.

So every time a file is requested the entire file is parsed to search for the "../" string, which is then replace with the correct path in relation to the web root folder.

The reason why I am doing this:

byte[] fileContent = new byte[objInputS.available()];

byte[] byteResult = new byte[objInputS.available()*2];

is because instead of writing the content straight away, I am holding it so that I can write it later on.

Anyway you guys probably have a better way of doing it, don't you ;)

Thanks,

MeTitus

Me_Titusa at 2007-7-12 9:50:32 > top of Java-index,Java Essentials,Java Programming...