socket 菜鸟求指教~ 找不到主函数?

into_wild 2012-11-15 05:45:20
写一个C/S的socket app,实现通信和一些自定义的基本命令, 以下代码可以正常编译,但是无论是server还是client,命令行下一运行就出错,显示

package ClientServer;
//client
import java.io.*;
import java.net.*;

public class myClient {

public static void main(String[] args) throws IOException {
String servername = args[0];
int port = Integer.parseInt(args[1]);

Socket clientsocket = null;
PrintStream out = null;
BufferedReader in = null;
boolean connected = false;
boolean isEcho = false;

try {
clientsocket = new Socket(servername, port);
connected = true;
out = new PrintStream(clientsocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
}catch (IOException e) {
System.err.println("Error:Cannot connect to server!");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader (new InputStreamReader(System.in));
String userInput;

//out.print("client");
while (connected) {
System.out.println("Session has been established.");
System.out.print("operation>");
userInput = stdIn.readLine();
if (userInput == "whoAMi") {
out.println(userInput);
System.out.println(in.readLine());
} else if (userInput == "bye") {
out.println(userInput);
System.out.println(in.readLine());
connected = false;
} else if (userInput == "startEcho") {
isEcho = true;
out.println(userInput);
System.out.println(in.readLine());

String echoInput;
while (isEcho) {
System.out.print("echo>");
echoInput = stdIn.readLine();
if (echoInput == "stopEcho") {
isEcho = false;
}
out.println(echoInput);
System.out.println(in.readLine());
}
}
}
System.out.println("Session is terminated.");

out.close();
in.close();
stdIn.close();
clientsocket.close();
}

}



package ClientServer;
//server
import java.io.*;
import java.net.*;

public class myServer{
ServerSocket serversocket;
String inputLine, outputLine;
String message;

public myServer(int portNum){}

void run()
{
try{
FileWriter fstream = new FileWriter("Log.txt", true);
PrintWriter log = new PrintWriter(fstream);

System.out.println("Waiting for client on port " + serversocket.getLocalPort());
Socket clientsocket = serversocket.accept();
System.out.println("Connected to client on " + clientsocket.getRemoteSocketAddress());
log.println("\r\n************SocketLog**************");
log.println("Connected to client on " + clientsocket.getRemoteSocketAddress());

PrintStream out = new PrintStream(clientsocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));

while ((inputLine = in.readLine()) != null) {

if (inputLine == "whoAMi") {
//outputLine = "server: " + serversocket.getLocalSocketAddress();
outputLine = "server: " + java.net.InetAddress.getLocalHost().getHostAddress();
out.println(outputLine);
log.println("\nOperation from client " + clientsocket.getRemoteSocketAddress() +": "+ inputLine);
} else if (inputLine == "startEcho") {
outputLine = "listening";
out.println(outputLine);
log.println("\nOperation from client " + clientsocket.getRemoteSocketAddress() +": "+ inputLine);
log.println("\nNow in Echo mode.\n");

while ((message = in.readLine()) != null) {
if (message == "stopEcho") {
log.println("Operation from client " + clientsocket.getRemoteSocketAddress() +": "+ message);
log.println("Now out of Echo mode.");
break;
}
outputLine = "server: " + message;
out.println(outputLine);
log.println("Echo from client " + clientsocket.getRemoteSocketAddress() +": "+ message);
}
out.println("stopped");
} else if (inputLine == "bye") {
System.out.println("Connection to client " + clientsocket.getRemoteSocketAddress() + " terminated.");
log.println("Connection to client " + clientsocket.getRemoteSocketAddress() + " terminated.");
out.println("bye");
break;
}
}
out.close();
in.close();
clientsocket.close();
log.close();
fstream.close();

} catch (IOException e) {
System.exit(1);
}
}

public static void main(String[] args)
{
int portNum = Integer.parseInt(args[0]);
myServer server = new myServer(portNum);
while(true){
server.run();
}
}
}


不知道是什么错误?找不到主函数?新手求详细指导~~谢各位大神啊~~
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2012-11-19
  • 打赏
  • 举报
回复
分别导出Jar文件,服务端需要一直运行,使用下面命令运行 java -jar server 100 (100为端口号)
dscccc 2012-11-16
  • 打赏
  • 举报
回复
相当不错,帮助很大
a8509190 2012-11-16
  • 打赏
  • 举报
回复
你的类可能没有编译。。
咖啡机 2012-11-16
  • 打赏
  • 举报
回复
如果是用命令的话,我担心你没有编译myServer类, 再者,请把类名开头第一字母大写(编码规范)。
nmyangym 2012-11-15
  • 打赏
  • 举报
回复
尝试一下: 1 把当前目录退到向上一级,cd .. 2 运行 java ClientServer.myServer
into_wild 2012-11-15
  • 打赏
  • 举报
回复
补充一下,不需要多线程,只要实现一对一的通信即可

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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