java吧 关注:1,304,520贴子:12,866,171
  • 0回复贴,共1

进来看看这个聊天程序是哪儿出问题了????

只看楼主收藏回复

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class server
{private static JFrame jf=new JFrame();
private static JTextField txt=new JTextField(20);
private static JTextField ok=new JTextField(20);
private static JButton bb=new JButton("为爱发送");
public static void main(String[] args)
{String okk=new String();
jf.setTitle("一位业余科学家的信息服务器");
jf.setVisible(true);
jf.setSize(600,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());
jf.add(txt);jf.add(bb);jf.add(ok);
ServerSocket ss=null;
Socket s=null;
OutputStream out=null;
try{ss=new ServerSocket(20023);}
catch(IOException e){System.out.println("呵呵");okk="一位业余科学家的错误";}
while(true)
{try{s=ss.accept();out=s.getOutputStream();BufferedReader ko=new BufferedReader(new InputStreamReader(s.getInputStream()));okk=ko.readLine();}
catch(IOException e){System.out.println("哈哈");}
PrintStream ff=new PrintStream(out);
bb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{final String fs=new String(txt.getText());ff.println(fs);}});
ok.setText(okk);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class client
{private static JTextField xs=new JTextField(20);
private static JTextField fs=new JTextField(20);
private static JFrame jf=new JFrame();
private static JButton zs=new JButton("为爱发送");
public static void main(String[] args)
{String sd=new String();
jf.setTitle("一位业余科学家的客户端");
jf.setSize(600,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());
jf.add(fs);
jf.add(zs);
jf.add(xs);
OutputStream os=null;
while(true)
{
try{
Socket ss=new Socket("127.0.0.1",20023);
BufferedReader sr=new BufferedReader(new InputStreamReader(ss.getInputStream()));
sd=sr.readLine();
zs.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{final String hs=new String(fs.getText());PrintStream wyy=new PrintStream(os);wyy.println(hs);}});
}
catch(IOException e)
{
sd="一位业余科学家的错误";
}
xs.setText(sd);
}
}
}


IP属地:广东1楼2019-03-24 02:37回复