关键词:
网络编程
1、什么是计算机网络
什么是计算及网络?
计算机网络系统就是利用通信设备和线路将地理位置不同、功能独立的多个计算机系统互联起来,以功能完善的网络软件实现网络中资源共享和信息传递的系统。
网络编程的目的
数据交换----通信
2、网络通信的两个要素
小结:
1.网络编程中有两个主要的问题
- 如何准确的定位到网络.上的一台或者多台主机
- 找到主机之后如何进行通信
2.网络编程中的要素
- IP和端口号IP.
- 网络通信协议udp, tcp
3.万物皆对象
3、IP地址
java.net.InetAddress
package com.yangbocsu.lesson1;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class TestInetAddress
public static void main(String[] args)
try
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println("inetAddress1:"+inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println("inetAddress2:"+inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println("inetAddress3:"+inetAddress3);
//查询网站Ip地址
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println("inetAddress1:"+inetAddress4);
//常用方法
System.out.println(inetAddress4.getAddress());
System.out.println(inetAddress4.getHostAddress());
System.out.println(inetAddress4.getCanonicalHostName());
System.out.println(inetAddress4.getHostName());
catch (UnknownHostException e)
e.printStackTrace();
4、端口Port
package com.yangbocsu.lesson1;
import java.net.InetSocketAddress;
public class TestInetSocketAddress
public static void main(String[] args)
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
System.out.println(socketAddress);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
5、通信协议
协议:约定,就好比我们现在说的普通话。
网络通信协议: 速率、传输码率、代码结构、传输控制。。。。
**问题:**非常复杂?
大事化小:分层!
TCP/IP协议簇
重要:
- TCP(Transmission Control Protocol,传输控制协议)
- UDP(User Data Protocol,用户数据报协议)
出名的协议:
- TCP:
- IP : 网络互连协议
6、TCP实现聊天
服务端 - 客户端
服务端
package com.yangbocsu.lesson2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo01
public static void main(String[] args)
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is =null;
ByteArrayOutputStream baos = null;
try
//1. 我得要有一个地址 localhost:9999
serverSocket = new ServerSocket(9999);
//2.等待客户端连接过来
socket = serverSocket.accept();
//3.读取客户端的消息
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 com.yangbocsu.lesson2;
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
//1. 要知道服务器的地址 端口号
InetAddress ServerIP= InetAddress.getByName("127.0.0.1");
int port= 9999;
//2.创建一个socket 连接
socket = new Socket(ServerIP, port);
//3.发送消息 IO流
os = socket.getOutputStream();
os.write("hello world!".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 com.yangbocsu.lesson2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo01
public static void main(String[] args)
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is =null;
ByteArrayOutputStream baos = null;
try
//1. 我得要有一个地址 localhost:9999
serverSocket = new ServerSocket(9999);
while (true) //服务端一直监听
//2.等待客户端连接过来
socket = serverSocket.accept();
//3.读取客户端的消息
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();
7、TCP文件上传实现
服务端
package com.yangbocsu.lesson2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo02
public static void main(String[] args) throws Exception
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的链接
Socket socket = serverSocket.accept();//阻塞试 监听,会一直监听到客户端连接
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive1.png"));
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer))!=-1)
fos.write(buffer,0,len);
//通知客户端我接收完毕了
OutputStream os = socket.getOutputStream();
os.write("Over! Over!".getBytes());
//5.关闭资源
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
客户端
package com.yangbocsu.lesson2;
import java.io.*;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.Socket;
import static java.net.InetAddress.*;
public class TcpClientDemo02
public static void main(String[] args) throws Exception
//1.创建一个socket 连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("img.png"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) !=-1)
os.write(buffer,0,len);
//通知服务器,我已经发送结束了
socket.shutdownOutput();
//确定服务器接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bytejava网络信息(代码片段)
java阳光网络2(代码片段)
java面向网络编程(代码片段)
这是服务器端的代码importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.Window 查看详情
java以编程方式设置图像(代码片段)
java打电话给网络(代码片段)
网络编程-第二节(代码片段)
1.1 基础题:客户端与服务器持续通信1.2 应用题:设计带窗口客户端导入程序MyServer.java、MyClient.java、ClientThread.java。补充程序MyClient.java的代码(在“//TODO添加代码”提示的位置),使得1) &n... 查看详情
转:java网络编程-http编程(代码片段)
转自:java网络编程-HTTP编程_Stillsings的博客-CSDN博客HTTP编程JavaHTTP编程支持模拟成浏览器的方式去访问网页URL,UniformResourceLocator,代表一个资源URLConnection获取资源连接器根据URL的openConnection()方法获得URLConnectioncon... 查看详情
字符串编程(代码片段)
java字符串编程题Java代码import java.util.HashMap; import java.util.Map; public class CountCharNum /** * JAVA编程题:字符串"yekmaakkccekymbvb",求出字符串中有多少种字符,以及每个字符的个数? * */ public static void main(St 查看详情
java跟网络相关的工具类(代码片段)
java以编程方式创建线性布局(代码片段)
java以编程方式创建图像视图(代码片段)
java跟网络相关的工具类(代码片段)
java跟网络相关的工具类(代码片段)
java以编程方式打开设置页面(代码片段)
java面向对象编程(代码片段)
Java面向对象编程1.构造方法publicclassPersonprivateStringname;privateintage;//构造方法1publicPerson(Stringname,intage)this.name=name;this.age=age;//构造方法2publicPerson(Stringname)this.name=name;publicStringgetNam 查看详情
java小白初识网络编程(代码片段)
一、网络基础二、网络协议 实现TCP的网络编程例子1:客户端发送信息给服务端,服务端将数据显示在控制台上publicclassTCPTest1//客户端@Testpublicvoidclient()Socketsocket=null;OutputStreamos=null;try//1.创建Socket对象,指明... 查看详情
java---网络编程(代码片段)
文章目录网络编程1.网络编程入门1.1软件结构1.2网络通信协议1.3协议分类1.4网络编程三要素2.TCP通信程序2.1概述2.2Socket类2.3ServerSocket类2.4简单的TCP网络程序3.综合案例3.1文件上传案例网络编程1.网络编程入门1.1软件结构C/S结构:... 查看详情
java示例代码_从Java中以编程方式动态加载OSGI片段
java示例代码_从Java中以编程方式动态加载OSGI片段 查看详情