注意:我的第一个StackOverFlow POST! WHOOOP!的
大家好,所以我构建了一个简单的客户端服务器程序,它只接受一个字符串然后返回它,但由于某种原因我无法让我的主要方法正确地调用方法而且我不会明白为什么它不会工作。有人可以看看,告诉我我错过了什么/做错了什么。我知道我的网络逻辑是可靠的,但不是电话。我认为是新鲜眼睛的时间,任何有用的东西!谢谢。
MAIN:
public class FileRetrieve_Easy_main
{
public static void main(String[] args)
{
new Thread(new SimpleServer()).start();
new Thread(new SimpleClient()).start();
}
}
客户端:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
public static class SimpleClient implements Runnable
{
@Override
public void run()
{
Socket socket = null;
try
{
Thread.sleep(3000);
socket = new Socket("localhost", 3333);
PrintWriter outWriter = new PrintWriter(socket.getOutputStream(),true);
outWriter.println("Hello Mr. Server!");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
}
finally
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}// end run
}//end simple client
}
SERVER:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static class SimpleServer implements Runnable
{
@Override
public void run()
{
ServerSocket serverSocket = null;
while (true)
{
try
{
serverSocket = new ServerSocket(3333);
Socket clientSocket = serverSocket.accept();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Client said :"+inputReader.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}//end while
}// end run
}//end simple server
}//end class
答案 0 :(得分:0)
服务器的名称Runnable是Server.SimpleServer。客户端Runnable的名称是Client.SimpleClient。你创建了嵌套类,你可以在这里阅读:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
答案 1 :(得分:0)
默认包中是否包含所有类?
如果你这样做怎么办:
public class FileRetrieve_Easy_main
{
public static void main(String[] args)
{
new Thread(new Server.SimpleServer()).start();
new Thread(new Client.SimpleClient()).start();
}
}