java找不到符号类socket,编译报错+解决方法:错误: 找不到符号
public class ServerPlainTest { // 包內包外可見
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8189);
System.out.println("the server has startuped, waiting for connections.");
while (true) { // accept multiple clients connection request.
Socket s = ss.accept();
System.out.println("a client has connected successfully.");
new Thread(new ReceiveHandler(s, Role.SERVER)).start();
new Thread(new SendHandler(s, Role.SERVER)).start();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
public class MessageHandler { // 包內包外可見.
}
// receive msg.
class ReceiveHandler implements Runnable { // 包內可見性.
private Socket socket;
private Role role;
public ReceiveHandler(Socket socket, Role role) {
this.socket = socket;
this.role = role;
}
public void run() {
try {
System.out.println(role + " executes ReceiveHandler.run method.");
InputStream is = socket.getInputStream();
Scanner in = new Scanner(is);
// echo client input
boolean done = false;
while (!done && in.hasNextLine()) {
String line = in.nextLine();
System.out.println("from "+ role + ": " + line);
if (line.trim().equalsIgnoreCase("bye")) {
done = true;
}
}
// close relative instance.
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// send msg.
class SendHandler implements Runnable { // 包內可見性.
private Socket socket;
private Role role;
public SendHandler(Socket socket, Role role) {
this.socket = socket;
this.role = role;
}
public void run() {
PrintWriter pw = null;
try {
System.out.println(role + " executes SendHandler.run method.");
Scanner s = new Scanner(System.in);
OutputStream os = socket.getOutputStream();
pw = new PrintWriter(os, true); // autoFlush=true.
if (role.equals(Role.SERVER)) {
pw.println("from server: you've connected with the server.");
}
while (s.hasNext()) {
String line = s.nextLine();
pw.println(line);
}
pw.println("connection closed successfully. bye!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
【2】solution:在編譯 ServerPlainTest.java 之前,編譯 MessageHandler.java, 這樣會附帶編譯 ReceiverHandler.java and SendHandler.java ;
總結
以上是生活随笔為你收集整理的java找不到符号类socket,编译报错+解决方法:错误: 找不到符号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datalayer.exe进程的详细介绍
- 下一篇: 国产封测第一 长电科技称已实现4nm手机