java程序改错
来源: 互联网 发布时间:2015-04-05
本文导语: 程序为: //: testServer.java // Very simple server that just // echoes whatever the client sends. import java.io.*; import java.net.*; public class testServer { // Choose a port outside of the range 1-1024: public static final int PORT = 8080; ...
程序为:
//: testServer.java
// Very simple server that just
// echoes whatever the client sends.
import java.io.*;
import java.net.*;
public class testServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
Socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
Socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
Socket.close();
}
} finally {
s.close();
}
}
} ///:~
报错:
testServer.java:24: non-static method getInputStream() cannot be referenced from a static context
Socket.getInputStream()));
^
testServer.java:31: non-static method getOutputStream() cannot be referenced from a static context
Socket.getOutputStream())),true);
^
testServer.java:41: non-static method close() cannot be referenced from a static context
Socket.close();
请教各位,有什么解决办法??
//: testServer.java
// Very simple server that just
// echoes whatever the client sends.
import java.io.*;
import java.net.*;
public class testServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
Socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
Socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
Socket.close();
}
} finally {
s.close();
}
}
} ///:~
报错:
testServer.java:24: non-static method getInputStream() cannot be referenced from a static context
Socket.getInputStream()));
^
testServer.java:31: non-static method getOutputStream() cannot be referenced from a static context
Socket.getOutputStream())),true);
^
testServer.java:41: non-static method close() cannot be referenced from a static context
Socket.close();
请教各位,有什么解决办法??
|
注意大小写,楼上的各位大哥说的是。
|
在静态方法(static main)中调用了非静态的方法。
可以放在构造器中,然后在main中使用new testServer ()。
可以放在构造器中,然后在main中使用new testServer ()。
|
Socket注意大小写就行了
Socket=>socket
Socket=>socket
|
Socket注意大小写就行了
你定义的是socket对象
使用方法却是Socket的
Socket=>socket
你定义的是socket对象
使用方法却是Socket的
Socket=>socket
|
楼上这个大哥就是高手
就是这个错误
就是这个错误
|
估计你用的是ultraedit吧!