预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共26页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

第十四章目标:14.1.1网络编程概述14.1.1网络编程概述14.1.1网络编程概述14.1.1网络编程概述14.1.1网络编程概述14.1.1网络编程概述14.1.1网络编程概述14.3.1InetAddress14.3.2流式通信协议连接通信14.3.3Socket14.3.4ServerSocket14.3.5流式通信示例14.3.5流式通信示例服务器端程序address.setText(InetAddress.getLocalHost().getHostName()); …… } publicvoidactionPerformed(ActionEvente){ JButtonb=(JButton)e.getSource(); if(b.equals(btnstart)){ try{ Stringp=port.getText(); if(!p.equals("")){//启动服务器 mysocket=newServerSocket( Integer.parseInt(port.getText())); connect=mysocket.accept(); l5.setText(“服务器已经启动!监听端口 在"+p+"。"); } else{ JOptionPane.showMessageDialog( this,“请输入端口号!","提示信息", JOptionPane.INFORMATION_MESSAGE); } }catch(Exceptionee){ l5.setText("服务器启动错误!");} } if(b.equals(btnconvert)){ try{ BufferedReaderin=newBufferedReader(new InputStreamReader(connect.getInputStream())); PrintWriterout=newPrintWriter (connect.getOutputStream(),true); Stringline=in.readLine(); oldword.setText(line); newword.setText(line.toUpperCase()); out.println(line.toUpperCase()); }catch(Exceptionioe){l5.setText("数据传输错误!"); }}} publicstaticvoidmain(Stringargs[])throwsIOException{ SocketServerclient=newSocketServer(); } } 客户端程序try{ Stringh=address.getText(); Stringp=port.getText(); if(h.equals("")||p.equals("")){ JOptionPane.showMessageDialog(this,“请输入服务 器名称和端口号!","提示信息", JOptionPane.INFORMATION_MESSAGE); }else{ connect=newSocket(InetAddress.getByName(h), Integer.parseInt(p)); l5.setText("连接服务器成功!"); } }catch(Exceptionee){l5.setText("连接服务器失败!");} } if(b.equals(btnsend)){ try{ PrintWriterout=newPrintWriter (connect.getOutputStream(),true); BufferedReaderin=newBufferedReader(new InputStreamReader(connect.getInputStream())); out.println(word.getText()); Stringline=in.readLine(); newword.setText(line); }catch(Exceptionioe){l5.setText("数据传输错误!");} }} publicstaticvoidmain(Stringargs[]){ SocketClientclient=newSocketClient(); } }14.4.4数据报通信协议14.4.4数据报通信协议总结: