计算机网络:
网络编程的目的
想要达到这个效果需要什么
如何实现网络的通信?
通信双方的地址:
规则:网络通信协议
ip地址:关键字InetAddress
唯一定位一台网络上的计算机
127.0.0.1:本机localhost
ip地址的分类
ipv4 127.0.0.1,4个字节组成,0~255,42亿;已用尽
ipv6:128位。8个无符号的证书
2001:0bb2:aaaa:0015:0000:0000:1aaa:1312
公网(互联网)-私网(局域网)
域名:用来记忆IP(www.vip.com)
端口便是计算机上一个程序的进程:
不同的进程有不同的端口号,用来区分不同的软件
被规定的端口号TCP,UDP数量为0~65535 *2
单个协议下,端口号不能冲突
端口分类
公有端口0~1023
程序注册端口:1024~49151,分配用户或者程序使用
动态、私有:49152~65535
TCP:打电话
UDP:发信息
简单的服务器客户端模拟
//服务器
package Net;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpSeriverDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
serverSocket=new ServerSocket(9999);//创建一个地址
socket=serverSocket.accept();//等待客户端连接
is = socket.getInputStream();//读取客户端信息
baos=new ByteArrayOutputStream();//管道流
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//客户端
package Net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo01 {
//要知道服务器 地址
public static void main(String[] args) {
Socket socket = null;
OutputStream os=null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");//得到服务器的地址和端口号
int port = 9999;
socket=new Socket(serverIP,port);//创建一个socket连接
os=socket.getOutputStream();//发送信息IO流
os.write("北京欢迎你".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
package Net.Demo02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket=new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is =socket.getInputStream();
FileOutputStream fos =new FileOutputStream(new File("receive.jpg"));
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
OutputStream os=socket.getOutputStream();
os.write("接收完毕".getBytes());
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
package Net.Demo02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
InetAddress address= InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(address,9000);
OutputStream os= socket.getOutputStream();
FileInputStream fis=new FileInputStream(new File("yanzi.jpg"));
byte[] buffer=new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is =socket.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer2=new byte[2014];
int lens;
while((len=is.read(buffer2))!=-1){
baos.write(buffer2,0,len);
}
System.out.println(baos.toString());
fis.close();
os.close();
socket.close();
}
}
服务端
客户端
不需要连接服务器:关键字:DatagramSocket
服务器
package Net.Demo03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket(9090);
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
客户端
package Net.Demo03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket();
String msg="你好,服务器!";
InetAddress localhost=InetAddress.getByName("127.0.0.1");
int port=9090;
DatagramPacket packet=new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
socket.send(packet);
socket.close();
}
}
初步聊天实现
package Net.Demo04;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class UdpsenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket();
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
while (true){
String data=reader.readLine();
byte[] datas= data.getBytes();
DatagramPacket packet =new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
package Net.Demo04;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket(6666);
while (true){
byte[] container=new byte[1024];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data=packet.getData();
String receiveData=new String(data,0,data.length);
System.out.println(receiveData);
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
发送端
package Net.Demo05;
import org.jcp.xml.dsig.internal.dom.ApacheOctetStreamData;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable{
DatagramSocket socket=null;
BufferedReader reader=null;
private int fromPort;
private String toIP;
private int toport;
public TalkSend(int fromPort, String toIP, int toport) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toport = toport;
try {
socket=new DatagramSocket(fromPort);
reader=new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data=reader.readLine();
byte[] datas =data.getBytes();
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toport));
socket.send(packet);
if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
接收端
package Net.Demo05;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket=null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom=msgFrom;
try {
socket=new DatagramSocket(port);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] container=new byte[1024];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data=packet.getData();
String receiveData=new String(data,0,data.length);
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
A
package Net.Demo05;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
B
package Net.Demo05;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
package Net.PA;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws Exception {
URL url=new URL("https://ws.stream.qqmusic.qq.com/C400002zpsxr1TlBvY.m4a?guid=7983659658&vkey=5EF7C88327DF6EDC07FB84C564CB1884F2D0B9AE7AACE0A72C8B387FB122CF1B1586FA8B6DE434A00A74B55AAE314FB2806E0C4C6B4F7DC9&uin=7739&fromtag=66");
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos =new FileOutputStream("f.m4a");
byte[] buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}