第15讲 Java网络程序设计(一).ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
//获得新套接口soc绑定的输入流InputStream对象 is=soc.getInputStream(); //构造数据输入流DataInputStream对象in in=new BufferedReader(new InputStreamReader(is)); //获得新套接口soc绑定的输出流OutputStream对象 os=soc.getOutputStream(); out=new PrintStream(os); //构造输出流PrintStream 对象out //获到客户方的IP地址,soc绑定了与它连接的客户地址 InetAddress clientIP=soc.getInetAddress(); //在服务方显示客户方的IP地址 System.out.println(Clients IP address:+clientIP); int port; port=soc.getPort(); //得到客户方的端口 System.out.println(Clients port:+port); //在服务方显示客户方的端口 Java网络程序设计 * out.println(Welcome!...); //向客户发送… String str=in.readLine(); //在in上读一行 /*当输入流读入的不是”quit”时,将输入的数据写入输出流中,在屏幕上显示,然后继续从输入流中读入,不断循环,直到输入”quit”为止*/ while(!str.equals(“quit”)){ //如读出的不是“quit”,继续读 System.out.println(Client said:+str); str=in.readLine(); } System.out.println(Client want to leave.); } catch(Exception e) { System.out.println(Error:+e); } Java网络程序设计 * //关闭输入输出流及Socket finally{ is.close(); //关闭输入流 os.close(); //关闭输出流 soc.close(); //关闭socket System.exit(0); } } } //服务器程序结束 Java网络程序设计 * (2)客户方源程序 import .*; import java.io.*; public class TcpClient{ static public void main(String args[]) throws IOException{ Socket soc=null; InputStream is=null; OutputStream os=null; DataInputStream in=null; PrintStream out=null; String strin=null; try{ // 客户创建一个与指定的服务者固定端口连接的Socket soc=new Socket(localhost,8000); System.out.println(Connecting to the Server...); Java网络程序设计 * is=soc.getInputStream(); os=soc.getOutputStream(); in=new DataInputStream(is); out=new PrintStream(os); strin=in.readLine(); System.out.println(Server said:+strin); byte bmsg[] = new byte[20]; System.in.read(bmsg); String msg=new String(bmsg,0); msg=msg.trim(); /*将键入的数据写入输出流中,然后继续从键盘读入,不断循环,直到输入”quit”为止*/ while(!msg.equals(quit)){ out.println(msg); System.in.read(bmsg); Java网络程序设计 * msg=new String(bmsg,0); msg=msg.trim(); } out.println(msg); } ca

文档评论(0)

ygxt89 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档