黑马程序员------网络编程
2015-10-20 23:07
603 查看
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
b,TCP/IP参考模型
2,端口号,用于标识进程的逻辑地址,不同进程的标识,有效端口:0-65535,其中0-1024系统使用或保留端口
3,传输协议,通讯的规则, 常见协议:TCP,UDP
数据传输示意图
数据通讯原理是数据传输。ping 127.0.0.1(本地回环地址),回执测试 可以测试网卡。Internet协议(TCP/IP)既可以用于广域网也可用于局域网。
代码示例:
import java.net.*;
public class IpDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
InetAddress i= InetAddress.getLocalHost();
System.out.println("address"+i.getHostAddress());
System.out.println(i.toString());
System.out.println
4000
("name:"+i.getHostName());
//获取任意主机IP地址对象 返回值类型,IP地址对象,
InetAddress a=InetAddress.getByName("192.168.1.254"/*"www.baidu.com"*/);
System.out.println("address"+ia.getHostAddress());
System.out.println(i.toString());
System.out.println("name:"+ia.getHostName());
}
}
范例中注意点:static InetAddress getAllByName(String host)在给定主机名的情况下,根据系统上配置的名称服务返回其IP地址所组成的数组。
unknownhostException 未知主机异常
BindException绑定异常。
1) 将数据及源和目的封装成数据包中,不需要建立连接
2) 每个数据报的大小在限制在64k内
3) 因无连接,是不可靠协议
4) 不需要建立连接,速度快
DatagramSocket:此类表示用来发送和接收数据报包的套接字。
Send(DatagramPacket p):从此套接字发送数据报包。
DatagramPacket :此类表示数据报包。数据报包用来实现无连接包投递服务,每条报文仅根据该包中包含的信息从一台机器路由到另一台机器,从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达,补对包投递做出保证。既用来发送数据也用来封装数据。
2) 建立发送端,接收端
3) 建立数据包
4) 调用Socket的发送接收方法
5) 关闭Socket
发送端与接收端是两个独立的运行程序。
UDP的发送端
需求:通过UDP传输方式,将一段文字数据发送出去
思路:
1,建立UdpSocket服务
2,提供数据,并将数据封装到数据包中
3,通过Socket服务的发送功能,将数据包发出去
4,关闭资源
[java]
view plaincopy
public class UdpSend {
public static void main(String[] args) throws Exception
{
//1创建UDP服务,通过DatagramSocket对象
DatagramSocket ds=new DatagramSocket(8888/*可以指定端口*/);
//2.确定数据,并封装成数据包,DataGrampacket(byte[]buf,int length,InetAddress address,int port)
byte[] buf="udp lei le".getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10000);
//3,通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//4,关闭资源。
ds.close();
}
}
定义udp的接收端
需求:定义一个应用程序,用于接收udp协议传输的数据并处理的
思路:
1,建立udpsocket服务,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识,方便于明确哪些数据过来该应用程序可以处理。
2,定义一个数据包,因为要存储接收到的字节数据,因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中
4,通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上
5,关闭资源。
[java]
view plaincopy
class UdpRece {
/**
* 需求:定义一个应用程序,用于接收udp协议传输的数据并处理的
* 定义udp的接收端
* 思路:
* 1,建立udpsocket服务,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识,方便于明确哪些数据过来该应用程序可以处理。
* 2,定义一个数据包,因为要存储接收到的字节数据,因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
* 3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中
* 4,通过数据包对象的特有功能 ,将这些不同的数据取出,打印在控制台上
* 5,关闭资源。
* @param args
*/
public static void main(String[] args) throws Exception
{
//1创建udp socket 建立端点
DatagramSocket ds=new DatagramSocket(8888);
while(true) //2定义数据包,储存数据<span style="margin:0px; padding:0px; border:none; font-family:Consolas,'Courier New',Courier,mono,serif; line-height:18px"> </span>
{
datagrampacket(byte[]buf,int length,InetAddress address,int port)
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
//3,通过socket服务,receiver方法将收到数据存入数据包中
ds.receive(dp);//阻塞式方法。没数据就等。
//4.通过数据包的方法获取其中的数据
String ip=dp.getAddress().getHostAddress();
String data= new String(dp.getData(),0,dp.getLength());
int port=dp.getPort();
System.out.println(ip+";;;"+data+":::"+port);
}
//5,关闭资源。
// ds.close();
}
}
import java.io.*;
import java.net.*;
public class UdpSend2 {
/**192.1.1.0网络段;192.1.1.255网络段中的广播地址。
* @param args
*/
public static void main(String[] args)throws Exception
{
DatagramSocket ds=new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[]buf=line.getBytes();//先变成数组
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.123"),1000);
ds.send(dp);
}
ds.close();
}
}
class UdpRece2 {
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(10001);
while(true)
{ byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":::"+data);
}
}
}
需求:编写一个聊天程序
有收数据的部分,和发数据的部分;这两部分需要同时执行;那就需要用到多线程技术;一个线程控制收,一个线程控制发
因为收和发动作是不一致的,所以要定义两个run方法,而且这两个方法要封装到不同的类中
import java.net.*;
import java.io.*;
public class ChatDemo {
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket();
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try {
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10000);
ds.send(dp);
}
} catch (Exception e) {
throw new RuntimeException("发送端失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try { while(true)
{
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+data);
}
} catch (Exception e) {
throw new RuntimeException("接收失败");
}
}
}
2) 在连接中进行大数据量传输
3) 通过三次握手完成连接,是可靠协议
4) 必须建立连接,效率会稍低。
TCP相当于打电话,UDP相当于步话机,
Socket:网络编程就是Socket。Socket 就是为网络服务提供的一种机制。通信的两端都有Socket。网络通信其实就是Socket间的通信。数据在两个Socket间通过IO传输。
TCP传输
1) Socket和ServerSocket
Socket此类实现客户端套接字,套接字是两台机器间通信的端点
ServerSocket
2) 建立客户端和服务器端
3) 建立连接后,通过Socket中的IO流进行数据的传输
4) 关闭socket
同样,客户端与服务器端是两个独立的应用程序。
演示TCP传输
1,TCP分客户端和服务端
2,客户端对应的对象是Socket.
服务端对应的对象是serverSocket
import java.io.*;
import java.net.*;
public class TcpClient {
/**客户端
* 通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
* 因为TCP是面向连接的,所以在建立socket服务时,就要有服务端存在,并连接成功,形成通路后,在该通道进行数据的传输
*需求:给服务端发一个文本数据
* 步骤
* 1,创建socket服务,并指定要连接的主机和端口。
* 2,为发送数据,应该获取socket流中的输出流
* @param args
*/
public static void main(String[] args)throws Exception
{
//创建客户端的socket服务,指定目的主机和端口。
Socket s=new Socket("192.168.1.123",1009);
//为发送数据,应该获取socket流中的输出流
//InputStream in=s.getInputStream();//返回此套接字的输入流。
OutputStream out=s.getOutputStream();
out.write("tcp ge men lai l ".getBytes());
s.close();
}
}
class TcpServer {
/**
* 服务端
* 通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
*accept():侦听并接受到此套接字的连接。
*需求:定义端点接收数据并打印在控制台上
* 步骤
* 1,建立服务端的socket服务,ServerSocket();
* 并监听一个端口
* 2,获取连接过来的客户端对象 ,
* 通过ServerSocket的accept方法,没有连接就会等,所以这个方法阻塞式的,
* 3,客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该客户端对象的读取流来读取发过来的数据
* 并打印在控制台。
* 4,关闭服务器(可选)
*/
public static void main(String[] args)throws Exception
{
//创建客户端的socket服务,并监听一个端口
ServerSocket ss=new ServerSocket(1009);
//通过accept方法获取连接过来的客户端对象
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"::::connected");
//获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
s.close();//关闭客户端。
ss.close();
}
}
void shutdownOutput();禁用此套接字的输出流。
示例:
import java.io.*;
import java.net.*;
public class TextClient {
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.2.245",10006);
BufferedReader bufr=new BufferedReader(new FileReader("IpDemo.java"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());//操作基本数据类型的流对象。
long time=System.currentTimeMillis();
dos.writeLong(time);
// out.println(time);
String line=null;
while((line=bufr.readLine())!=null)
{
out.println(line);
}
/* out.println("over");//自定义结束。定义标记,第一种方式自定义结束标记,
dos.writeLong(time);*/
s.shutdownOutput();//关闭客户端的输出流,相当于给流中加入一个结束标记-1.禁止此套接字的输出流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TextServer {
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"..............connecsed");
DataInputStream dis=new DataInputStream(s.getInputStream());
long l=dis.readLong();//
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new FileWriter("server.txt"),true);
String line=null;
while((line=bufIn.readLine())!=null)
{
/*if("over".equals(line))
break;*/
out.println(line);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
out.close();
s.close();
ss.close();
}
}
TCP传输最容易出现的问题
客户端连接上服务端,两端都在等待,没有任何数据传输
通过例程分析:
因为read方法或是readline方法是阻塞式
解决方法:自定义结束标记
使用shutdowninput shutdownoutput方法。
需求:简单的一个客户端和一个服务器上传图片
import java.io.*;
import java.net.*;
public class PicClient {
/**客户端
* 1,服务端点
* 2,读取客户端已有的图片数据
* 3,通过socket输出流将数据发给服务端
* 4,读取服务端反馈信息
* 5,关闭
* @param args
*/
public static void main(String[] args) throws Exception
{ if(args.length!=1)
{
System.out.println("请选择一个jpg格式的图片");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
System.out.println("该文件有问题,要么不存在,要么不是文件");
return;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
return ;
}
if(file.length()>1024*1024*8)
{
System.out.println("文件过大。没安好心");
return;
}
Socket s=new Socket("192.168.1.2",1007);
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 {
/**服务器
* 这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程
* 这时B客户端连接,只有等待
* 因为服务端还没有处理完A客户端的请求,还有循环回来执行下次accept方法,所以
* 暂时获取不到B客户端对象
* 那么为了可以让多个客户端同时并发访问服务端
* 那么服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。
* @param args
* 如何定义线程?
* 只要明确了每一个客户端要在服务端执行的代码即可,将该代码存入run方法中。
*/
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(1007);
Socket s=ss.accept();//accept()是阻塞式的。
InputStream in=s.getInputStream();
FileOutputStream fos=new FileOutputStream("server.bmp");
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();
}
}
演示客户端和服务端
1,客户端:浏览器(telnet)
服务器:自定义
2,客户端:浏览器
服务端:tomcat
3,客户端自定义
服务端: tomcat服务器。
import java.net.*;
import java.io.*;
public class ServerDemo {
/**演示客户端和服务端
*1,客户端:浏览器(telnet)
* 服务器 自定义
*2,客户端:浏览器
* 服务端:tomcat
*3,客户端 自定义
* 服务端 tomcat服务器。
* @param args
*/
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("<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();
}
}
import java.io.*;
public class URLConnectionDemo {
/**URLConnection 抽象类是所有类的超类,它代表应用程序和URL之间的通信链接,此类的实例可用于读取和写入此URL引用的资源,通常,创建一个到URL的 连接需要几个步骤。
* 1,通过在URL,上调用openConnection()方法创建连接对象,
* 2,处理设置参数和一般请求属性
* 3,使用connect方法建立到远程对象的实际连接,
* 4,远程对象变为可用,远程对象的头字段和内容变为可访问。
* URLConnection openConnection()返回一个URLConnection对象,它表示到URL所引用的远程对象的连接。
* InputStream getInputStream()返回从此打开的连接读取的输入流。
* InputStream openStream()打开到此URL的连接并返回一个用于从该连接读入的InputStream
* @param args
*/
public static void main(String[] args) throws Exception
{
URL url=new URL("http://192.68.1.123:8080/myweb/demo.html");
URLConnection conn=url.openConnection();
System.out.println(conn);
InputStream in=conn.getInputStream();
byte[]buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
}
}
1,网络模型
a,OSL参考模型b,TCP/IP参考模型
2,网络通讯要素:
1,Ip地址:网络中设备的标识,不易记忆,可用主机名, 本地回环地址:127.0.0.1主机名localhost----- ping localhost ping 127.0.0.12,端口号,用于标识进程的逻辑地址,不同进程的标识,有效端口:0-65535,其中0-1024系统使用或保留端口
3,传输协议,通讯的规则, 常见协议:TCP,UDP
数据传输示意图
数据通讯原理是数据传输。ping 127.0.0.1(本地回环地址),回执测试 可以测试网卡。Internet协议(TCP/IP)既可以用于广域网也可用于局域网。
代码示例:
import java.net.*;
public class IpDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
InetAddress i= InetAddress.getLocalHost();
System.out.println("address"+i.getHostAddress());
System.out.println(i.toString());
System.out.println
4000
("name:"+i.getHostName());
//获取任意主机IP地址对象 返回值类型,IP地址对象,
InetAddress a=InetAddress.getByName("192.168.1.254"/*"www.baidu.com"*/);
System.out.println("address"+ia.getHostAddress());
System.out.println(i.toString());
System.out.println("name:"+ia.getHostName());
}
}
范例中注意点:static InetAddress getAllByName(String host)在给定主机名的情况下,根据系统上配置的名称服务返回其IP地址所组成的数组。
unknownhostException 未知主机异常
BindException绑定异常。
3,TCP和UDP
UDP:面向无连接。发送数据前不需要建立连接。UDP把数据打成数据包,数据会被封包。对方的端口和地址要明确出来。面向无连接不需要连接,它发送的包是有限制的,分成多包发送。不可靠,数据丢失。速度快。不用确定对方在不在,省略确认过程。这种传输方式在我们生活当中如聊天,视频会议,桌面共享,下载是TCP1) 将数据及源和目的封装成数据包中,不需要建立连接
2) 每个数据报的大小在限制在64k内
3) 因无连接,是不可靠协议
4) 不需要建立连接,速度快
UDP传输
1) DatagramSocket与DatagramPacketDatagramSocket:此类表示用来发送和接收数据报包的套接字。
Send(DatagramPacket p):从此套接字发送数据报包。
DatagramPacket :此类表示数据报包。数据报包用来实现无连接包投递服务,每条报文仅根据该包中包含的信息从一台机器路由到另一台机器,从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达,补对包投递做出保证。既用来发送数据也用来封装数据。
2) 建立发送端,接收端
3) 建立数据包
4) 调用Socket的发送接收方法
5) 关闭Socket
发送端与接收端是两个独立的运行程序。
UDP的发送端
需求:通过UDP传输方式,将一段文字数据发送出去
思路:
1,建立UdpSocket服务
2,提供数据,并将数据封装到数据包中
3,通过Socket服务的发送功能,将数据包发出去
4,关闭资源
[java]
view plaincopy
public class UdpSend {
public static void main(String[] args) throws Exception
{
//1创建UDP服务,通过DatagramSocket对象
DatagramSocket ds=new DatagramSocket(8888/*可以指定端口*/);
//2.确定数据,并封装成数据包,DataGrampacket(byte[]buf,int length,InetAddress address,int port)
byte[] buf="udp lei le".getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10000);
//3,通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//4,关闭资源。
ds.close();
}
}
定义udp的接收端
需求:定义一个应用程序,用于接收udp协议传输的数据并处理的
思路:
1,建立udpsocket服务,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识,方便于明确哪些数据过来该应用程序可以处理。
2,定义一个数据包,因为要存储接收到的字节数据,因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中
4,通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上
5,关闭资源。
[java]
view plaincopy
class UdpRece {
/**
* 需求:定义一个应用程序,用于接收udp协议传输的数据并处理的
* 定义udp的接收端
* 思路:
* 1,建立udpsocket服务,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识,方便于明确哪些数据过来该应用程序可以处理。
* 2,定义一个数据包,因为要存储接收到的字节数据,因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
* 3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中
* 4,通过数据包对象的特有功能 ,将这些不同的数据取出,打印在控制台上
* 5,关闭资源。
* @param args
*/
public static void main(String[] args) throws Exception
{
//1创建udp socket 建立端点
DatagramSocket ds=new DatagramSocket(8888);
while(true) //2定义数据包,储存数据<span style="margin:0px; padding:0px; border:none; font-family:Consolas,'Courier New',Courier,mono,serif; line-height:18px"> </span>
{
datagrampacket(byte[]buf,int length,InetAddress address,int port)
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
//3,通过socket服务,receiver方法将收到数据存入数据包中
ds.receive(dp);//阻塞式方法。没数据就等。
//4.通过数据包的方法获取其中的数据
String ip=dp.getAddress().getHostAddress();
String data= new String(dp.getData(),0,dp.getLength());
int port=dp.getPort();
System.out.println(ip+";;;"+data+":::"+port);
}
//5,关闭资源。
// ds.close();
}
}
import java.io.*;
import java.net.*;
public class UdpSend2 {
/**192.1.1.0网络段;192.1.1.255网络段中的广播地址。
* @param args
*/
public static void main(String[] args)throws Exception
{
DatagramSocket ds=new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[]buf=line.getBytes();//先变成数组
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.123"),1000);
ds.send(dp);
}
ds.close();
}
}
class UdpRece2 {
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(10001);
while(true)
{ byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":::"+data);
}
}
}
需求:编写一个聊天程序
有收数据的部分,和发数据的部分;这两部分需要同时执行;那就需要用到多线程技术;一个线程控制收,一个线程控制发
因为收和发动作是不一致的,所以要定义两个run方法,而且这两个方法要封装到不同的类中
import java.net.*;
import java.io.*;
public class ChatDemo {
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket();
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try {
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10000);
ds.send(dp);
}
} catch (Exception e) {
throw new RuntimeException("发送端失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try { while(true)
{
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+data);
}
} catch (Exception e) {
throw new RuntimeException("接收失败");
}
}
}
TCP:面向连接,对方必须在,
1) 建立连接,形成传输数据的通道2) 在连接中进行大数据量传输
3) 通过三次握手完成连接,是可靠协议
4) 必须建立连接,效率会稍低。
TCP相当于打电话,UDP相当于步话机,
Socket:网络编程就是Socket。Socket 就是为网络服务提供的一种机制。通信的两端都有Socket。网络通信其实就是Socket间的通信。数据在两个Socket间通过IO传输。
TCP传输
1) Socket和ServerSocket
Socket此类实现客户端套接字,套接字是两台机器间通信的端点
ServerSocket
2) 建立客户端和服务器端
3) 建立连接后,通过Socket中的IO流进行数据的传输
4) 关闭socket
同样,客户端与服务器端是两个独立的应用程序。
演示TCP传输
1,TCP分客户端和服务端
2,客户端对应的对象是Socket.
服务端对应的对象是serverSocket
import java.io.*;
import java.net.*;
public class TcpClient {
/**客户端
* 通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
* 因为TCP是面向连接的,所以在建立socket服务时,就要有服务端存在,并连接成功,形成通路后,在该通道进行数据的传输
*需求:给服务端发一个文本数据
* 步骤
* 1,创建socket服务,并指定要连接的主机和端口。
* 2,为发送数据,应该获取socket流中的输出流
* @param args
*/
public static void main(String[] args)throws Exception
{
//创建客户端的socket服务,指定目的主机和端口。
Socket s=new Socket("192.168.1.123",1009);
//为发送数据,应该获取socket流中的输出流
//InputStream in=s.getInputStream();//返回此套接字的输入流。
OutputStream out=s.getOutputStream();
out.write("tcp ge men lai l ".getBytes());
s.close();
}
}
class TcpServer {
/**
* 服务端
* 通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机
*accept():侦听并接受到此套接字的连接。
*需求:定义端点接收数据并打印在控制台上
* 步骤
* 1,建立服务端的socket服务,ServerSocket();
* 并监听一个端口
* 2,获取连接过来的客户端对象 ,
* 通过ServerSocket的accept方法,没有连接就会等,所以这个方法阻塞式的,
* 3,客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该客户端对象的读取流来读取发过来的数据
* 并打印在控制台。
* 4,关闭服务器(可选)
*/
public static void main(String[] args)throws Exception
{
//创建客户端的socket服务,并监听一个端口
ServerSocket ss=new ServerSocket(1009);
//通过accept方法获取连接过来的客户端对象
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"::::connected");
//获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
s.close();//关闭客户端。
ss.close();
}
}
文件的复制。
void shutdownInput() ;此套接字的输入流置于"流的末尾"void shutdownOutput();禁用此套接字的输出流。
示例:
import java.io.*;
import java.net.*;
public class TextClient {
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.2.245",10006);
BufferedReader bufr=new BufferedReader(new FileReader("IpDemo.java"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());//操作基本数据类型的流对象。
long time=System.currentTimeMillis();
dos.writeLong(time);
// out.println(time);
String line=null;
while((line=bufr.readLine())!=null)
{
out.println(line);
}
/* out.println("over");//自定义结束。定义标记,第一种方式自定义结束标记,
dos.writeLong(time);*/
s.shutdownOutput();//关闭客户端的输出流,相当于给流中加入一个结束标记-1.禁止此套接字的输出流
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TextServer {
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"..............connecsed");
DataInputStream dis=new DataInputStream(s.getInputStream());
long l=dis.readLong();//
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new FileWriter("server.txt"),true);
String line=null;
while((line=bufIn.readLine())!=null)
{
/*if("over".equals(line))
break;*/
out.println(line);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
out.close();
s.close();
ss.close();
}
}
TCP传输最容易出现的问题
客户端连接上服务端,两端都在等待,没有任何数据传输
通过例程分析:
因为read方法或是readline方法是阻塞式
解决方法:自定义结束标记
使用shutdowninput shutdownoutput方法。
需求:简单的一个客户端和一个服务器上传图片
import java.io.*;
import java.net.*;
public class PicClient {
/**客户端
* 1,服务端点
* 2,读取客户端已有的图片数据
* 3,通过socket输出流将数据发给服务端
* 4,读取服务端反馈信息
* 5,关闭
* @param args
*/
public static void main(String[] args) throws Exception
{ if(args.length!=1)
{
System.out.println("请选择一个jpg格式的图片");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
System.out.println("该文件有问题,要么不存在,要么不是文件");
return;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
return ;
}
if(file.length()>1024*1024*8)
{
System.out.println("文件过大。没安好心");
return;
}
Socket s=new Socket("192.168.1.2",1007);
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 {
/**服务器
* 这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程
* 这时B客户端连接,只有等待
* 因为服务端还没有处理完A客户端的请求,还有循环回来执行下次accept方法,所以
* 暂时获取不到B客户端对象
* 那么为了可以让多个客户端同时并发访问服务端
* 那么服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。
* @param args
* 如何定义线程?
* 只要明确了每一个客户端要在服务端执行的代码即可,将该代码存入run方法中。
*/
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(1007);
Socket s=ss.accept();//accept()是阻塞式的。
InputStream in=s.getInputStream();
FileOutputStream fos=new FileOutputStream("server.bmp");
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();
}
}
演示客户端和服务端
1,客户端:浏览器(telnet)
服务器:自定义
2,客户端:浏览器
服务端:tomcat
3,客户端自定义
服务端: tomcat服务器。
import java.net.*;
import java.io.*;
public class ServerDemo {
/**演示客户端和服务端
*1,客户端:浏览器(telnet)
* 服务器 自定义
*2,客户端:浏览器
* 服务端:tomcat
*3,客户端 自定义
* 服务端 tomcat服务器。
* @param args
*/
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("<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();
}
}
与URL链接
import java.net.*;import java.io.*;
public class URLConnectionDemo {
/**URLConnection 抽象类是所有类的超类,它代表应用程序和URL之间的通信链接,此类的实例可用于读取和写入此URL引用的资源,通常,创建一个到URL的 连接需要几个步骤。
* 1,通过在URL,上调用openConnection()方法创建连接对象,
* 2,处理设置参数和一般请求属性
* 3,使用connect方法建立到远程对象的实际连接,
* 4,远程对象变为可用,远程对象的头字段和内容变为可访问。
* URLConnection openConnection()返回一个URLConnection对象,它表示到URL所引用的远程对象的连接。
* InputStream getInputStream()返回从此打开的连接读取的输入流。
* InputStream openStream()打开到此URL的连接并返回一个用于从该连接读入的InputStream
* @param args
*/
public static void main(String[] args) throws Exception
{
URL url=new URL("http://192.68.1.123:8080/myweb/demo.html");
URLConnection conn=url.openConnection();
System.out.println(conn);
InputStream in=conn.getInputStream();
byte[]buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
}
}
相关文章推荐
- 网络编程(0)--multicast
- iOS开发5-NSURLSession实现Http通信
- ubuntu配置网络安装
- ubuntu14.0.4网络图标不见了
- HDU 5452 Minimum Cut (2015年沈阳赛区网络赛C题)
- TCP流量控制和拥塞控制
- 转:OSI七层协议与TCP/IP四层协议之间的比较(学习中)
- C++网络通信库性能大比拼
- Paw HTTP Client for mac
- Linux集群系列(负一):VMWare下CentOS7网络设置
- 神经网络(1)--Non-linear hypotheses,为什么我们要学习神经网络这种算法来处理non-linear hypotheses
- Linux监控网络流量和限制网卡速率
- 在浏览器里输入网址到显示完整网页
- Windows系统下C++socket TCP编程步骤
- 动手学习TCP:数据传输(转)
- 第一款采用人工神经网络的智能输入法
- Linux下的网络编程模型总结
- 黑马程序员--Java学习日记之GUI&网络编程
- HTTPS背后的加密算法(转)
- iOS开发4-NSURLConnection实现Http通信