快速业务通道

网络编程:谈谈TCP和UDP的一些简单应用 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-07-04
while((bytes_read < len)&& (n != -1)); } else { // Otherwise, just combine all the remaining arguments. String msg = args[2]; for (int i = 3; i < args.length; i++) msg += " " + args[i]; message = msg.getBytes(); }    // Get the internet address of the specified host InetAddress address = InetAddress.getByName(host);    // Initialize a datagram packet with data and address DatagramPacket packet = new DatagramPacket(message, message.length, address, port);    // Create a datagram socket, send the packet through it, close it. DatagramSocket dsocket = new DatagramSocket(); dsocket.send(packet); dsocket.close(); } catch (Exception e) { System.err.println(e); System.err.println(usage); } } } //UDPreceive.java import java.io.*; import java.net.*;    /** * This program waits to receive datagrams sent the specified port. * When it receives one, it displays the sending host and prints the * contents of the datagram as a string. Then it loops and waits again. **/ public class UDPReceive { public static final String usage = "Usage: java UDPReceive "; public static void main(String args[]) { try { if (args.length != 1) throw new IllegalArgumentException("Wrong number of args");    // Get the port from the command line int port = Integer.parseInt(args[0]);    // Create a socket to listen on the port. DatagramSocket dsocket = new DatagramSocket(port);    // Create a buffer to read datagrams into. If anyone sends us a // packet containing more than will fit into this buffer, the // excess will simply be discarded! byte[] buffer = new byte[2048];    // Create a packet to receive data into the buffer DatagramPacket packet = new DatagramPacket(buffer, buffer.length);    // Now loop forever, waiting to receive packets and printing them. for(;;) { // Wait to receive a datagram dsocket.receive(packet);    // Convert the contents to a string, and display them String msg = new String(buffer, 0, packet.getLength()); System.out.println(packet.getAddress().getHostName() + ": " + msg);    // Reset the length of the packet before reusing it. // Prior to Java 1.1, we''d just create a new packet each time. packet.setLength(buffer.length); } } catch (Exception e) { System.err.println(e); System.err.println(usage); } } }

在UDP中主要的类是DatagramSocket()和DatagramPacket(),而在UDPreceive中,被接受的字节是受限制,这些感觉不是太好,既然buf是一个字节数组,我们实在是很奇怪为什么构建器自己不能调查出数组的长度呢?唯一能猜测的原因就是C风格的编程使然,那里的数组不能自己告诉我们它有多大。

而我们实际使用的过程中,当然不仅仅限于这些,其中要考虑有多台客户机来连接服务器,所以要考虑到线程Thread的使用,如果再加上SWING,就可以做一个类似于QQ的SOCKET功能了,这仅仅限于我在学习SOCKET时的一些领悟。供大家参考。

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号