tcp和udp的区别,测试实例.doc_第1页
tcp和udp的区别,测试实例.doc_第2页
tcp和udp的区别,测试实例.doc_第3页
tcp和udp的区别,测试实例.doc_第4页
tcp和udp的区别,测试实例.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package com.server;import java.io.OutputStreamWriter;import .Socket;public class TCPClient /* * param args * author zzy */public static void main(String args) throws Exception Socket s = new Socket(, 8083);OutputStreamWriter w = new OutputStreamWriter(s.getOutputStream();w.write(这里是客户端,Hello服务器,准备发到服务器端);w.flush();w.close();s.close();package com.server;import java.io.InputStreamReader;import .ServerSocket;import .Socket;public class TCPServer /* * param args * author zzy */public static void main(String args) throws Exception ServerSocket ss = new ServerSocket(8083);while (true) Socket s = ss.accept();System.err.println(A client has heen connected.);System.err.println(这里是服务器端+TCPServer.class);InputStreamReader r = new InputStreamReader(s.getInputStream();int c = 0;while (c = r.read() -1) System.err.print(char) c);r.close();s.close();package com.server;import java.io.IOException;import .DatagramPacket;import .DatagramSocket;import .InetAddress;public class UDPClient private bytebuffer= new byte1024;private DatagramSocketds= null;/* * 构造函数,创建UDP客户端 * * throws Exception * author zzy */public UDPClient() throws Exception ds = new DatagramSocket();/* * 设置超时时间,该方法必须在bind方法之后使用. * * param timeout * 超时时间 * throws Exception * author zzy */public final void setSoTimeout(final int timeout) throws Exception ds.setSoTimeout(timeout);/* * 获得超时时间. * * return 返回超时时间 * throws Exception * author zzy */public final int getSoTimeout() throws Exception return ds.getSoTimeout();public final DatagramSocket getSocket() return ds;/* * 向指定的服务端发送数据信息. * * param host * 服务器主机地址 * param port * 服务端端口 * param bytes * 发送的数据信息 * return 返回构造后俄数据报 * throws IOException * author zzy */public final DatagramPacket send(final String host, final int port,final byte bytes) throws IOException DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);ds.send(dp);return dp;/* * 接收从指定的服务端发回的数据. * * param lhost * 服务端主机 * param lport * 服务端端口 * return 返回从指定的服务端发回的数据. * throws Exception * author zzy */public final String receive(final String lhost, final int lport)throws Exception DatagramPacket dp = new DatagramPacket(buffer, buffer.length);ds.receive(dp);String info = new String(dp.getData(), 0, dp.getLength();return info;/* * 关闭udp连接. * */public final void close() try ds.close(); catch (Exception ex) ex.printStackTrace();/* * 测试客户端发包和接收回应信息的方法. * * param args * throws Exception * author zzy */public static void main(String args) throws Exception UDPClient client = new UDPClient();String serverHost = ;int serverPort = 3366;client.send(serverHost, serverPort, (你好!).getBytes();String info = client.receive(serverHost, serverPort);System.out.println(服务端回应数据: + info);package com.server;import java.io.IOException;import .DatagramPacket;import .DatagramSocket;import .InetAddress;import .InetSocketAddress;import .SocketException;/* * author zzy * */public class UDPServer private bytebuffer= new byte1024;private DatagramSocketds= null;private DatagramPacketpacket= null;private InetSocketAddresssocketAddress= null;private StringorgIp;/* * 构造函数,绑定主机和端口. * * param host * 主机 * param port * 端口 * throws Exception */public UDPServer(String host, int port) throws Exception socketAddress = new InetSocketAddress(host, port);ds = new DatagramSocket(socketAddress);System.out.println(服务端启动!);public final String getOrgIp() return orgIp;/* * 设置超时时间,该方法必须在bind方法之后使用. * * param timeout * 超时时间 * throws Exception */public final void setSoTimeout(int timeout) throws Exception ds.setSoTimeout(timeout);/* * 获得超时时间. * * return 返回超时时间. * throws Exception */public final int getSoTimeout() throws Exception return ds.getSoTimeout();/* * 绑定监听地址和端口. * * param host * 主机IP * param port * 端口 * throws SocketException */public final void bind(String host, int port) throws SocketException socketAddress = new InetSocketAddress(host, port);ds = new DatagramSocket(socketAddress);/* * 接收数据包,该方法会造成线程阻塞. * * return 返回接收的数据串信息 * throws IOException */public final String receive() throws IOException packet = new DatagramPacket(buffer, buffer.length);ds.receive(packet);orgIp = packet.getAddress().getHostAddress();String info = new String(packet.getData(), 0, packet.getLength();System.out.println(接收信息: + info);return info;/* * 将响应包发送给请求端. * * param bytes * 回应报文 * throws IOException */public final void response(String info) throws IOException System.out.println(客户端地址 : + packet.getAddress().getHostAddress()+ ,端口: + packet.getPort();DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet.getAddress(), packet.getPort();dp.setData(info.getBytes();ds.send(dp);/* * 设置报文的缓冲长度. * * param bufsize * 缓冲长度 */public final void setLength(int bufsize) packet.setLength(bufsize);/* * 获得发送回应的IP地址. * * return 返回回应的IP地址 */public final InetAddress getResponseAddress() return packet.getAddress();/* * 获得回应的主机的端口. * * return 返回回应的主机的端口. */public final int getResponsePort() return packet.getPort();/* * 关闭udp监听口. * */public final void close() try ds.close(); catch (Exception ex) ex.printStackTrace();/* * 测试方法. * * param ar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论