我有兩個客戶端,以及中間的服務器。我正試圖在客戶端之間發送信息文本。Java - 在客戶端之間發送
它工作在前兩秒,但它只接收來自其他客戶端的信息。
這裏是服務器代碼處理該消息發送:
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);
}
}
);
}
}
我很確定這是sendMessage方法不工作。 – theOGloc
你的方法是錯誤的。您正在創建兩個端口,但您應該使用一個端口並使用線程,以便服務器可以接受任意數量的連接。 – avijendr
但我只想要兩個連接。 – theOGloc