1,网络通讯中的三个要素
2,IP简述使用:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test1 {
public static void main(String[] args) {
try {
//本地主机ip地址对象
InetAddress ip=InetAddress.getLocalHost();
//获取其他主机ip地址对象
ip=InetAddress.getByName("192.168.100");
System.out.println(ip.getHostAddress());//地址
System.out.println(ip.getHostName());//主机名
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4,两个类
DatagramPacket
5,UDP建立发送端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* 需求:通过udp传输方式,将一段文字数据发送出去
* 1,建立udpSocket服务
* 2,提供数据,并将数据封装到数据包中。
* 3,通过socket服务的发送功能,将数据发出去
* 4,关闭资源
*/
public class UdpSend {
public static void main(String[] args) {
try {
// 创建udp服务通过DatagramSocket对象
DatagramSocket ds = new DatagramSocket(8888);//可以不指定,系统随机分配
// 确定数据,将数据封装成包
byte[] b = "aaa".getBytes();
try {
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress
.getByName("192.168.1.100"), 10000);
ds.send(dp);// 通过socket服务发送数据包
ds.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6,udp建立接受端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Scanner;
/*
* 需求:
* 定义一个应用程序,用于接收ydp协议传输的数据并处理的
* 思路
* 1,定义udpsocket服务.通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识
* 方便于明确,那些数据过来,该应用程序可以处理
* 2,定义一个数据包,因为要存储接收到的字节数据。
* 因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
* 3,通过socket服务receive方法将收到的数据存入已定义好的数据包。
* 4,通过数据包对象的特有功能。讲这些不同的数据取出,打印在控制台上。
* 5,关闭资源
*
*/
public class UdpRe {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
// 1建立udp socket,建立端点
ds = new DatagramSocket(1000);// 系统可以分配
// 2定义数据包,
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
// 3通过服务的receive方法将收到的数据存入数据包中。
ds.receive(dp);//阻塞式方法,没有数据就等,线程机制wait,发过来数据的时候notify唤醒
// 4,通过数据包的方法获取其中的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, b.length);
int post = dp.getPort();
System.out.println(ip + "::" + data + "::" + post);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ds.close();
}
}
}
/*
* 聊天程序
* 有接受数据的部分和发数据的部分
* 并且同时执行
* 多线程技术
* 因为收数据和发数据是不一致的,所有要定义两个run方法,所以要有两个类
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Test9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DatagramSocket sendsocket = new DatagramSocket();//发送端
DatagramSocket recesocket = new DatagramSocket(10001);//接受端必须制定端口号
Thread t1 = new Thread(new Send(sendsocket));//发送端线程
Thread t2 = new Thread(new Rece(recesocket));//接收端 线程
t1.start();
t2.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 发送端类
*
* @author Administrator
*/
class Send implements Runnable {
private DatagramSocket ds;//udpsocket服务
public Send(DatagramSocket ds) {// 初始化一个发送端DatagramSocket对象
this.ds = ds;
}
//重写run方法,键盘录入io操作
public void run() {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// 键盘录入
try {
String line = null;
while ((line = br.readLine()) != null) {// 循环读取
if (line.equals("86"))//自定义结束标记
break;
// 构造数据包,封装了数据,接受ip,接受端口,存储数据
byte[] b = line.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress
.getByName("192.168.1.100"), 10001);//封装数据包
ds.send(dp);//发送
}
} catch (Exception e) {
// TODO Auto-generated catch block
try {
throw new Exception("发送端失败");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
ds.close();//关闭资源
}
}
}
/*
* 接收 发送端类
*
* @author Administrator
*/
class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {// 初始化一个接受端DatagramSocket对象,接受数据 必须明确一个端口号
this.ds = ds;
}
@Override
public void run() {// 重写线程run方法
// TODO Auto-generated method stub
while (true) {
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);//创建数据包,用于存储接受的数据
try {
ds.receive(dp);// 阻塞式方法,将接受的数据存储到数据包中
// 通过数据包获取ip,和转换后的字符串
String ip = dp.getAddress().getHostName();//通过数据包的方法解析包中的数据
String data = new String(dp.getData(), 0, dp.getLength());
System.out.println("ip" + ip + "data" + data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
9,TCP建立客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 客户端
* 通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
* 因为tcp是面向连接的,所以在建立连接socket服务时。
* 就要有服务端存在,并连接成功。形成通路后,在该通道进行数据的传输。
* 需求;给服务端发送一个文本数据
* 步骤
*
* 1,创建TCP 客户端Socket服务,使用Socket对象。并指定要连接的主机和端口
* 建议该对象一创建就明确目的地。要连接主机
* 2,如果连接成功,说明数据的通道已建立
* 该通道是Socket流,是底层的,既然是流,说明这里既有输入又有输出
* 可以通过getOutputStream(),和getInputSteam()来获取两个字节流
* 3,使用输出流。将数据写出
* 4,关闭资源
*/
public class TcpClient {
public static void main(String[] args) {
try {
// 创建客户端的socket服务,指定目的主机和端口
Socket s = new Socket("192.168.1.100", 10001);
// 为了发送数据,应该获取socket流中的输出流
OutputStream out = s.getOutputStream();
out.write("aaa".getBytes());
s.close();// socket关闭,流也关闭了
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) {
Socket s = null;
ServerSocket ss = null;
try {
// 建立服务端的socket,并监听一个端口
ss = new ServerSocket(10001);
// 通过accept方法获取连接过来的客户端对象
s = ss.accept();// 阻塞式的方法
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip:" + ip);
// 获取客户端发送过来的数据,那么要使用客户端对象的读取流方法来读取数据
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
System.out.println(new String(buf, 0, buf.length));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 不关自己,关闭客户端
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 客服端
* 1,建立socket服务指定要连接主机和端口子
* 2,获取socket流中的输出流,将数据写到该流中,通过网络发送给服务端
*
*/
public class TcpClient {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.1.240", 10002);
OutputStream out = s.getOutputStream();
out.write("服务端,你好".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read();
System.out.println(new String(buf, 0, len));
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) {
// 服务端接受客户端发送过来的数据,并打印在控制台上
/*
* 通过tcp服务的思路 1,建立服务端socket服务。通过ServerSocket对象 2,服务端必须对外提供一个借口,否则客户端无法连接
* 3,获取链接过来的客户端对象 4,通过客户端对象获取socket流读取客户端发来的数据 并打印在控制台上 5,关闭资源,管客户端,管服务端
*/
try {
// 1创建服务端对象
ServerSocket ss = new ServerSocket(10002);
// 获取连接过来的客户端对象
Socket s = ss.accept();
// 2获取ip
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip:" + ip);
// 通过socket对象获取输入流,要对客户端发过来的数据
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf, 0, len));
// 使用客户端socket对象的输出流给客户端返回数据
OutputStream out = s.getOutputStream();
out.write("收到".getBytes());
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端上传图片
单人
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 单人上传图片
* 客户端。
* 1,服务端点
* 2,去读客户端已有的图片数据
* 3,通过socket输出流将数据发送给服务端
* 4,读取服务端反馈信息
* 5,关闭
*/
public class PicClient {
public static void main(String[] args) throws Exception, IOException {
Socket s = new Socket("192.168.1.100", 10004);
FileInputStream fis = new FileInputStream("c:\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
out.write(buf, 0, len);
}
// 告诉服务端数据已写完
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufin = new byte[1024];
int num = in.read(bufin);
System.out.println(new String(bufin, 0, num));
fis.close();
s.close();
}
}
class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("c:\\server.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
客户端并发上传图片
加上while的方案
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 单人上传图片
* 客户端。
* 1,服务端点
* 2,去读客户端已有的图片数据
* 3,通过socket输出流将数据发送给服务端
* 4,读取服务端反馈信息
* 5,关闭
*/
public class PicClient {
public static void main(String[] args) throws Exception, IOException {
Socket s = new Socket("192.168.1.100", 10004);
FileInputStream fis = new FileInputStream("c:\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
out.write(buf, 0, len);
}
// 告诉服务端数据已写完
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufin = new byte[1024];
int num = in.read(bufin);
System.out.println(new String(bufin, 0, num));
fis.close();
s.close();
}
}
class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10004);
while(true){
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("c:\\server.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
}
这个服务端有个局限性。当a客户端连接上以后,被服务端获取到,服务端执行具体执行流程,
这时b客户端连接,只有等待
因为服务端还没有处理完A客户端的请求,还没有循环回来执行accept方法。所以暂时获取不到B客户对象。
那么为了可以让多个客户端同时并发访问服务
那么服务端最好就是将每个客户端封装到一个单独的线程中。这样,就可以同属处理多个客户端请求。
如何定义线程呢?
只要明确了每一个客户端要在服务端执行的代码即可。将该代码存入run方法中。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class PicClient {
public static void main(String[] args) throws Exception, IOException {
// 主函数传值
if (args.length != 1) {
System.out.println("请选择一个jpd格式的图片");
return;
}
File file = new File(args[0]);//根据传入的值,实例file对象
if (!(file.exists() && file.isFile())) {//判断是否存在,是不是文件
System.out.println("该文件有问题,要么不存在,要么不是文件");
return;
}
if (!(file.getName().endsWith(".jpg"))) {//判断是不是以.jpg为后缀的
System.out.println("图片格式错误,请重新选择");
return;
}
if (file.length() > 1024 * 1024 * 5) {//对文件的大小进行设置
System.out.println("文件过大");
return;
}
Socket s = new Socket("192.168.1.100", 10004);//指定ip和端口号
FileInputStream fis = new FileInputStream("c:\\1.jpg");//得到文件输入流,根据文件所在的路径
OutputStream out = s.getOutputStream();//得到输出流
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {//把文件写入指定的流中。上传到客户端
out.write(buf, 0, len);
}
// 告诉服务端数据已写完
s.shutdownOutput();
InputStream in = s.getInputStream();//得到服务端的反馈信息
byte[] bufin = new byte[1024];//缓冲数组
int num = in.read(bufin);//读取的字节数
System.out.println(new String(bufin, 0, num));//打印输出
fis.close();
s.close();
}
}
class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10004);//服务端Socket指定端口
while (true) {
Socket s = ss.accept();//阻塞式接受
new Thread(new PicThread(s)).start();//线程开启,多线程,并发上传
}
}
}
class PicThread implements Runnable {
private Socket s;
PicThread(Socket s) {
this.s = s;
}
@Override
public void run() {
// TODO Auto-generated method stub
int count = 1;// 如果定义到成员,多个线程共享数据了。不安全
String ip = s.getInetAddress().getHostAddress();
try {
System.out.println(ip + "...connected");
InputStream in = s.getInputStream();
// 为了不覆盖,用ip地址来命名
File file = new File(ip + "(" + (count) + ")" + ".jpg");
while (file.exists()) {//判断文件是否存在
file = new File(ip + "(" + (count++) + ")" + ".jpg");
}
FileOutputStream fos = new FileOutputStream(file);//文件输出流
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());//返回给客户端的信息
fos.close();
s.close();
} catch (Exception e) {
throw new RuntimeException(ip + "上传失败");
}
}
}
客户端并发登陆
客户端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/*
*客户端通过键盘录入用户名。
*服务端对这个用户名进行校验。
*如果该用户名存在,在服务端系那是xxx,已登录
*并在客户端显示xxx欢迎光临。
*如果该用户不存在在服务端显示xxx,尝试登陆。
*并在客户端显示xxx,该用户不存在。
*最多就登陆三次
*
*/
public class LoginClient{
public static void main(String[] args) throws Exception, IOException {
Socket s=new Socket("192.168.1.100",10006);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader bufin=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<3;x++){
String line=bufr.readLine();//读一次
if(line==null){
break;
}
out.println(line);//发出去
String info=bufin.readLine();//读取服务端的反馈信息
System.out.println("info:"+info);//打印反馈信息
if(info.contains("欢迎")){
break;
}
}
bufr.close();
s.close();
}
}
服务端
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class LoinServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10006);
while(true){
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
class UserThread implements Runnable{
private Socket s;
UserThread(Socket s){
this.s=s;
}
@Override
public void run() {
// TODO Auto-generated method stub
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
try{
for(int x=0;x<3;x++){
BufferedReader bufin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=bufin.readLine();
if(name==null){//客户端ctrl+c停止了
break;
}
BufferedReader bufr=new BufferedReader(new FileReader("c:\\user.txt"));
PrintWriter out=new PrintWriter(s.getOutputStream());
String line=null;
boolean flag=false;
while((line=bufr.readLine())!=null){
if(line.equals(name) ){
flag=true;
break;
}
}
if(flag){
System.out.println(name+",已登录");
out.println(name+",欢迎光临");
break;
}else{
System.out.println(name+",尝试登陆");
out.println(name+",用户名不存在");
}
}
s.close();
}catch(Exception ex){
throw new RuntimeException(ip+"校验失败");
}
}
}
浏览器客户端-自定义服务端
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 演示客户端和服务端
* 1,客户端:浏览器
* 2,服务端,自定义
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(11000);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("客户端你好");
s.close();
ss.close();
}
}
如上图所示客户端
浏览器客户端-Tomcat服务端
在tomcat下的webapps下面新建myweb 里面有一个demo.html文件访问成功如下图
自定义浏览器-Tomcat服务端
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
*模拟浏览器的动作,向tomcat服务请求数据
*浏览器在访问服务器中到底发送了什么数据?才能请求道myweb资源呢?
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(11000);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
InputStream in=s.getInputStream();
byte []buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("客户端你好");
s.close();
ss.close();
}
}
客户端没有变化,服务端打印出了一些数据,这些数据到底是什么呢?
/* 192.168.1.100
GET / HTTP/1.1
Accept: */*
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; .NET4.0C; .NET4.0E; InfoPath.2)
Accept-Encoding: gzip, deflate
Host: 192.168.1.100:11000
Connection: Keep-Alive*/下面还有一行叫做请求数据体
浏览器给服务端发送了这些消息,这些就是Http请求消息头。浏览器和tomcat服务器虽然是不同厂商制作的客户端和服务端,但是他们都遵从了一些国际标准化的协议规则,在底层都走的tcp,在应用层http协议,公共的传输规则。浏览器厂商要想和不同厂商的服务器进行数据交互的话,他们必须得遵守规则。
自定义客户端 服务端 tomcat
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/*
*自定义客户端
*服务端 tomcat
*
*/
public class MyIE {
public static void main(String[] args) throws Exception, IOException {
Socket s=new Socket("192.168.1.100",8080);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println("GET /myweb/demo.html HTTP/1.1");
out.println("Accept: */*");//什么都支持
out.println("Accept-Language: zh-CN");//支持简体中文
out.println("Connection: Keep-Alive");//加上后变慢。
out.println();//一定要写空行,要和请求体分开。
out.println(); //一定要写空行
//服务端要发数据到本浏览器中。
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=bufr.readLine())!=null){
System.out.println(line);
}
s.close();
}
}