私のメインメソッドの変数 "name"をrun()メソッドに渡そうとしています。Javaスレッドに変数を渡す(run()メソッド)
私はこの方法how can I pass a variable into a new Runnable declaration?
を試みた。しかし、私はそれを動作させることができませんでした。その変数をどのように渡すことができますか? (私はフルでプログラムを実行するために必要な他のクラスを挿入した。)
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.net.InetAddress;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
public class Server {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
try {
final int port = 8181;
ServerSocket server = new ServerSocket(port);
System.out.println("Server is up and running, waiting for clients to connect.");
while (true) {
Socket sock = server.accept();
System.out.println("A new user has connected");
System.out.println("Type \"LIST\" to get a list of commands");
System.out.println("Enter your Username"); final String name = input.nextLine();
System.out.println("Now go to the client console to enter your messages");
new Thread(new Client(sock)).start();
}
} catch (Exception e) {
System.out.println("An error occured.");
e.printStackTrace();
}
}
static class Client implements Runnable {
private Socket socket;
int id;
public Client(Socket sock)
{
id = 1;
socket = sock;
}
@Override
public void run() {
long jvmUpTime = ManagementFactory.getRuntimeMXBean().getUptime();
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
try {
Scanner in = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String data = in.nextLine();
System.out.println("User : " + data);
if (data.startsWith("LEAVENOW")) {
System.out.println("Client has left the server");
break;
}
if (data.startsWith("GETIP")) {
// System.out.println("Client connected from " +
// InetAddress.getLocalHost());
System.out.println("Client connected from " + InetAddress.getLocalHost());
}
if (data.startsWith("SERVERTIME")) {
System.out.println(mxBean.getUptime());
}
/*
* if (data.startsWith("SETNAME")) {
*
* Scanner input = new Scanner(System.in);
*
* System.out.println("Enter your Username"); final String
* name = input.nextLine();
* System.out.println("Press 1 to confirm your user name");
* int namenum = input.nextInt(); if (namenum == 1){
*
* System.out.println("name changed"); }
*
*
* }
*/
if (data.startsWith("LIST")) {
System.out.println("Here is a list of commands that the user can use in the server");
System.out.println(
"LEAVENOW = exit the server \nGETIP = Gets the IP address of the server \nSERVERTIME = The amount of milliseconds the server has been running \n ");
}
// send the line back to the client
// or whatever custom message we want
// out.println(line);
// out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
を追加クラス私は私のコードを変更し、編集した@Bergerのアドバイスに満ちプログラム
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ClientInstance {
final static int port = 8181;
final static String host = "localhost";
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) throws IOException {
try {
Socket sock = new Socket(host, port);
System.out.println("You connected to " + host);
System.out.println("Now enter your message ");
new Thread(new ClientWriter(sock)).start();
new Thread(new ClientReader(sock)).start();
} catch (Exception CannotConnect) {
System.err.println("Could not connect to the server please try again, if error proceeds restart IDE");
}
}
static class ClientWriter implements Runnable {
public Socket socket;
public ClientWriter(Socket sock) {
socket = sock;
}
public void run() {
try {
Scanner chat = new Scanner(System.in);
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String input = chat.nextLine();
out.println(input);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class ClientReader implements Runnable {
public Socket socket;
public ClientReader(Socket sock) {
socket = sock;
}
public void run() {
try {
/* Scanner scanner = new Scanner(socket.getInputStream());
//read data from client
while(true) {
String data = scanner.nextLine();
System.out.println("Received data! : " + data);
}
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
を実行するために、この部分。私がしたのは、ソケットソックのように、文字列名をパラメータとして追加することだけでした。しかし、run()メソッドでname変数を呼び出すと、文字列はnullを出力します。
new Thread(new Client(sock, name)).start();
}
} catch (Exception e) {
System.out.println("An error occured.");
e.printStackTrace();
}
}
static class Client implements Runnable {
private Socket socket;
int id;
String name;
public Client(Socket sock, String name)
{
id = 1;
socket = sock;
name = name;
}
'Client'ランナブルのパラメータとして追加してください:' Client(ソケットソック、文字列名) '、ソケットで既に行ったように保存して' new Client(sock、name) 'を呼び出す – Berger
Iそれをやろうとしましたが、入力した名前の代わりにnullが出力されます。私は私のコードにしたものをオリジナルの投稿に編集して表示します。 – LookingWest
渡された値がnullではありませんでしたか?名前はまだ読んだことがありますか? –