我有两个客户端,服务器介于两者之间。我正在尝试在客户端之间发送信息文本。
它适用于前两秒,但之后它只接收来自其他客户端的信息。
以下是处理邮件发送的服务器代码:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ObjectOutputStream output2;
private ObjectInputStream input2;
private ServerSocket server;
private Socket connection;
private Socket connection2;
private ServerSocket server2;
private String message;
private String message2;
//constructor
public Server(){
super("Server");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
try {
sendMessage();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(300,150);
setVisible(true);
}
//set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
server2 = new ServerSocket(6788,100);
while(true){
try{
waitForConnection();
waitForConnection2();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}finally{
closeConnection();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
showMessage(" Waiting for someone to connect... \n");
connection = server.accept();
showMessage(" Now connected to client 1 \n");
//
}
private void waitForConnection2() throws IOException{
showMessage("Waiting for connection");
connection2 = server2.accept();
showMessage("Now connected to Client 2 \n");
}
//get stream to send and receive data
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
output2 = new ObjectOutputStream(connection2.getOutputStream());
input2 = new ObjectInputStream(connection2.getInputStream());
showMessage("\n Streams are now setup! \n");
}
// during the chat conversation
private void whileChatting() throws IOException{
String message = " You are now connected! ";
String message2 = "jiiii";
try {
sendMessage();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ableToType(true);
// while(!message.equals("CLIENT - END")){
// try{
// message = (String) input2.readObject();
// message2 = (String) input.readObject();
//
//
//// showMessage("\n" + message);
//// showMessage("\n" + message2);
// }catch(ClassNotFoundException classNotFoundException){
// showMessage("\n not valid");
// }
// }
}
//close streams and sockets after you are done chatting
private void closeConnection(){
showMessage("\n Closing connections... \n");
ableToType(false);
try{
output2.close();
input2.close();
connection2.close();
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send a message to client
private void sendMessage() throws ClassNotFoundException{
boolean msg1 = true;
boolean msg2 = false;
try{
while(msg1 == true){
message = (String)input.readObject();
output2.writeObject("Phone" + message);
output2.flush();
msg1 = false;
msg2 = true;
}
while(msg2 == true){
message2 = (String)input2.readObject();
output.writeObject(message2);
output.flush();
msg1 = true;
}
}catch(IOException ioException){
chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE");
}
}
//updates chatWindow
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}
);
}
//let the user type stuff into their box
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
答案 0 :(得分:1)
让我们来看看sendMessage方法的代码。
private void sendMessage() throws ClassNotFoundException {
boolean msg1 = true;
boolean msg2 = false;
try{
while(msg1 == true){
message = (String)input.readObject();
output2.writeObject("Phone" + message);
output2.flush();
msg1 = false;
msg2 = true;
}
while(msg2 == true){
message2 = (String)input2.readObject();
output.writeObject(message2);
output.flush();
msg1 = true;
}
} catch(IOException ioException){
chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE");
}
}
在第二个while循环中,你不会将msg2恢复为false,因此它会在while循环中永远运行。这就是您停止从第一个客户端接收消息并仅接收来自第二个客户端的消息的原因。
但是当你纠正这个问题时,程序将在每个客户端收到1条消息后停止。要解决这个问题,你需要将2个while循环放在另一个while循环中,例如while(true)循环,这样它们就可以交替。
但是这引入了另一个问题,你将无法在程序运行时停止它。一个可能的解决方案可能是您引入了一个真实的局部变量,但当您的一个客户输入一个特定的单词时会更改为false(例如“退出”)。
我在代码中的例子:
private void sendMessage() throws ClassNotFoundException {
boolean msg1 = true;
boolean msg2 = false;
boolean not-quit? = true; //Boolean to stop the program
try{
while(not-quit?) {
while(msg1 == true){
message = (String)input.readObject();
if (message.equalsIgnoreCase("quit") {
not-quit? = false;
}
output2.writeObject("Phone" + message);
output2.flush();
msg1 = false;
msg2 = true;
}
while(msg2 == true){
message2 = (String)input2.readObject();
if (message.equalsIgnoreCase("quit") {
not-quit? = false;
}
output.writeObject(message2);
output.flush();
msg1 = true;
msg2 = false; //So it stops this while-loop
}
}
} catch(IOException ioException){
chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE");
}
}
您使用的这种客户端 - 服务器编程风格并不是那么好。我可以做的另一个建议是你使用Threads实现你的Server,其中每个与Client的通信实例都在一个单独的Thread中,你可以让你的Thread通过Server中的全局变量进行通信。这是一种更清洁的工作方式!