简单聊天程序,线程问题

imonkeyi 2018-01-13 03:43:14
我编了一个简单的聊天程序,为收信息设置了一个线程,但是我每次要发两次信息另一端才能收到,且只能收到第二次发的信息,第一次发的收不到,也就是必须是第偶数次发对面才能收到,为什么?
package cn.itcast.chapter11;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class test1 extends JFrame implements ActionListener,WindowListener{

static final int PORT=6699;//服务端端口号
static final String IP="172.27.80.187";//服务端IP地址
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸,使窗口居中
JTextArea area=new JTextArea();//文本域,用于放信息
JTextField tfield=new JTextField(30);//文本框,用于放要发的信息
JButton but=new JButton("确定");
JButton but1=new JButton("连接");
JPanel panel=new JPanel();
JLabel label=new JLabel("信息");
static Socket client;
String reMsg;
String Msg;
String sendMsg,tfieldMsg;
DataInputStream dis;
DataOutputStream dos;
boolean thstate=true;

/*static {//先用服务端的IP地址和端口号实例化socket
try {
client=new Socket(IP,PORT);
}catch(Exception e) {
e.printStackTrace();
}
}*/

test1(){//构造函数,设置界面
setSize(640,420);
setLocation(screen.width/2-320,screen.height/2-210);
area.setLineWrap(true);
setTitle("客户端");
area.setEditable(false);

panel.add(label);
panel.add(tfield);
panel.add(but);
panel.add(but1);

add(area,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH);
but.addActionListener(this);
but1.addActionListener(this);
tfield.addActionListener(this);
addWindowListener(this);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {//按确定时发信息给服务端并显示在文本域中
if(e.getSource()==but||e.getSource()==tfield) {
sendButton();
}
if(e.getSource()==but1) {
connectButton();
}

}

public void connectButton() {
try {
client=new Socket(IP,PORT);
dos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
}catch(Exception e) {
e.printStackTrace();
}
receiveMsg();
}

public void sendButton() {
sendMsg=tfield.getText();
try {
//DataOutputStream dos=new DataOutputStream(client.getOutputStream());
if(sendMsg!="") {

Msg=area.getText();
tfieldMsg="你发的信息:"+sendMsg+"\r\n";
area.setText(Msg+tfieldMsg);
dos.writeUTF(sendMsg);
//dos.close();
}
}catch(Exception IO) {
IO.printStackTrace();
}

tfield.setText("");
}

public void receiveMsg() {//收信息函数,当有信息是显示在文本域中
ReceiveThread receivethread=new ReceiveThread();
receivethread.start();
/*if(thstate) {
receivethread.interrupt();
}*/
}

class ReceiveThread extends Thread {
public void run() {
while(thstate) {
try {

reMsg=dis.readUTF();
if(reMsg!="") {
Msg=area.getText();
reMsg="接受到信息:"+dis.readUTF()+"\r\n";
area.setText(Msg+reMsg);
}

reMsg="";

}catch(Exception e) {
e.printStackTrace();
}
}
}
}

public void windowClosing(WindowEvent e) {//关闭窗口时关闭socket
if(client!=null) {
try {
client.close();
dis.close();
dos.close();
thstate=false;
}catch(Exception e1) {
e1.printStackTrace();
}}
Window window=e.getWindow();
window.dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {//主函数,实例化自己,调用收信息函数收信息
test1 test=new test1();
//test.receiveMsg();
}
}[code=java]package cn.itcast.chapter11;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class test3 extends JFrame implements ActionListener,WindowListener{

static final int PORT=6699;//定义服务端的端口号
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸使窗口居中
JTextArea area=new JTextArea();
JTextField tfield=new JTextField(45);
JButton but=new JButton("确定");
JPanel panel=new JPanel();
JLabel label=new JLabel("信息");
static ServerSocket server;
static Socket client;
String reMsg,Msg;
String sendMsg,tfieldMsg;
DataOutputStream dos;
DataInputStream dis;
boolean thstate=true;
/*static {//静态代码块,先实例化serversocket并得到socket
try {
server=new ServerSocket(PORT);
client=server.accept();
}catch(Exception e) {
e.printStackTrace();
}
}*/

test3(){//构造函数,设置界面
setSize(640,420);
setLocation(screen.width/2-320,screen.height/2-210);
area.setLineWrap(true);
setTitle("服务端");
area.setEditable(false);

panel.add(label);
panel.add(tfield);
panel.add(but);

add(area,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH);
but.addActionListener(this);
tfield.addActionListener(this);
addWindowListener(this);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
try {
server=new ServerSocket(PORT);
area.setText("等待连接中。。。");
client=server.accept();
area.setText("等待连接中。。。"+"\r\n"+"已连接,开始聊天。。。"+"\r\n");
dos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
receiveMsg();
}catch(Exception e) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {//按确定时发信息给服务端并显示在文本域中
sendMsg=tfield.getText();
try {
//DataOutputStream dos=new DataOutputStream(client.getOutputStream());
if(sendMsg!="") {

Msg=area.getText();
tfieldMsg="你发的信息:"+sendMsg+"\r\n";
area.setText(Msg+tfieldMsg);
dos.writeUTF(sendMsg);

}
}catch(Exception IO) {
IO.printStackTrace();
}

tfield.setText("");
}
public void receiveMsg() {//收信息函数,当有信息是显示在文本域中
ReceiveThread receivethread=new ReceiveThread();
receivethread.start();
/*if(thstate) {
receivethread.interrupt();
}*/

}

class ReceiveThread extends Thread{
public void run(){
while(thstate) {
Msg=area.getText();
try {
//DataInputStream dis=new DataInputStream(client.getInputStream());
reMsg=dis.readUTF();
if(reMsg!="") {
reMsg="接受到信息:"+dis.readUTF()+"\r\n";
area.setText(Msg+reMsg);
}
reMsg="";
}catch(Exception e) {
e.printStackTrace();
}
}
}
}

public void windowClosing(WindowEvent e) {//关闭窗口时关闭socket
try {
client.close();
server.close();
dis.close();
dos.close();
thstate=false;
}catch(Exception e1) {
e1.printStackTrace();
}
Window window=e.getWindow();
window.dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
/*public void close(Socket client)throws Exception {
client.getOutputStream().close();
client.getInputStream().close();
client.close();
}*/
public static void main(String[] args) {//主函数,实例化自己,调用收信息函数收信息
test3 test=new test3();
//test.receiveMsg();
}
}
[/code]
...全文
617 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
donnasky1 2018-01-15
  • 打赏
  • 举报
回复
学习学习,看看
super攻城狮 2018-01-15
  • 打赏
  • 举报
回复
class ReceiveThread extends Thread { public void run() { while(thstate) { try { reMsg=dis.readUTF(); if(!reMsg.equals("")) { Msg=area.getText(); reMsg="接受到信息:"+reMsg+"\r\n"; area.setText(Msg+reMsg); } reMsg=""; }catch(Exception e) { e.printStackTrace(); } } } } 我改了下,接收消息正常
OwenZeng_DBA 2018-01-13
  • 打赏
  • 举报
回复
调试一下,缩小问题范围先
二月十六 2018-01-13
  • 打赏
  • 举报
回复
建议到java版问问

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧