| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 344 人关注过本帖
标题:有关通讯器的问题!
只看楼主 加入收藏
trhuang
Rank: 1
等 级:新手上路
帖 子:126
专家分:0
注 册:2007-4-1
收藏
 问题点数:0 回复次数:0 
有关通讯器的问题!

最近我在学做通讯器!做了简单的服务端和客户端,但是出现了些问题,希望各位可以帮帮忙!
问题:两个运行后,服务端看不到客户端输入的字!
代码:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class client extends JFrame implements ActionListener{
Socket sock;
JTextArea t1=new JTextArea();
JTextField t2=new JTextField(20);
JButton b1=new JButton("send");
JButton b2=new JButton("link...");
DataOutputStream out;
DataInputStream in;
public client()
{
JScrollPane jsp=new JScrollPane(t1);
this.getContentPane().add(jsp,"Center");
JPanel p1=new JPanel();
p1.add(t2);
p1.add(b1);
JPanel p2=new JPanel();
p2.add(b2);
this.getContentPane().add(p2,"North");
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("client");
setSize(340,200);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{try{
out.writeUTF("bye");
}
catch(Exception ee)
{}
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
if(!t2.getText().equals("")){
try{
out.writeUTF(t2.getText());
t1.append("client say:"+t2.getText()+"\n");
}
catch(Exception ee){}
}
}
else{
try{
sock=new Socket("127.0.0.1",6000);
OutputStream os=sock.getOutputStream();
out=new DataOutputStream(os);
InputStream is=sock.getInputStream();
in=new DataInputStream(is);
out.writeUTF("ok");
Communion th=new Communion(this);
th.start();
}
catch(IOException ee){
JOptionPane.showMessageDialog(null,"err");
return;
}
}
}
public static void main(String f[])
{
client mainframe=new client();
}
}
class Communion extends Thread {
client fp;
Communion(client fp){
this.fp=fp;
}
public void run()
{
String msg=null;
while(true)
{
try{
msg=fp.in.readUTF();
if(msg.equals("bye")){
fp.t1.append("close\n");
break;
}
fp.t1.append("sever say:"+msg+"\n");
}
catch(Exception ee){break;
}
}
try{
fp.out.close();
fp.in.close();
fp.sock.close();
}
catch(Exception ee){}
}
}

****************************************************************8
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class sever extends JFrame implements ActionListener{
ServerSocket severSock;
Socket sock;
JTextArea t1=new JTextArea();
JTextField t2=new JTextField(20);
JButton b1=new JButton("send");
DataOutputStream out;
DataInputStream in;
String cname=null;
public sever()
{
try{
severSock =new ServerSocket(6000);
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"err");
return;
}
JScrollPane jsp=new JScrollPane(t1);
this.getContentPane().add(jsp,"Center");
JPanel p1=new JPanel();
p1.add(t2);p1.add(b1);
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
setTitle("sever");
setSize(340,200);
setVisible(true);
try{
sock =severSock.accept();
out=new DataOutputStream(sock.getOutputStream());
in=new DataInputStream(sock.getInputStream());
out.writeUTF("ok");
Communion th=new Communion(this);
th.start();
}
catch(Exception e){}
addWindowListener(new WindowAdapter()
{
public void windowCloseing(WindowEvent e){
try{
out.writeUTF("bye");
}
catch(Exception ee){}
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(! t2.getText().equals("")){
try{
out.writeUTF(t2.getText());
t1.append("sever say:"+t2.getText()+"\n");
}
catch(Exception ee){}
}
}


public static void main(String d[])
{
sever mainFrame=new sever();
}
}


class Communion extends Thread{
sever fp;
Communion(sever fp){
this.fp=fp;
}
public void run()
{
String msg=null;
while(true){
try{
msg=fp.in.readUTF();
if(msg.equals("bye")){
fp.t1.append("leave\n");
break;
}
fp.t1.append("client say:"+msg+"\n");
}
catch(Exception ee){break;}
}
try{
fp.out.close();
fp.in.close();
fp.sock.close();
fp.severSock.close();
}
catch(Exception ee){}
}
}

搜索更多相关主题的帖子: 通讯器 
2007-04-18 22:35
快速回复:有关通讯器的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041044 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved