当用户按下Enter键时, 当前行被发送到客户端。客户端有两个文本区域:一个用于接收 来自服务器的文本,另一个用于输入文本。当用户 按Enter键,当前行被发送到服务器。
当我运行程序时,客户端和服务器没有发送任何东西 为什么??可以帮助吗?
client.java
package sheet4_q7_client;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Sheet4_Q7_Client extends JFrame{
String s="";
/**
* @param args the command line arguments
*/
private JTextArea jtaServer = new JTextArea();
// Text area for displaying client text
private JTextArea jtaClient = new JTextArea();
private PrintWriter output,output1,output2;
public static void main(String[] args) {
new Sheet4_Q7_Client();
}
public Sheet4_Q7_Client() {
// Place text area on the frame
setLayout(new GridLayout(2, 1));
JScrollPane jScrollPane1 = new JScrollPane(jtaServer);
JScrollPane jScrollPane2 = new JScrollPane(jtaClient);
jScrollPane1.setBorder(new TitledBorder("Server"));
jScrollPane2.setBorder(new TitledBorder("Client"));
add(jScrollPane2, BorderLayout.CENTER);
add(jScrollPane1, BorderLayout.CENTER);
jtaServer.setWrapStyleWord(true);
jtaServer.setLineWrap(true);
jtaClient.setWrapStyleWord(true);
jtaClient.setLineWrap(true);
// jtaClient.setEditable(false);
setTitle("Exercise30_12Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the frame
setVisible(true); // It is necessary to show the frame here!
try {
// Create a server socket
Socket connectToServer = new Socket("localhost", 8000);
//output = new
//PrintWriter(connectToServer.getOutputStream());
new SendThread(connectToServer).start();
new ReceiveThread(connectToServer).start();
}
catch(IOException ex) {
System.err.println(ex);
}
jtaServer.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_BACK_SLASH) {
s=jtaServer.getText().toString();
}
}
});
}
class SendThread extends Thread {
SendThread(Socket socket) {
// Keep sending to the client
try {
output1 = new PrintWriter(socket.getOutputStream());
output1.write(s);
output1.flush();
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
class ReceiveThread extends Thread {
ReceiveThread(Socket socket) {
// Keep receiving from the client
try {
Scanner input1 = new Scanner(socket.getInputStream());
String s1=input1.nextLine();
jtaServer.setText(s1);
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
}
server.java
package sheet4_q7_server;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Sheet4_Q7_Server extends JFrame{
String s="";
/**
* @param args the command line arguments
*/
private JTextArea jtaServer = new JTextArea();
// Text area for displaying client text
private JTextArea jtaClient = new JTextArea();
private PrintWriter output,output1;
public static void main(String[] args) {
new Sheet4_Q7_Server();
}
public Sheet4_Q7_Server() {
// Place text area on the frame
setLayout(new GridLayout(2, 1));
JScrollPane jScrollPane1 = new JScrollPane(jtaServer);
JScrollPane jScrollPane2 = new JScrollPane(jtaClient);
jScrollPane1.setBorder(new TitledBorder("Server"));
jScrollPane2.setBorder(new TitledBorder("Client"));
add(jScrollPane1, BorderLayout.CENTER);
add(jScrollPane2, BorderLayout.CENTER);
jtaServer.setWrapStyleWord(true);
jtaServer.setLineWrap(true);
jtaClient.setWrapStyleWord(true);
jtaClient.setLineWrap(true);
// jtaClient.setEditable(false);
setTitle("Exercise30_12Server");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the frame
setVisible(true); // It is necessary to show the frame here!
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
// Listen for a new connection request
Socket connectToClient = serverSocket.accept();
output = new
PrintWriter(connectToClient.getOutputStream());
new SendThread(connectToClient).start();
new ReceiveThread(connectToClient).start();
}
catch(IOException ex) {
System.err.println(ex);
}
jtaServer.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER) {
s=jtaServer.getText().toString();
//System.out.print(s);
}
}
});
}
class SendThread extends Thread {
SendThread(Socket socket) {
// Keep sending to the client
try {
output1 = new PrintWriter(socket.getOutputStream());
output1.write(s);
output1.flush();
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
class ReceiveThread extends Thread {
ReceiveThread(Socket socket) {
// Keep receiving from the client
try {
Scanner input1 = new Scanner(socket.getInputStream());
String s1=input1.nextLine();
jtaClient.setText(s1);
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
}
答案 0 :(得分:-2)
看起来你发送和接收线程正在构造函数中执行它们的功能。
尝试改写run方法。
您可能还想在while循环中执行此操作,否则它只会执行一次。