masterofpuppets 19

Hi,

You could have a look at the Random class, especially the nextInt() and nextInt( int arg0 ) methods.

Hope this helps.

Edit: here's the API for it: [url]http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html[/url]

masterofpuppets 19

Hi,

I agree with NormR1 - instead of the getActionCommand() method, you could try evt.getSource(). Here's an example using your code:
[CODE]public void actionPerformed( ActionEvent evt ) {
if( evt.getSource() == b1 ) {
String m=message.getText();
String m1=message1.getText();
String m2=t.getText();

    computeDigest(m.getBytes());
    computeDigest1(m1.getBytes());
    con();
    computeDigest2(m2.getBytes());
}
else if( evt.getSource() == b2 ) {
    cal_n();
}
else if( evt.getSource() == b3 ) {
    en();
}
else if( evt.getSource() == b0 ) {
    message.setText("");
    digest.setText("");
    message1.setText("");
    digest1.setText("");
    t.setText("");
    digest2.setText("");
    n.setText("");
    phi.setText("");
    pub.setText("");
    a.setText("");
    a1.setText("");
    en_msg.setText("");
}

}
[/CODE]

and please use code tags next time - it's really hard to see what's going on like that.

Finally, you could try to add print statements in your ActionListener. For example, I usually make sure the listener gets called first, and then add commands to it. Also, try to use more meaningfull names for your elements - b0 doesn't say "Clear" to the person who is reading your code.

masterofpuppets 19

Hi,

Not much to do to improve the OO design in your code. Looks pretty good indeed. I addition to what JamesCherrill said, I would probably just add a constructor to the Player class:
[CODE]public Player() {
this.number = -1;
}[/CODE]in case you want to extend it later.

masterofpuppets 19

Hi,

As jackmaverick1 said, the Scanner is simpler to use, depending on what you want to do - if you just need to enter commands, use scanner.nextLine() and then parse the command. As for the DataInputStream, I think you should use it for reading bytes and file transfer.

masterofpuppets 19

..alternatively, you could look at the standard functions for string manipulation - length(), split(...), etc.

masterofpuppets 19

Hi,

Why don't you use a Set to keep track of all unique words. Maybe something like this:
[CODE]Set words = new TreeSet();[/CODE] and then in your code you would just have [CODE]words.add( word );[/CODE] The number of words should be the same as the size of the set.

Hope this helps :)

AhmedGhazey commented: Good Solution +0

masterofpuppets 19

The name and all POST data (apart from file upload) is received as a String. I have it as byte[] as well but I'm using the String one since parsing is easier. Could this be the issue? The user input that generated this was the word Test written in Cyrillic (Тест). I don't have internet access right now but I'll try to test it using String.charAt(...) later and let you know how it goes.

masterofpuppets 19

The 'Тест' is the part that's encoded. What you can see here in the forum is what I get in my site as well, i.e. I can see the correct name in the browser but on my computer it is encoded.

masterofpuppets 19 Posting Whiz in Training

Hello guys,

Here's a problem that's giving me a hard time. I am working on a web server in Java and right now I want to enable the user to create a photo album. The user can type the album name in an field in an HTML form. The problem is that I receive the client request to create a new album with the album name being encoded in some way, which I can't figure out. Here's what I have so far:

  1. A simple .html file that contains the form (it's quite large, so I won't post it here) and I am using this as the encoding: [CODE][/CODE]
  2. A form that contains the input field for the album name. Here's the code for it:[CODE]
    New album name
    <br >

    [/CODE]

  3. The createNewAlbum() function:
    [CODE]function createNewAlbum() {
    var string = document.getElementById( 'albumNameField' ).value;
    if ( string != "" ) {
    document.forms[ 'createAlbumForm' ].submit();
    }
    }[/CODE]

And here's the request from the client that my server prints out:
[CODE]POST /createnewalbum HTTP/1.1
Host: www.gravyty.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://www.gravyty.com/UserHome.html
Content-Type: multipart/form-data; boundary=---------------------------14945890932209
Content-Length: 179

-------------------14945890932209
Content-Disposition: form-data; name="albumNameField"

[COLOR="Red"]Тест[/COLOR]
-----------------------------14945890932209--[/CODE]

So, my question is how should I ...

masterofpuppets 19

Thanks for the replies - I managed to figure it out. The problem was that I was checking for .jpg whereas the original file was .JPG which apparently makes a difference here. I changed it so that the content type also checks .JPG and now it seems to work.

masterofpuppets 19 Posting Whiz in Training

Hi all,

I searched this topic and found a couple of posts but I couldn't find what I am looking for. Here's the problem - I am trying to send an image to a client (browser in this case) over a socket. After the image is received I am trying to display it in the browser but it looks corrupted, as if some of the data is missing. The weird thing is that I am able to send some images and display them without any problems. I probably should mention that I am resizing the images, but that's done in HTML so I don't think it's the problem here. Here's what I've tried so far:

  1. Send different image type, e.g. png, gif - worked;
  2. Save the jpg file as a png and send it over - worked;
  3. Write a simple program that copies a jpg image from one location to another - worked;
  4. Send the image in actual size to the client - worked.

(by 'worked' I mean that I can see it in the browser)

Here's how I send the data to the client:
[CODE]
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
// f is the file to be sent to the client.
BufferedInputStream reader = new BufferedInputStream( new FileInputStream( f ) );

// send OK headers and content length using f.length()

byte[] buffer = new byte[ 4096 ];
int bytesRead;
while ( (bytesRead = reader.read(buffer)) != -1 ) {
out.write( buffer, 0, bytesRead ); ...

masterofpuppets 19

Thanks for the advice.. Got it working now - I'm reading bytes instead of characters by using what you've suggested above.

masterofpuppets 19

Is there a way that I can view the browser request before it is sent to the server?

masterofpuppets 19

OK, here's what I have so far. I managed to read the POST request but Java adds extra '?' characters in some places and I don't know why. Could it be an encoding issue? Here's how I read the POST data:
[CODE]
int contentType;
String line;
String headers;

// Read header lines.
while ( (line = is.readLine()).length() > 0 ) {
headers += (line + "\n");
if ( line.startsWith( "Content-Length: " ) )
len = Integer.parseInt( line.substring( "Content-Length: ".length(), line.length() ) );
numLines++;
}

// Read data.
char[] buffer = new char[ len ];
char c;
int count = 0;
while ( count < len ) {
c = (char) is.read();
buffer[ count ] = c;
count++;
}[/CODE]

masterofpuppets 19

ahh, thanks man dodn't know that :D, I'll try to fix it now

masterofpuppets 19

it is supposed to add a new line to the end of the line that is read since readline() does not include the \n. This might be the problem actually. I have tried a number of ways for reading the request but they were not working properly.

masterofpuppets 19

[QUOTE=NormR1;1584899]Have you compared the input file to the output file (byte by byte) to see exactly what the differences are?[/QUOTE]

Yeah, I checked that and I found out that my file has some new lines inserted in some places ( I assume that's coming from the way I read the request? ) Also I tried to open the .pdf file ( for example ), copy its contents, create a new file with the same name elsewhere and copy the contents there and it doesn't open. Actually, it does but gives me an error saying the file is corrupted.

masterofpuppets 19 Posting Whiz in Training

Hi all,

I am writing a server in Java and I have an issue which I've been working on for days now and I can't seem to find a solution. I want to upload files to the server using an HTML form. I can read the whole POST request and the data that it contains but when I try to create the file in my server space, it seems corrupted. Here is the upload request:
[CODE]POST /uploadprofilepicture HTTP/1.1
Host: www.gravyty.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.gravyty.com/UserHome.html
Content-Type: multipart/form-data; boundary=---------------------------105852418210285
Content-Length: 812

-----------------------------105852418210285
Content-Disposition: form-data; name="uploadfile"; filename="bday.gif"
Content-Type: image/gif

GIF89a
-----------------------------105852418210285--[/CODE]
and here's the way I read that request:
[CODE]BufferedReader is = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
String clientData = "", method = "", boundary = "", line;
int lineCount = 0;

while ( (line = is.readLine()) != null ) {
if ( line.toLowerCase().startsWith( "get" ) )
method = "g";
else if ( line.toLowerCase().startsWith( "post" ) )
method = "p";

if ( line.length() <= 0 && method.equals( "g" ) )
    break;
else if ( method.equals( "p" ) ) {
    if ( line.contains( "boundary=" ) ) {
        boundary = line.substring( line.indexOf( "boundary=" ) + 9, line.length() );
    }
    else if ( !boundary.equals( "" ) && line.equals( "--" + boundary + "--" ) ) {
        clientData += line + '\n'; // separate each new line.
        break;
    }
}

clientData += line + '\n';
lineCount++; ...

masterofpuppets 19

Thanks for the links :) I checked them out but I decided to use Javascript and Ajax for the chat system.

masterofpuppets 19 Posting Whiz in Training

Hi all,

Here's the problem I have. I am designing a website in HTML and I want to include a chat system using a Java applet. I have written a small example applet that just connects to my server and gets the users that are currently online. The applet works when I run it in Eclipse but I tried to launch it from the browser and I got a SocketPermission exception.

I did some reading on the subject and found out that I need to make some changes to the java.policy file in order to grant my applet access to the socket. Also I found a couple of discussions here in the forum but couldn't find anything that actually describes the process of signing the applet. I read something about using a JAR file to include all files that the applet needs (including a java.policy file) but I am not sure how to do that. In addition, I read that the policy file is contained in the Java directory on each client's machine. So my question is how can I connect my applet to the server without having the user download a policy file or having to do any configurations?

masterofpuppets 19

Thanks for all the replies guys :) I managed to figure it out - I wasn't checking for the boundary in the end of the POST request. It works now :)

masterofpuppets 19

[QUOTE=NormR1;1577827]What about status like: available()

Look at your code. It looks like you are reading a line in two places and only testing at one place.[/QUOTE]

working on it...

masterofpuppets 19

ok, I changed the loop a bit to see what's going on. Here's the changed version:

[CODE]BufferedReader is = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
String clientData = "", line;
int lineCount = 0;
do {
try {
line = is.readLine();
System.out.println( "LINE " + line );
clientData += line + "\n";
lineCount++;
} catch( IOException e ) {
e.printStackTrace();
}
} while ( is.readLine() != null );[/CODE]
and here's what I got:
[CODE]LINE GET /index.html HTTP/1.1
LINE User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
LINE Accept-Language: en-us,en;q=0.5
LINE Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
LINE Connection: keep-alive[/CODE]
and after that the readline() appears to block as nothing else happens.

masterofpuppets 19

Here's my server's trace for the small file:

[CODE]POST /uploadprofilepicture HTTP/1.1
User-Agent: JFileUpload
Host: www.gravyty.com:8080
Content-Length: 768
Content-Type: multipart/form-data; boundary=Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY

--Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY
Content-Disposition: form-data; name="todo"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

upload
--Q_R8VUqHouRbUvmwvBmRglK8pUDWVDJ2_FwnY
Content-Disposition: form-data; name="uploadfile"; filename="badge.gif"
Content-Type: image/gif; charset=ISO-8859-1
Content-Transfer-Encoding: binary

GIF89a
//includes a lot of binary data which I cannot copy for some reason[/CODE]

and here's what I get for the larger file:

[CODE]POST /uploadprofilepicture HTTP/1.1
User-Agent: JFileUpload
Host: www.gravyty.com:8080
Content-Length: 47839
Content-Type: multipart/form-data; boundary=zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY

--zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY
Content-Disposition: form-data; name="todo"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

upload
--zoPZAXA7eEsPlf9bVSTZYF9QcN3uZ7Yk4GrasVkY
Content-Disposition: form-data; name="uploadfile"; filename="icons.png"
Content-Type: image/png; charset=ISO-8859-1
Content-Transfer-Encoding: binary

//binary data missing here[/CODE]

masterofpuppets 19

yeah, they have a website and I've already posted a topic there but as I found out just now, the size of the file is initially set to be unlimited, so I guess there's a problem with my server...

To answer your question - I have tried to put a print after the while terminates and I get the correct result for the smaller files, i.e. POST header, connection details, and the data transferred, but for the larger files I get everything but the data (including the content-length header). So I am not sure if I am reading the request correctly.

masterofpuppets 19 Posting Whiz in Training

Hey,

I am not sure whether this is the right place to ask this but I figured that maybe someone here has used JFileUpload for uploading files to a server.

I just downloaded the applet and tested it out on my server and uploading works as long as the files are smaller that 1,000 bytes, i.e. very small images. Whenever I try with a larger file I get the error:

[CODE]Upload started ...Upload failed : java.net.SocketException: Software caused connection abort: socket write error[/CODE]

When I look at the request sent to my server, I see that it is using a POST method but it is not sending any data, whereas with the smaller images I can see the data being sent. I think there might be a limitation for the file size in the applet but I couldn't see anything in the applet_http.js file. Also, I think I might be reading the browser request in a wrong way. Here's how I do it:

[CODE]...s = socket.accept();
// Read in request.
BufferedReader is = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
String clientData = "", line;
int lineCount = 0;
do {
line = is.readLine();
clientData += line + "\n";
lineCount++;
}
while ( is.ready() );[/CODE]

Any ideas? Thanks.

masterofpuppets 19

Thanks for all the replies people :) I think I'll go with ~s.o.s~'s suggestion for the situation.

masterofpuppets 19

Thanks for the reply :) I've tried this but it gives me the IP address of the machine, which is the same on all three here at home. I just changed this:

[CODE]System.out.println( "ADDRESS: " + s.getLocalAddress().getHostAddress() );[/CODE]

to:

[CODE]System.out.println( "ADDRESS: " + s.getRemoteSocketAddress() );[/CODE]

masterofpuppets 19 Posting Whiz in Training

Hey guys,

Here's the problem I have:

I am writing a server application in Java using the ServerSocket and Socket classes. I have an instance of ServerSocket which listens for connection and accepts them. The server runs a website that requires users to log in using an e-mail and a password. Everything works fine until I tried it on my local network at home - I am using a wireless router for 3 computers and they all share the same IP address and differ by their local address, e.g. one is 192.168.1.1 and another is 192.168.1.2, etc. The problem is that whenever I call

[CODE]...
Socket s = null;
while ( true ) {
s = socket.accept();
System.out.println( "ADDRESS: " + s.getLocalAddress().getHostAddress() );
...
}
[/CODE]

it prints my current local address (192.168.1.2) even when I enter the website from one of the other computers, which means that I can log in from one pc and I would automatically logged in the others as well, since I am using the local address as a unique identifier for each connection.

My question is: is that the correct way to get the local IP and why is it printing the same address even when I am loading the site from a different computer?

Thanks. (pls let me know if you need to know anything else)

masterofpuppets 19

hi,
you could do something like this:
[CODE]men = [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ], [ 13 ], [ 14 ], [ 15 ] ]
for gender in men[ :10 ]:
print gender

Output:

"""[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]"""[/CODE]

I'm not sure whether the format of the list I'm using is the same as the one you specified, so pls. correct me if this is not right.

hope this helps :)