我正在尝试制作一个java服务器/客户端程序,但我不明白为什么它不起作用,即使我在一个单独的线程中运行循环,我的服务器界面也不显示设法在某些时候出现端口错误,我甚至不知道该尝试什么,所以这里是代码: (我在一台计算机上执行了两个程序)
我做错了什么?
客户端:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client extends JFrame {
String sentence;
String modifiedSentence;
int port = 6791;
JTextField tf = new JTextField();
JButton valider = new JButton("Send");
JLabel retour = new JLabel("nothing");
Socket clientSocket;
DataOutputStream outToServer;
BufferedReader inFromServer;
public Client() throws UnknownHostException, IOException{
this.setSize(200, 130);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Client");
this.setResizable(false);
JPanel zoneclient = (JPanel)getContentPane();
Dimension d = new Dimension(300 , 25);
tf.setPreferredSize(d);
tf.setMaximumSize(d);
retour.setPreferredSize(d);
valider.setPreferredSize(d);
zoneclient.setLayout(new BoxLayout(zoneclient, BoxLayout.Y_AXIS));
zoneclient.add(new JLabel("Client"));
zoneclient.add(tf);
zoneclient.add(retour);
zoneclient.add(valider);
valider.addActionListener(new appActionListener());
}
class appActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
sentence = tf.getText();
try {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
clientSocket = new Socket("localhost", port);
clientSocket.setReuseAddress(true);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (Exception f) {
// TODO: handle exception
System.out.println("connexion failed");
}
try {
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
retour.setText(modifiedSentence);
clientSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
tf.setText("error");
}
}
}
}
这是服务器:
package server;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class MyServer extends JFrame{
ServerSocket welcomeSocket;
String clientSentence;
String capitalizedSentence;
JLabel retour = new JLabel("nothing");
public MyServer() throws IOException{
System.out.println("server started");
this.setSize(200, 130);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Server");
this.setResizable(false);
JPanel zoneclient = (JPanel)getContentPane();
Dimension d = new Dimension(300 , 25);
retour.setPreferredSize(d);
zoneclient.setLayout(new BoxLayout(zoneclient, BoxLayout.Y_AXIS));
zoneclient.add(new JLabel("Server"));
zoneclient.add(retour);
welcomeSocket = new ServerSocket(6791);
welcomeSocket.setReuseAddress(true);
MyThread th = new MyThread();
th.run();
}
class MyThread extends Thread{
public void run(){
while (true) {
Socket connectionSocket;
try {
connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
retour.setText("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase();
outToClient.writeBytes(capitalizedSentence);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("???");
}
}
}
}
}
答案 0 :(得分:1)
问题是客户端代码等待来自服务器的新行字符\n
。由于您正在调用流的readLine
。
modifiedSentence = inFromServer.readLine();
<强>解决方案:强>
将服务器代码更改为\n
,如下所示。
outToClient.writeBytes(capitalizedSentence+'\n');
注意: 我认为您不需要new BufferedReader(new nputStreamReader(System.in))
内的actionPerformed()