网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.使用JFreeChart创建图象.
.Java核心代码例程之:ShowFileCo.
.给所有刚学JAVA以及常来此版的兄.
.JDK1.5使用总结 --《Java 1.5 Ti.
.面向服务架构(SOA)推荐方法(组.
.Java中的17种常用正则表达式归纳.
.剖析.Net下的数据访问层技术(5).
.eval 方法.
.DudoJ框架——最初的设想.
.一个初学者初识Java内部类.
.Think in java 3rd 中文版5.
.Tomcat中怎样设置虚拟目录.
.线程基础---wait(),notify的应用.
.DOM文档操作和XML文件互换java实.
.让自己学会写的一个在线升级的程.
.使用档案文件提高applet的加载速.
.简单的Java循环控制实现行列式算.
.如何使用JSTL标签做页面资源国际.
.实例讲解:Java中的SOAP技术应用.
.Properties类不直接支持多语言属.

谈谈TCP和UDP的一些简单应用

发表日期:2008-1-5


  网络编程中最重要的就是SOCKET,它其实也就是监听端口的原理。和我们用手机发短信的原理应该是大致无二(我是这样理解的),而Java最出色的一点也就是“无痛苦连网”。

  网络最基本的精神就是让两台机器连接起来,“被呼叫的一方”也就是服务器,而“找人的一方”则叫做客户机,所以说在连接中服务器、客户机也就是一个相对的概念了。而我们对机器的标识主要是通过IP地址和端口来区分的。

  “传输控制协议”TCP和“用户数据报协议”是两种不同的协议,JAVA对这两种协议的支持基本是一致的,而它们本身最大的区别也就是发送的可靠性和速率,前者相比后者是可靠协议,后者当然是速度快得多了,下面我们分别用两个SOCKET下演示:

eg1:

//Clients.java

import java.io.*;
import java.net.*;


public class Clients
{
public static void main(String[] args) throws Exception
{
InetAddress addr = InetAddress.getByName(null);
Socket socket = new Socket(addr,2000);
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
byte[] b = new byte[2048];
String msg = new String(b,0,System.in.read(b));
out.println(msg);
socket.close();
}
}


//Servers.java


import java.io.*;
import java.net.*;

public class Servers
{
public static void main(String[] args) throws Exception
{
ServerSocket s = new ServerSocket(2000);
try{
while(true){
Socket socket = s.accept();
try{
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
StringBuffer sb = new StringBuffer();
int c;
while( (c = in.read()) != -1 ){
char ch = (char)c;
sb.append(ch);
}
System.out.println(sb.toString());

}catch(IOException e){
socket.close();
}finally{
socket.close();
}
}//while
}finally{
s.close();
}//try
}//main
}


此程式主要用Servers来进行无限监听,而Clients是客户机发送程式,他们的端口全采用2000。


eg2:

//UDPsend.java
import java.io.*;
import java.net.*;

/**
* This class sends the specified text or file as a datagram to the
* specified port of the specified host.
**/
public class UDPSend {
public static final String usage =
"Usage: java UDPSend ...\n" +
" or: java UDPSend -f ";

public static void main(String args[]) {
try {
// Check the number of arguments
if (args.length < 3)
throw new IllegalArgumentException("Wrong number of args");

// Parse the arguments
String host = args[0];
int port = Integer.parseInt(args[1]);

// Figure out the message to send.
// If the third argument is -f, then send the contents of the file
// specified as the fourth argument. Otherwise, concatenate the
// third and all remaining arguments and send that.
byte[] message;
if (args[2].equals("-f")) {
File f = new File(args[3]);
int len = (int)f.length(); // figure out how big the file is
message = new byte[len]; // create a buffer big enough

上一篇:基于Java的移动游戏开发入门 人气:639
下一篇:循速渐进学用Session Bean(三) 人气:1183
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐