java服务器套接字
我想在同一个文件中创建一个java服务器和客户端...因为这是我的项目
SERVER ---> 的结构客户端/服务器---->客户端
服务器和客户端部分的编码非常简单,但我在创建客户端/服务器部分时遇到问题..我的代码只能运行客户端部分而不能启动服务器部分。因为我的线程 run() 代码。
package com.main.datadistributor;
import java.io.IOException;
import java.net.ServerSocket;
public class Slave {
public static void main(String args[]) throws IOException{
Config cfg = new Config("Slave");
String MasterServerIP = cfg.getProperty("MasterServerIP");
String MasterServerPort = cfg.getProperty("MasterServerPort");
String SlaveServerPort = cfg.getProperty("SlaveServerPort");
Client client = new Client(MasterServerIP,Integer.parseInt(MasterServerPort),"SLAVEONE");
client.run();
int numClient = 0;
ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
System.out.println("Server starts running");
try{
while(true){
new Server(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort), "SLAVESERVER").start();
}
} finally {
listener.close();
}
}
}
从上面的代码中,我只在执行 client.run() 时遇到问题,并且代码只是停在那里,而没有继续在尝试部分执行下面的新服务器
i wanted to create a java server and client in a same file...because this is my structure of project
SERVER ---> CLIENT/SERVER ----> CLIENT
the coding for the SERVER and CLIENT part is quite simple but i have a problem when creating for CLIENT/SERVER part.. where my code can only run CLIENT part and not starting the SERVER part. because of my thread run() code.
package com.main.datadistributor;
import java.io.IOException;
import java.net.ServerSocket;
public class Slave {
public static void main(String args[]) throws IOException{
Config cfg = new Config("Slave");
String MasterServerIP = cfg.getProperty("MasterServerIP");
String MasterServerPort = cfg.getProperty("MasterServerPort");
String SlaveServerPort = cfg.getProperty("SlaveServerPort");
Client client = new Client(MasterServerIP,Integer.parseInt(MasterServerPort),"SLAVEONE");
client.run();
int numClient = 0;
ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
System.out.println("Server starts running");
try{
while(true){
new Server(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort), "SLAVESERVER").start();
}
} finally {
listener.close();
}
}
}
from the code above i have problem only executing client.run() and the code just stop there without proceeding to execute new Server below at the try section
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
Client
是Thread
的子类,那么您需要调用start()
,而不是run()
。如果它实现了 Runnable,那么您需要实例化一个 Thread 来执行它并调用线程的 start() 方法。If
Client
subclassesThread
, then you need to callstart()
, notrun()
. If it implementsRunnable
then you need to instantiate aThread
to execute it and call the thread'sstart()
method.不知道
Client
是什么,或者看到它的代码限制了我们提供帮助的能力。如果它有一个 run() 方法,它可能会实现 Runnable,因此您可以只调用 new Thread(client).start() 来代替直接调用其 run 方法。Not knowing what
Client
is, or seeing its code limits our ability to help. If it has arun()
method it may implementRunnable
, so you can possibly just callnew Thread(client).start()
instead of calling its run method directly.