0
我試圖讓如何使一個Java服務器/客戶端程序,但我不明白爲什麼它不起作用,即使我運行我的服務器的界面不顯示在一個單獨的線程循環,我設法得到一些端口錯誤,我甚至不知道該怎麼嘗試了,所以這裏的代碼: (我在一臺計算機上執行這兩個程序)Java簡單的TCP客戶端/服務器測試凍結
什麼是我做錯了?
客戶端:
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("???");
}
}
}
}
}
哉!這是它的感謝!但你知道爲什麼我的服務器GUI不顯示嗎? – javaNoob49854
以及爲什麼每次運行編程時都必須更改端口,或者我在線程「main」java.net.BindException中獲取異常:已使用的地址:JVM_Bind? – javaNoob49854
沒關係,作爲一個白癡從來沒有幫助過編程:我使用了thread.run;而不是thread.start(); – javaNoob49854