------ android培训、java培训、java学习型技术博客、期待与您交流! ----------
网络编程
1 UDP协议
UDP协议是面相无连接的协议并且数据的传输形式数据报包。
与UDP传输协议本身相关的类就是DatagramSocket类。
与UDP每次传输数据形式相关的类就是DatagramPacket类。
UDP 发送数据及键盘录入
package cn.itcast.nets;
import java.net.*;
//UDP发送端
public class UDPSend {
public static void main(String[] args) throws Exception{
//建立Socket服务
DatagramSocket ds = new DatagramSocket(8000);
//封装数据包
byte[] buf = "你好UDP".getBytes();
DatagramPacket dp = new DatagramPacket
(buf, 0,buf.length,InetAddress.getByName("192.168.3.100"),10000);
//发送数据
ds.send(dp);
ds.close();
}
}
package cn.itcast.nets;
import java.net.*;
import java.io.*;
//UDP发送端,获取用户的键盘录入,发送给接收端
public class UDPSendKey {
public static void main(String[] args)throws Exception {
//建立Socket服务,指定发送端口8000
DatagramSocket ds = new DatagramSocket(8000);
//键盘录入吗,读取一行,转换流,读取的缓冲区对象
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
//读一行,封装一个数据包,发送出去
String line = null;
while((line = bfr.readLine())!=null){
byte[] buf = line.getBytes();
//封装数据包
DatagramPacket dp = new
DatagramPacket(buf, 0,buf.length,InetAddress.getByName("192.168.3.255"),10000);
ds.send(dp);
if("886".equals(line))
break;
}
bfr.close();
ds.close();
}
}
UDP 端接收数据及键盘输出
package cn.itcast.nets;
import java.net.*;
//UDP的接收端
public class UDPReceive {
public static void main(String[] args)throws Exception {
//建立Socket服务,监听10000端口
DatagramSocket ds = new DatagramSocket(10000);
//建立字节数组,接收发送端的数据
byte[] buf = new byte[1024];
//封装数据包
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//Socket服务接收
ds.receive(dp);
//开始解包,利用数据包对象中的getLength()方法,获得字节数组中的有效个数
int length = dp.getLength();
//获取发送方法IP地址,使用数据包对象getAddress()返回的是发送端的IP地址对象,通过地址对象获取IP地址
/*InetAddress i = dp.getAddress();
String ip = i.getHostAddress();*/
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
System.out.println(new String(buf,0,length)+":::"+ip+":::"+port);
ds.close();
}
}
package cn.itcast.nets;
import java.net.*;
//接收端,接收用户的发送的数据,保证不能退出
public class UDPReceiveKey {
public static void main(String[] args) throws Exception{
//建立Socket服务,监听10000端口
DatagramSocket ds = new DatagramSocket(10000);
while(true){
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收并拆包
ds.receive(dp);
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String message = new String(buf,0,length);
if("886".equals(message)){
System.out.println(ip+"..."+port+"离开聊天程序");
}else
System.out.println(message +" "+ ip+"..."+port);
}
}
}
多线程来实现 UDP
package cn.itcast.nets;
import java.net.*;
//多线程实现单一控制台的接收与发送
public class ThreadTest {
public static void main(String[] args)throws Exception {
//负责启动发送端和接收端
//建立接收端的Socket和发送端Socket服务,传递给线程控制的发送端和接收端
DatagramSocket send = new DatagramSocket(8000);
DatagramSocket receive = new DatagramSocket(10000);
new Thread(new ThreadUDPSend(send)).start();
new Thread(new ThreadUDPReceive(receive)).start();
}
}
package cn.itcast.nets;
import java.net.*;
import java.io.*;
//UDP发送端,线程进行控制
public class ThreadUDPSend implements Runnable{
//实现发送,需要将发送端的Socket服务,传递给多线程
private DatagramSocket ds;
public ThreadUDPSend(DatagramSocket ds){ this.ds = ds;}
public void run(){
try{
//获取键盘录入
BufferedReader bfr = new
BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = bfr.readLine())!=null){
byte[] buf = line.getBytes();
DatagramPacket dp = new
DatagramPacket(buf, 0,buf.length,InetAddress.getByName("192.168.3.100"),10000);
ds.send(dp);
if("886".equals(line))
break;
}
bfr.close();
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
package cn.itcast.nets;
import java.net.*;
//多线程控制接收端
public class ThreadUDPReceive implements Runnable{
//需要传递Socket服务
private DatagramSocket ds;
public ThreadUDPReceive(DatagramSocket ds){this.ds = ds;}
public void run(){
while(true){
try{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收并拆包
ds.receive(dp);
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String message = new String(buf,0,length);
if("886".equals(message))
System.out.println(ip+"..."+port+"离开聊天程序");
else
System.out.println(message +" "+ ip+"..."+port);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
2 TCP协议
TCP协议是面相连接 的协议。创建的数据传输的两个Socket通信端点用的并不是同一个Socket类。
通信的一方程序称为服务器Socket端点,使用ServerSocket类进行创建。通信的另一方程序称为客户端Socket端点,使用Socket类进行创建。
TCP客户端
package cn.itcast.nets;
//实现TCP客户端
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String[] args)throws Exception {
//建立Socket服务,指定连接的主机和端口号
//建立对象的一行代码,只要一执行,连接相应的服务端
Socket s = new Socket("192.168.3.100",10000);
//通过建立好的Socket服务,获取相应的流对象,数据发送到服务端,输出流、
OutputStream out = s.getOutputStream();
out.write("你好TCP".getBytes());
//接收服务端的数据
InputStream in = s.getInputStream();
byte[] buf = new byte[10];
int len = 0 ;
len = in.read(buf);
System.out.println(new String(buf,0,len));
//关闭资源
s.close();
}
}
TCP服务端
package cn.itcast.nets;
//实现TCP的服务端
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String[] args) throws Exception{
//建立服务端的Socket 服务,监听端口
ServerSocket ss = new ServerSocket(10000);
//通过服务端的Socket对象,获取客户端连接对象 accept()
Socket s = ss.accept();
//通过客户端对象,获取相应的IO流
InputStream in = s.getInputStream();
byte[] buf = new byte[10];
int len = 0 ;
len = in.read(buf);
//将读取到的数组变成字符串打印出来
//System.out.println(new String(buf,0,len));
//获取客户端IP地址
String ip = s.getInetAddress().getHostAddress();
int port = s.getPort();
String message = new String(buf,0,len);
System.out.println(message+" :::"+ip+":::"+port);
//向客户端会送一个消息
OutputStream out = s.getOutputStream();
out.write("我收到了,谢谢".getBytes());
//关闭资源
s.close();
ss.close();
}
}
多线程实现TCP传输
package cn.itcast.nets;
import java.net.*;
//多线程实现单一控制台的接收与发送
public class ThreadTest {
public static void main(String[] args)throws Exception {
//负责启动发送端和接收端
//建立接收端的Socket和发送端Socket服务,传递给线程控制的发送端和接收端
DatagramSocket send = new DatagramSocket(8000);
DatagramSocket receive = new DatagramSocket(10000);
new Thread(new ThreadUDPSend(send)).start();
new Thread(new ThreadUDPReceive(receive)).start();
}
}
package cn.itcast.nets;
import java.net.*;
import java.io.*;
//UDP发送端,线程进行控制
public class ThreadUDPSend implements Runnable{
//实现发送,需要将发送端的Socket服务,传递给多线程
private DatagramSocket ds;
public ThreadUDPSend(DatagramSocket ds){ this.ds = ds;}
public void run(){
try{
//获取键盘录入
BufferedReader bfr = new
BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = bfr.readLine())!=null){
byte[] buf = line.getBytes();
DatagramPacket dp = new
DatagramPacket(buf, 0,buf.length,InetAddress.getByName("192.168.3.100"),10000);
ds.send(dp);
if("886".equals(line))
break;
}
bfr.close();
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
package cn.itcast.nets;
import java.net.*;
//多线程控制接收端
public class ThreadUDPReceive implements Runnable{
//需要传递Socket服务
private DatagramSocket ds;
public ThreadUDPReceive(DatagramSocket ds){this.ds = ds;}
public void run(){
while(true){
try{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收并拆包
ds.receive(dp);
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String message = new String(buf,0,length);
if("886".equals(message))
System.out.println(ip+"..."+port+"离开聊天程序");
else
System.out.println(message +" "+ ip+"..."+port);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
------ android培训、java培训、java学习型技术博客、期待与您交流! ----------