小弟在java socket编程上面遇到了一个问题,新手求助,谢谢

fujiji 2008-03-21 06:37:50
代码如下:server端
import java.net.*; import java.io.*;
public class EchoServer {
void doService(Socket clientSocket) {
try{
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
PrintStream out = new PrintStream(clientSocket.getOutputStream());
while (true) {String theLine=in.readLine(); out.println(theLine); }
}catch (IOException e) { System.err.println(e); }}

public void main(String[] args) {
ServerSocket listenSocket;
try {
listenSocket = new ServerSocket(8888); // port
while(true) {
Socket clientSocket = listenSocket.accept();
System.err.println("Connexion from:" + clientSocket.getInetAddress());
doService(clientSocket);
}}
catch (Exception e) { System.err.println(e); }
}}

client端
import java.net.*; import java.io.*;
public class EchoClient {
public static void main(String[] args) {
Socket theSocket;
DataInputStream theInputStream; DataInputStream userInput;
PrintStream theOutputStream;
String theLine;
try {
theSocket = new Socket(InetAddress.getLocalHost(),8888);
theInputStream = new DataInputStream(theSocket.getInputStream());
theOutputStream = new PrintStream(theSocket.getOutputStream());
userInput = new DataInputStream(System.in);
while (true) {
theLine = userInput.readLine();
if (theLine.equals(".")) break;
theOutputStream.println(theLine);
System.out.println(theInputStream.readLine());
}
} catch (UnknownHostException e) { System.err.println(e);
} catch (IOException e) { System.err.println(e); } } }


不知道为什么编译的时候没有问题,但是在运行服务端的时候,显示如下错误?
Exception in thread "main" java.lang.NoSuchMethodError: main

希望有明白人给小弟解答,多谢各位高手了
...全文
96 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AshEmissary 2008-03-21
  • 打赏
  • 举报
回复
少了个i
AshEmissary 2008-03-21
  • 打赏
  • 举报
回复
main方法必须是public static void main(Strng[] xxx)
不能调用doService就把他搞成static就行了
fujiji 2008-03-21
  • 打赏
  • 举报
回复
还是不行啊,说static方法main不能调用doService(clientSocket);
谢了
网络咖啡 2008-03-21
  • 打赏
  • 举报
回复
public void main(String[] args) {
修改为:
public static void main(String[] args) {

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧