WEB开发网      婵犵數濞€濞佳囧磹婵犳艾鐤炬い鎰堕檮閸嬬喐銇勯弽銊с€掗梻鍕閺岋箑螣娓氼垱笑闂佽姘﹂褔婀佸┑鐘诧工妤犲憡绂嶉崜褏纾奸弶鍫涘妼缁楁岸鏌熷畡鐗堝殗闁诡喒鏅犲畷褰掝敃閵堝棙顔忔繝鐢靛仦閸ㄥ爼骞愰幘顔肩;闁规崘绉ぐ鎺撳亹闁绘垶锕╁Λ鍕⒑閹肩偛濡奸悗娑掓櫇缁顓兼径妯绘櫇闂佹寧绻傞弻濠囨晝閸屾稓鍘甸柣搴㈢⊕閿氶柣蹇ョ稻缁绘繃绻濋崘銊т紝闂佽鍨伴崯鏉戠暦閻旂⒈鏁傞柛鈾€鏅欑槐妯衡攽閻愬樊鍤熷┑顔藉劤铻為柛鏇ㄥ墯閸欏繘鏌嶉崫鍕櫣缂佲偓婢跺绠鹃柟瀛樼箘閿涘秵顨ラ悙顏勭伈闁诡喖缍婂畷鎯邦槻婵℃彃顭烽弻娑㈠Ω閵夈儺鍔夌紓浣稿€哥粔褰掑极閹剧粯鏅搁柨鐕傛嫹 ---闂傚倷鐒︾€笛兠洪埡鍛闁跨噦鎷�
开发学院WEB开发Jsp 谈谈TCP和UDP的一些简单应用 阅读

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

 2008-01-05 20:08:09 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姇閹碱偆鐥閺屾洘绻濊箛鏇犵獥闂佺厧澹婃禍婊堚€旈崘顏佸亾閿濆骸澧ù鐘欏嫮绠鹃柛顐ゅ枎閻忋儲銇勯弴妯哄姦妞ゃ垺鐟╅幃鈩冩償閵囧府鎷�
核心提示:网络编程中最重要的就是SOCKET,它其实也就是监听端口的原理,谈谈TCP和UDP的一些简单应用,和我们用手机发短信的原理应该是大致无二(我是这样理解的),而java最出色的一点也就是“无痛苦连网”,下面我们分别用两个SOCKET下演示: eg1://Clients.javaimport java.io.*;impor
  网络编程中最重要的就是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

Tags:谈谈 TCP UDP

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接