A:IP地址
a:点分十进制
b:IP地址的组成
c:IP地址的分类
d:dos命令
e:InetAddress
B:端口
是应用程序的标识。范围:0-65535。其中0-1024不建议使用。
C:协议
UDP:将数据源和目的封装成数据包中,不需要建立连接;每个数据报的大小在限制在64k;因无连接,是不可靠协议;不需要建立连接,速度快
TCP:建立连接,形成传输数据的通道;在连接中进行大数据量传输;通过三次握手完成连接,是可靠协议;必须建立连接,效率会稍低
A:通信两端都应该有Socket对象
B:所有的通信都是通过Socket间的IO进行操作的
发送:
创建UDP发送端的Socket对象
创建数据并把数据打包
发送数据
释放资源
package tongxin;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* 创建UDP发送端的Socket对象
* 创建数据并把数据打包
* 发送数据
* 释放资源
* @author moon
*
*/
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端的Socket对象
DatagramSocket ds = new DatagramSocket();
//封装键盘录入数据
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
String lineString = null;
while((lineString = bReader.readLine()) != null) {
if("886".equals(lineString)) {
break;
}
//创建数据并打包
byte[] bys = lineString.getBytes();
DatagramPacket dPacket = new DatagramPacket(bys, bys.length,
InetAddress.getByName("localhost"), 12345);
//发送数据
ds.send(dPacket);
}
//释放资源
ds.close();
}
}
接收:
创建UDP接收端的Socket对象
创建数据包用于接收数据
接收数据
解析数据包
释放资源
package tongxin;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
*
* 创建UDP接收端的Socket对象
* 创建数据包用于接收数据
* 接收数据
* 解析数据包
* 释放资源
* @author moon
*
*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象
DatagramSocket dpSocket = new DatagramSocket(999);
while(true) {
//创建一个包裹
byte[] bys = new byte[1024];
DatagramPacket dpPacket = new DatagramPacket(bys, bys.length);
//接收数据
dpSocket.receive(dpPacket);
//解析数据
String ipString = dpPacket.getAddress().getHostAddress();
String s = new String(dpPacket.getData() , 0, dpPacket.getLength());
System.out.println("from " + ipString + "data is :" + s);
}
//释放资源
//dpSocket.close();
}
}
发送:
创建TCP客户端的Socket对象
获取输出流,写数据
释放资源
package tongxin;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost" , 7777);
//获取输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你是小可爱吗".getBytes());
//获取输入流
InputStream inputStream = socket.getInputStream();
byte[] b = new byte[1024];
int len = inputStream.read(b);
String client = new String(b, 0, len);
System.out.println("client:" + client);
socket.close();
}
}
接收:
创建TCP服务器端的Socket对象
监听客户端连接
获取输入流,读取数据
释放资源
package tongxin;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(7777);
Socket socket = serverSocket.accept();
//获取输入流
InputStream is = socket.getInputStream();
byte[] b = new byte[1024];
int len = is.read(b);
String serve = new String(b, 0, len);
System.out.println("serve: " + serve);
//获取输出流
OutputStream os = socket.getOutputStream();
os.write("数据已收到".getBytes());
serverSocket.close();
socket.close();
}
}
A:UDP
a:把发送数据改进为键盘录入
package lianxi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SendDemo {
public static void main(String[] args) throws IOException {
// 创建发送端的Socket对象
DatagramSocket ds = new DatagramSocket();
// 封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
// 创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("localhost"), 12345);
// 发送数据
ds.send(dp);
}
// 释放资源
ds.close();
}
}
package lianxi;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// 创建接收端的Socket对象
DatagramSocket ds = new DatagramSocket(12345);
while (true) {
// 创建一个包裹
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
// 接收数据
ds.receive(dp);
// 解析数据
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from " + ip + " data is : " + s);
}
// 释放资源
// 接收端应该一直开着等待接收数据,是不需要关闭
// ds.close();
}
}
B:TCP
d:客户端键盘录入服务器写到文本文件
package lianxi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/*
* 键盘录入,服务器端以a.txt接收
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 33333);
//键盘录入
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
//把通道中的流给包装一下
BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = null;
while((line = bReader.readLine()) != null) {
if("886".equals(line)) {
break;
}
bWriter.write(line);
bWriter.newLine();
bWriter.flush();
}
socket.close();
}
}
package lianxi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServeDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(33333);
//监听客户端连接
Socket socket = serverSocket.accept();
//封装通道中的流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//封装文本文档
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("a.txt"));
String lineString = null;
while((lineString = bufferedReader.readLine()) != null) {
bufferedWriter.write(lineString);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
socket.close();
}
}