LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error in coding a java server. (https://www.linuxquestions.org/questions/programming-9/error-in-coding-a-java-server-329189/)

mrobertson 06-01-2005 09:29 AM

Error in coding a java server.
 
I am currently coding a java server in the vi text editor using a knoppix linux system. I have used the following code.

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

public class Server {

public static void main(String[] ar) {

int port = 6666;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket socket = ss.accept();
System.out.println("Got a client :) ... !");
System.out.println();

InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();

DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
while(true) {
line = in.readUTF();
System.out.println("The client just sent me this line : " + line);
System.out.println("Waiting for the next line...");
System.out.println();
}


} catch(Exception x) {
x.printStackTrace();
}
}
}

I am getting the following errors:

No encode delegate for this image format "java.net.*"
No encode delegate for this image format ".io.*"
line 4: public: command not found
line 6: syntax error near expected token "("
line 6: public static void main(String[] ar) {


Does anyone know what is causing these errors and what I can do to fix them?

huibert.alblas 06-01-2005 03:38 PM

Your codes looks ok.

What steps do you take to execute the code.
(You know you have to compile it fisrt :-)

Your Error messages do not look like nornal error messages.

Please give more info.

mrobertson 06-02-2005 07:09 AM

I used chmod 711 JavaServer1 and then typed JavaServer1 on the next command line. What steps should I be taking? Is there another way to go about it? Do I need to import libraries that handle the java socket class?

huibert.alblas 06-02-2005 01:37 PM

You need to be more exact in your description:

you must have run
"javac Server.java"
at some point.

This should create a file named Server.class

To run the program you need to do something along the lines like this: "java -cp . Server"

Trying to execute a Java classfile directly will not work.

If you don't understand any of this, I recomend you start reading something like "thinking Java" by Bruce Eckel
http://www.bruceeckel.com/javabook.html

Or if you don't want to buy the book (yet) :
http://www.codeguru.com/java/tij/tij_c.shtml
The book is free (but you can buy a dead tree copy at your bookstore of choise)


All times are GMT -5. The time now is 03:28 PM.