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

这是个c/s的做加法的程序,client输入server计算并返回结果.

现在问题是能计算结果但client取不回来.

源码:

client~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import java.net.*; import java.awt.*; import java.awt.event.*; import java.io.*;

class Client1 extends Frame { static private TextField taIn1,taIn2; static private TextArea taOut; static private Panel p,p1; static private Button bSub,bClr,bres; static private Label l1,l2,l3; //变量定义 public Client1(){ super("通讯程序客户端"); taIn1=new TextField("",10); taIn2=new TextField("",10); taOut=new TextArea("",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY); taOut.setEditable(false); taOut.setBackground(Color.black); taOut.setForeground(Color.white); p=new Panel(); p1=new Panel(); bSub=new Button("SUBMIT"); bClr=new Button("CLEAR"); bres=new Button("RESULT"); l1=new Label("加数:"); l2=new Label("被加数:"); this.add(taOut,"North"); this.add(p1,"Center"); //框架 p1.add(l1); p1.add(taIn1); p1.add(l2); p1.add(taIn2); //输入文本框 p.add(bSub); p.add(bClr); p.add(bres); //按钮 this.addWindowListener(new MyWindowListener()); this.add(p,"South"); this.pack(); this.setSize(430,300); this.setVisible(true); } public static void main(String[] args) { Client1 gtxs=new Client1(); bSub.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ String s3; taOut.setText("输入的两位运算数值为:\n"+taIn1.getText()+'\t'+taIn2.getText()+'\n'); //s3= tongXun(); //taOut.setText(s3); } }); bClr.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ taOut.setText(""); taIn1.setText(""); taIn2.setText(""); } }); bres.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ // } });

}//end main public static String tongXun(){ Socket sClient; String s,s1=new String(""); BufferedWriter bw; BufferedReader br; int i=0; //变量定义 try{ sClient=new Socket("127.0.0.1",1024); bw=new BufferedWriter( new OutputStreamWriter(sClient.getOutputStream())); br=new BufferedReader( new InputStreamReader(sClient.getInputStream())); }catch(UnknownHostException e) { taOut.setText("UnknownHostException Happened."); return("UnknownHostException Happened."); } catch(IOException e) { taOut.setText("can not connect with Server."); return("can not connect with Server."); } taOut.setText("Connect Server Success."); //while(true){ try{ s=taIn1.getText()+'\n'+taIn2.getText()+'\n'; bw.write(s); bw.flush(); bw.close(); }catch(IOException e) { taOut.setText(taOut.getText()+'\n'+"write data error."); } while(i==0){ try{ while((s=br.readLine())!=null) {s1=s; i=1;} }catch(IOException e) { taOut.setText(taOut.getText()+'\n'+"read data error."); return("read error."); } }//end while 2 return(s1); //}//end while

}//end tongXun() }// end class Client1 class MyWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } }

server~~~~~~~~~~~~~~~~~~

import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*;

class Server extends Frame { static private TextArea taOut; public Server(){ super("通讯程序服务器端"); Label l=new Label("接受到的客户输入如下..."); taOut=new TextArea("",20,20,TextArea.SCROLLBARS_VERTICAL_ONLY); taOut.setEditable(false); this.addWindowListener(new MyWindowListener()); this.add(l,"North"); this.add(taOut,"Center"); this.setSize(430,300); this.setVisible(true); service(); }

public static void main(String[] args) { Server gtxs=new Server(); } // end main public static void service() { ServerSocket serverGui=null; Socket sGui; BufferedReader br; BufferedWriter bw; String[] s; s=new String[2]; s[0]=new String(); s[1]=new String(); String s1,s2=new String(); //变量定义 try{ serverGui=new ServerSocket(1024); }catch(IOException e) { taOut.setText("Server cannot be created."); System.exit(0); //return; } taOut.setText("now server is started."+'\n'); while(true) { try{ sGui=serverGui.accept(); bw=new BufferedWriter( new OutputStreamWriter(sGui.getOutputStream())); br=new BufferedReader( new InputStreamReader(sGui.getInputStream())); for(int i=0;i<=1&&(s1=br.readLine())!=null;i++) s[i]=s1; s2=plus(s); taOut.setText(taOut.getText()+"s"+s+'\n'+"s[0]:"+s[0]+'\n'+"s[1]:"+s[1]+'\n'+"s2:"+s2); bw.write("from server."); taOut.setText(taOut.getText()+"已经送回"); }catch(IOException e) { taOut.setText("cannot read from client."); return; } try{ //bw.write("from server."); bw.flush(); bw.close(); }catch(IOException e){taOut.setText("write data error.");} try{ taOut.setText(taOut.getText()+"\nserver will be closed."); sGui.close(); }catch(IOException e){taOut.setText("server closes error.");} }//end while

}//end service

static String plus(String[] ss){ float a; String s; a=Float.parseFloat(ss[0])+Float.parseFloat(ss[1]); s=Float.toString(a); return s; } } class MyWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } }

高手帮忙给看看,多谢了.在线等.................

搜索更多相关主题的帖子: 源码 在线 
2004-12-15 10:40
快速回复:IO问题~在线等~附源码
数据加载中...
 
   



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

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