Tal Nathan Tal Nathan

Greenhorn
+ Follow
since Feb 26, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tal Nathan Tal Nathan

Then again the JVM handles the threads so I prob am wrong.
Not related to your code but more related to why it happens in solaris and not winxp.

The one thing I can think of is how the operating systems map kernel threads to system threads.

Win XP uses the M:M model so that many threads are serviced by many kernel threads and solaris possibly uses a M:1 or a 1:1 so if you thread blocks the kernel thread then all other threads will be blocked.

I could be talking rubbish, my CS course on thread synch's was a long time ago.
Look in the api but there is a way to set a window to be always on top I think you use setAlwaysOnTop(true); in your JFrame.

Would show(); not bring it to the front as well?
12 years ago
I know this works with javax.swing.JLabel but not sure what other components, basically you can format it using html.

ie:

lbl.setText("<html>Hello this is<b>bold<b></html>");
12 years ago
I agree with the above post.
Also you may need to open a second socket/port for transmitting data while maintaing your communication port for your protocol unless you require no communication while data transmission is occuring.
import java.lang.Runtime;


String cmd = "cmd /c notepad.exe"
Runtime.getRuntime().exec(cmd);

Havent tested this, but it should do the trick.
12 years ago
Hey, you had two problems in your code.
Firstly every time you want to send data accross a socket you need to call the flush(); method;
Ie after every out.println(); call out.flush(); this ensures that all data in the buffer is sent accross.

Secondly, on your server code you keep on using system.out.println to display the status and you are printing out in.readLine();

this will cause the server to hang since every time you call in.readLine() it waits for the client to send data.

Below is the code with modifications, note I added out.flush and I used a variable String clientData = in.readLine() so that you only call in.readLine once.

I know you are doing this to test but when you get the hang of it you need to learn how to use sockets and threads together to allow multiple connections.

Ie, every time you call socket.accept() you should launch a new thread or call a thread from a thread pool to handle the request.
Hope this helps, good luck!.




import java.io.*;
import java.net.*;

public class server {

public static void main(String[] args) throws IOException {
listenSocket();
}

public static void listenSocket(){
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
String line;

try{
server = new ServerSocket(20001);
System.out.println("starting local server on port 20001...");
}
catch (IOException e){
System.out.println("Could not listen on port 20001");
}

try{
client = server.accept();
}
catch (IOException e) {
System.out.println("Accept failed: 8123");
}

try{
//Establish a input stream from client socket
// * for reading data from client port
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
//Establish a output stream to client socket
// * for writing data to client port
out = new PrintWriter(client.getOutputStream(), true);
}
catch (IOException e) {
System.out.println("Accept failed: 8123");;
}

// while(true)
{
try{
System.out.println("* starting to cope with client data *");
String clientData = in.readLine();
System.out.println("receive data from client (" + clientData + ")");
//line = in.readLine();
System.out.println("send data to client (" + clientData + ")");
out.println("This is a message from server");
out.flush();
}
catch (IOException e) {
System.out.println("Read failed");
e.printStackTrace();
}
}
}


}





import java.io.*;
import java.net.*;


class Client{

public static void main(String args[]){
try {
Socket socket = new Socket("localhost", 20001);
System.out.println("connecting to server localhost on port *20001*...");

//ready to send data to server
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("This is the message from a client");
out.flush();
//ready to receive data from server
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine());

}
catch (UnknownHostException e){
System.out.println("Unknown host");
}
catch (IOException e){
e.printStackTrace();
}
}
}
Could you post more of your code so we can get a bigger picture?

Another simple test is to telnet into your server socket and see if a connection can be established.

telnet localhost <port>

I cant see the rest of your code but if after your call client = socket.accept(); and dont do anything afterwards then your application will terminate after it exists the main method and close the port. I cant see what your code is doing so this is just one blind assumption.
Come to think of it, the above method is not every efficient.
Rather create byte array [1024] and use that to read the data in order to make sure you read 1kb at a time rather then 1 byte at a time.
12 years ago
Note: I am very new to struts so there may very well be a simpler/better way
to do this, but this method does work for getting an uploaded file and saving it.

In your action class do the following:

FormFile myFile = myForm.getTheFile();
String fileName = myFile.getFileName();
InputStream input = null;
input = myFile.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(input));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveLocation));
int i = 0;
while (((i = dis.read()) != -1)) {
out.write(i);
}
out.flush();
out.close();

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 years ago
Is this update needed only once? Ie are you migrating your data to hsqldb?
If so then I suggest using a program called "Universal Database Converter"
This shouldnt be a big issue, you can create a jar file and put all the class files of the libraries you are using in it.

For example if your mysql connector classes are in the ext/com/mysql/.. directory just place the com directory into the same directory as your other class files and the JRE will detect it when you run your program.
12 years ago