Я много читал о многопоточном клиенте, но для этого я не могу сделать его многопоточным!
не могли бы вы помочь мне?
public class MainClient implements Runnable{
private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;
public static String getText() {
return text;
}
public static void setText(String text) {
MainClient.text = text;
}
private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MainFrame farme = new MainFrame();
farme.setVisible(true);
try {
c = new Socket("localhost", 5050);
os = new PrintWriter(c.getOutputStream(), true);
is = new BufferedReader(new InputStreamReader(c.getInputStream()));
} catch (UnknownHostException ex) {
Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void active() {
String teXt = MainClient.getText();
System.out.println(teXt);
os.println(teXt);
try {
String line = is.readLine();
System.out.println("Text received: " + line);
os.flush();
is.close();
is.close();
c.close();
} catch (IOException ex) {
Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Также активный метод будет вызван, когда клиент напишет что-нибудь в текстовой области, а затем нажмет кнопку отправки.
2) также у меня есть вопрос, который:
в другом классе я выполнил это действие для моей кнопки отправки, означает ли это, что клиент является многопоточным ??
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Thread(new Runnable() {
@Override
public void run() {
// This gets run in a background thread
String text = jTextArea1.getText();
jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
MainClient.setText(client.getCurrentName() + " : " + text + "\n");
clear();
MainClient.active();
}
}).start();
}
Последнее редактирование:
это мой активный метод:
public static void active() {
String teXt = MainClient.getText();
os.println(teXt);
String line = is.readLine();
System.out.println("Text received: " + line);
os.flush();
is.close();
is.close();
c.close();
}