Socket 编程的初级问题

zhongxin 2003-12-17 03:27:30
package Chat;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends Frame implements ActionListener{
Panel p = new Panel();
Panel p1 = new Panel();
TextArea ta = new TextArea(5, 20);
Button btn = new Button("Send");
Button btn_conn = new Button("Connect");
Label lbl_ip = new Label("IP :");
Label lbl_port = new Label("Port :");
TextField t_ip = new TextField("ms-p4",15);
TextField t_port = new TextField("6666",10);
private String ip,port;
ObjectInputStream input;
ObjectOutputStream output;
Socket connection;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn_conn)
if(this.Connect())
{
new myThread().run();
}
else
return;
if(e.getSource()==btn)
this.sendMes();
}

public Client() {
super("Client");
p.add(lbl_ip);
p.add(t_ip);
p.add(lbl_port);
p.add(t_port);
btn_conn.addActionListener(this);
p.add(btn_conn);
this.setLayout(new BorderLayout());
this.add(p,BorderLayout.NORTH);
p1.add(new JScrollPane(ta));
p1.add(btn);
this.add(p1,BorderLayout.CENTER);
btn.addActionListener(this);
this.setSize(400,300);
this.setVisible(true);
}

public void sendMes()
{
try
{
output.writeObject(this.ta.getText());
output.flush();
ta.setText("");
ta.requestFocus();

}
catch(Exception e){JOptionPane.showMessageDialog(null,"Send message wrong!");}

}

public boolean Connect()
{
try{
connection = new Socket(t_ip.getText(), Integer.parseInt(t_port.getText()));
//JOptionPane.showMessageDialog(null, "Connect Successfully!");
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
return true;

}
catch(Exception e)
{
System.out.println(e.toString());
return false;
}
}
public void begin()
{
try {
connection = new Socket("ms-p4", 6666);
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
String message = "";
while (true) {
if (input != null)
{
message = (String) input.readObject();
ta.append("\nClient has received this client's message:");
this.ta.append("\n" + message );
ta.setCaretPosition(ta.getText().length());
}
}

}
catch (Exception e) {
//JOptionPane.showMessageDialog(null, "服务器未打开或已经关闭!");
}
}


public static void main(String args[]) {
Client client = new Client();
client.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
class myThread extends Thread{
public void run()
{
String message = "";
try{
while (true) {
if (input != null) {
message = (String) input.readObject();
ta.append("\nClient has received this client's message:");
ta.append("\n" + message);
ta.setCaretPosition(ta.getText().length());
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"an error accurred while listening.");
}

}
}
}

当客户端与server正常建立链接后,就如同死机了,我知道是进入了死循环,但如何将这个循环函数放进另外一个线程里面执行?
...全文
114 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
POwner 2003-12-17
  • 打赏
  • 举报
回复
说的不错,说不定就可以呢,我们等好消息吧
dreamnear 2003-12-17
  • 打赏
  • 举报
回复
把建立连接的东东,放到一个继承于Thread的类中,并重载run()方法,不太懂AWT什么的,写段伪代码吧:
public class SonThread extends Thread{
public SonThread(){}
public void run()//重载的run()方法
{
//add your code here
}
}
在调用的时候,可以这样:

SonThread sThread = new SonThread();
sThread.start();/*注意,这里虽然是调用run方法,为了能并发,只能用start(),用run()不成*/


偶只能说这么多。不到之处楼下继续~

zhongxin 2003-12-17
  • 打赏
  • 举报
回复
但如果不用按钮事件连接直接用new client().begin()就不会出现死掉的现象(其实一样也在进行死循环)可以和服务器端正常通讯,为什么呢?
zhongxin 2003-12-17
  • 打赏
  • 举报
回复
up
zhongxin 2003-12-17
  • 打赏
  • 举报
回复
倒~我在做一个java课的作业,不是什么项目啊
Demonx 2003-12-17
  • 打赏
  • 举报
回复
感觉很像NIIT的项目,我回去赵找看.

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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