我试图找出如何在同一时间多次运行相同的功能。我可以使用基于其他SO问题的多处理来实现某些功能,但遗憾的是它不能按我的意愿工作。实际上,当我运行它时,我得到类似的东西(函数在每个oster之后运行):
<label>
我想实现这个目标(或类似的东西):
<label>
我对Python很陌生,所以有可能犯了一些微不足道的错误,但遗憾的是我无法弄清楚问题出在哪里,所以如果有人能告诉我正确的解决方案,我真的很感激。
Client
public class Requester {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message="";
String message2 = "";
String login="";
String password="";
String ipaddress;
Scanner stdin;
Requester(){}
void run()
{
stdin = new Scanner(System.in);
try{
//1. creating a socket to connect to the server
User newUser = new User();
User receivedUser = new User();
boolean authenticated = false;
System.out.println("Please Enter your IP Address");
ipaddress = stdin.next();
requestSocket = new Socket(ipaddress, 2004);
System.out.println("Connected to "+ipaddress+" in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
out.reset();
in = new ObjectInputStream(requestSocket.getInputStream());
System.out.println("Hello");
//3: Communicating with the server
do{
try
{
message = (String)in.readObject();
System.out.println(message);
System.out.print("Please enter username: ");
login = stdin.next();
newUser.setName(login);
System.out.println(login);
System.out.print("Please enter password: ");
password = stdin.next();
newUser.setPassword(password);
System.out.println(password);
//in = new ObjectInputStream(requestSocket.getInputStream());
sendMessage(newUser);
message = (String)in.readObject();
receivedUser = (User)in.readObject();
//System.out.println(message);
//receivedUser = (User)in.readObject();
/*if(receivedUser.getAthentication() == true) {
System.out.println("CLIENT LOGIN");
} else {
System.out.println("CLIENT FAILED");
}*/
}
catch(ClassNotFoundException classNot)
{
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(User newUser)
{
try{
out.writeObject(newUser);
out.flush();
//System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}
Server
public class EchoServer {
static List<User> users = new ArrayList<User>();
public static void main(String[] args) throws Exception {
User user1 = new User("john", "test1");
users.add(user1);
User user2 = new User("paul", "test2");
users.add(user2);
User user3 = new User("martin", "test3");
users.add(user3);
ServerSocket m_ServerSocket = new ServerSocket(2004,10);
int id = 0;
while (true) {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
}
}
class ClientServiceThread extends Thread {
Socket clientSocket;
String message, login;
int clientID = -1;
boolean running = true;
ObjectOutputStream out;
ObjectInputStream in;
EchoServer list = new EchoServer();
ClientServiceThread(Socket s, int i) {
clientSocket = s;
clientID = i;
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
out.reset();
System.out.println("client> " + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
void sendUser(User usrObj)
{
try {
out.writeObject(usrObj);
out.flush();
out.reset();
System.out.println("clent> " + "User Object Received");
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}
public void run() {
User newUser = new User();
User userAuthen = new User();
boolean userAuthenticated = false;
System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
try
{
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.flush();
out.reset();
in = new ObjectInputStream(clientSocket.getInputStream());
System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
sendMessage("Connection successful\n");
do{
try
{
//message = (String)in.readObject();
//System.out.println("client>"+clientID+" "+ message);
//if (message.equals("bye"))
newUser = (User)in.readObject();
for(User userObj : list.users) {
if(userObj.getName().equals(newUser.getName())) {
userAuthenticated = true;
userAuthen.setAthentication(userAuthenticated);
}
}
/*if(userAuthenticated == false) {
sendMessage("Login Incorrect!");
sendMessage("FALSE");
}*/
if(userAuthenticated == true) {
sendMessage("Login Correct\n");
sendMessage("TRUE");
sendUser(userAuthen);
}
if(userAuthenticated == false) {
sendMessage("Login Failed\n");
sendMessage("FALSE");
}
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
//sendMessage("server got the following: "+newUser.getName());
//sendMessage("server got the following: "+newUser.getPassword());
userAuthenticated = false;
}while(running);
System.out.println("Ending Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
User
public class User implements Serializable{
String name = "";
String password = "";
boolean userAuthenticated = false;
User(String name, String password) {
this.name = name;
this.password = password;
}
User(){}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setAthentication(boolean userAuthenticated) {
this.userAuthenticated = userAuthenticated;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public boolean getAthentication() {
return userAuthenticated;
}
}
答案 0 :(得分:4)
正在发生的事情是,即使在程序可以创建和启动下一个线程之前,后台线程也在完成,这就是为什么要让每个工作者分开。获得您想要的输出。你可以添加一个睡眠,就像埃弗特提到的那样:
def doSomething (x):
time.sleep(.01)
for num in range(3):
print (num, x)