本文介绍了客户端服务器聊天不是双方都在工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过这段代码,我只能从客户端或服务器的一侧向其他框架文本区域发送消息,但不能同时向两侧发送消息
你能告诉我这是什么问题
----------------------------客户代码--- --------------------------
by this code i can send message only from one side either client or server to other frame text area but not both side
could you please tell me whats the problem
----------------------------Client Code-----------------------------
public class ClientForm extends javax.swing.JFrame {
Socket server = null;
DataInputStream dis = null;
DataOutputStream dos = null;
public ClientForm() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btn_connect = new javax.swing.JButton();
txt_msg = new javax.swing.JTextField();
btn_send = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txt_recMsgClient = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client");
setBackground(new java.awt.Color(-65536,true));
btn_connect.setText("Connect");
btn_connect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_connectActionPerformed(evt);
}
});
btn_send.setText("Send");
btn_send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_sendActionPerformed(evt);
}
});
txt_recMsgClient.setBackground(new java.awt.Color(-4144960,true));
txt_recMsgClient.setColumns(20);
txt_recMsgClient.setFont(new java.awt.Font("Monospaced", 2, 14)); // NOI18N
txt_recMsgClient.setForeground(new java.awt.Color(-65536,true));
txt_recMsgClient.setRows(5);
txt_recMsgClient.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(-16711936,true)));
txt_recMsgClient.setSelectedTextColor(new java.awt.Color(-65536,true));
jScrollPane1.setViewportView(txt_recMsgClient);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_connect)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txt_msg, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(btn_send)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btn_connect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txt_msg, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_send))
.addGap(11, 11, 11))
);
pack();
}// </editor-fold>
public void recmsg()
{
try
{
String msg = dis.readUTF();
String oldText = txt_recMsgClient.getText();
StringBuilder yourStringBuilder = new StringBuilder();
yourStringBuilder.append("\n").append(oldText).append(msg);
txt_recMsgClient.setText(yourStringBuilder.toString());
}
catch (IOException ex)
{
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void btn_connectActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
server = new Socket("127.0.0.1",2000);
JOptionPane.showMessageDialog(null, "Connected To Server");
dis = new DataInputStream(server.getInputStream());
dos = new DataOutputStream(server.getOutputStream());
ReceiveMessage clientThread = new ReceiveMessage(dis, txt_recMsgClient);
clientThread.setDaemon(true);
clientThread.setName("Server");
clientThread.start();
} catch (UnknownHostException ex) {
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Connection Failed");
} catch (IOException ex) {
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Connect Failed");
}
}
/**/
private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String m = txt_msg.getText();
try
{
dos.writeUTF(m);
}
catch (IOException ex)
{
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
}
recmsg();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_connect;
private javax.swing.JButton btn_send;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txt_msg;
private javax.swing.JTextArea txt_recMsgClient;
--------------------------服务器代码--------------- ------------------
--------------------------Server Code---------------------------------
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class ServerForm extends javax.swing.JFrame {
ServerSocket server = null;
Socket client = null;
DataInputStream dis = null;
DataOutputStream dos = null;
public ServerForm() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btn_strtServer = new javax.swing.JButton();
btn_send = new javax.swing.JButton();
txt_msg = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
txt_recMsgServer = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Server");
setBackground(new java.awt.Color(-1,true));
btn_strtServer.setText("Start");
btn_strtServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_strtServerActionPerformed(evt);
}
});
btn_send.setText("Send");
btn_send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_sendActionPerformed(evt);
}
});
txt_recMsgServer.setBackground(new java.awt.Color(-16711681,true));
txt_recMsgServer.setColumns(20);
txt_recMsgServer.setForeground(new java.awt.Color(-256,true));
txt_recMsgServer.setRows(5);
jScrollPane1.setViewportView(txt_recMsgServer);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(txt_msg, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(btn_send)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btn_strtServer)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
.addGap(85, 85, 85))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btn_strtServer)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_msg, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_send))
.addContainerGap())
);
pack();
}// </editor-fold>
public void recmsg()
{
try
{
String msg = dis.readUTF();
String oldText = txt_recMsgServer.getText();
StringBuilder yourStringBuilder = new StringBuilder();
yourStringBuilder.append("\n").append(oldText).append(msg);
txt_recMsgServer.setText(yourStringBuilder.toString());
}
catch (IOException ex)
{
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void btn_strtServerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
server = new ServerSocket (2000);
client = server.accept();
JOptionPane.showMessageDialog(null, "Client Request Accepted");
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
ReceiveMessage serverThread = new ReceiveMessage(dis, txt_recMsgServer);
serverThread.setDaemon(true);
serverThread.setName("Client");
serverThread.start();
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, "Client Not Available");
}
}
private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String m = txt_msg.getText();
try
{
dos.writeUTF(m);
}
catch (IOException ex)
{
Logger.getLogger(ServerForm.class.getName()).log(Level.SEVERE, null, ex);
}
recmsg();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ServerForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_send;
private javax.swing.JButton btn_strtServer;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txt_msg;
private javax.swing.JTextArea txt_recMsgServer;
- ----------------------------收到留言代码------------------- ---
-----------------------------Receive Message Code----------------------
import java.io.DataInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
public class ReceiveMessage extends Thread {
String msg = "";
DataInputStream dis = null;
JTextArea txt_area = null;
public ReceiveMessage (DataInputStream d, JTextArea a)
{
this.dis= d;
this.txt_area= a;
}
public void run()
{
while(true)
{
try
{
msg=dis.readUTF();
txt_area.append("\n"+this.getName() + ":" + msg);
}
catch(IOException ex)
{
Logger.getLogger(ReceiveMessage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
推荐答案
这篇关于客户端服务器聊天不是双方都在工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!