一、前言 网络通讯要素
InetAddress
public class InetAddress extends Object implements Serializable
此类表示互联网协议 (IP) 地址。
IP 地址是 IP 使用的 32 位或 128 位无符号数字,它是一种低级协议,UDP 和 TCP 协议都是在它的基础上构建的。
InetAddress 类提供将主机名解析为其 IP 地址(或反之)的方法。
获取本机计算机名称和IP地址
InetAddress ia = InetAddress.getLocalHost();
sop(ia.toString());
获取任意主机名称和IP地址
InetAddress i = InetAddress.getByName("Datura");
sop(i.getHostAddress());
//sop(i.getHostName());
类似于ping可以直接输入域名,InetAddress也可以这样写:
InetAddress i = InetAddress.getByName("www.baidu.com");
反之,如果知道IP地址,也可通过该语句获取主机名称:
InetAddress i = InetAddress.getByName("192.168.0.102");
sop(i.getHostName());
二.TCP & UDP
(一)Socket
extends Object
此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
套接字的实际工作由 SocketImpl 类的实例执行。应用程序通过更改创建套接字实现的套接字工厂可以配置它自身,以创建适合本地防火墙的套接字。
(二)UDP
public class DatagramSocket
extends Object
此类表示用来发送和接收数据报包的套接字。
数据报套接字是包投递服务的发送或接收点。每个在数据报套接字上发送或接收的包都是单独编址和路由的。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达。
在 DatagramSocket 上总是启用 UDP 广播发送。为了接收广播包,应该将 DatagramSocket 绑定到通配符地址。在某些实现中,将 DatagramSocket 绑定到一个更加具体的地址时广播包也可以被接收。
实例1:一个简单的UDP发送端程序
import java.net.*;
class UdpDemo
{
public static void main(String[] args) throws Exception
{
//1.创建UDP服务
DatagramSocket ds = new DatagramSocket();
//2.确定发送数据
byte[] buf = "Hello UDP!".getBytes();
//3.封装成包
DatagramPacket dp =
new DatagramPacket (buf,buf.length,InetAddress.getByName("Datura"),10000);
//4.通过send发送
ds.send(dp);
//关闭资源
ds.close();
}
}
实例2:一个简单的接收端程序
import java.net.*;
class UpdReceiveDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(10000);
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());
int port = dp.getPort();
System.out.println("["+ip+":"+port+"]"+data);
}
}
实例3 聊天程序
import java.io.*;
import java.net.*;
class ChatDemo
{
public static void main(String[] args) throws Exception
{
int sendport=10003;
int receport=10007;
new Thread(new Send(new DatagramSocket(sendport))).start();
new Thread(new Receive(new DatagramSocket(receport))).start();
}
}
class Send implements Runnable
{
private DatagramSocket ds;
Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
//DatagramSocket ds = new DatagramSocket(12345);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line=bufr.readLine())!=null)
{
if ("exit".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("Datura"),10003);
ds.send(dp);
}
}
catch (Exception e)
{
System.out.println("发送失败");
}
}
}
class Receive implements Runnable
{
private DatagramSocket ds;
Receive(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
//DatagramSocket ds = new DatagramSocket(10000);
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());
int port = dp.getPort();
System.out.println("["+ip+":"+port+"]"+data);
}
}
catch (Exception e)
{
System.out.println("接受失败");
}
}
}
(三)TCP
实例1:客户端
/*
客户端对应的对象是Socket
服务端对应的对象是ServerSocket
*/
import java.net.*;
import java.io.*;
class ClientDemo
{
public static void main(String[] args) throws Exception
{
//创建客户端的Socket服务,指定目的主机和端口
Socket s = new Socket("192.168.0.102",10006);
//为了发送数据,应该获取Socket流中的输出流
OutputStream out = s.getOutputStream();
out.write("hello TCP".getBytes());
s.close();
}
}
实例2:服务端
import java.net.*;
import java.io.*;
class TCPServer
{
public static void main(String[] args) throws Exception
{
//建立服务端Socket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10006);
//通过accept方法获取连接过来的客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......is 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();
}
}
实例3:建立一个文本转换服务器和可以访问它的客户端,服务器将客户端提交的文本转换为大写再反馈给客户端
import java.net.*;
import java.io.*;
class TransClient
{
public static void main(String[] args) throws Exception
{
//建立服务
Socket s = new Socket("Datura",10002);
//键盘读取流,读取键盘录入的信息
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//服务输出流,将读取到的数据输出到服务端
BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//服务读取流,将经过服务端处理的数据取回到本地
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufr.readLine())!=null)
{
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
if ("over".equals(line))
{
break;
}
String str = bufIn.readLine();
System.out.println("Server:"+str);
}
s.close();
}
}
import java.net.*;
import java.io.*;
class TransServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufo = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while ((line = bufr.readLine())!=null)
{
System.out.println(line);
bufo.write(line.toUpperCase());
bufo.newLine();
bufo.flush();
}
s.close();
ss.close();
}
}
需求:上传一个文本文件
import java.io.*;
import java.net.*;
class TextClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10006);
BufferedReader bufr =
new BufferedReader(new FileReader("IPDemo.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null)
{
out.println(line);
}
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+"....connected");
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();
}
}
需求:客户并发上传图片。
/*
客户端。
1,服务端点。
2,读取客户端已有的图片数据。
3,通过socket 输出流将数据发给服务端。
4,读取服务端反馈信息。
5,关闭。
*/
import java.io.*;
import java.net.*;
class PicClient
{
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*5)
{
System.out.println("文件过大,没安好心");
return ;
}
Socket s = new Socket("192.168.1.254",10007);
FileInputStream fis = new FileInputStream(file);
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();
}
}
/*
服务端
这个服务端有个局限性。当A客户端连接上以后。被服务端获取到。服务端执行具体流程。
这时B客户端连接,只有等待。
因为服务端还没有处理完A客户端的请求,还有循环回来执行下次accept方法。所以
暂时获取不到B客户端对象。
那么为了可以让多个客户端同时并发访问服务端。
那么服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。
如何定义线程呢?
只要明确了每一个客户端要在服务端执行的代码即可。将该代码存入run方法中。
*/
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println(ip+"....connected");
InputStream in = s.getInputStream();
File dir = new File("d:\\pic");
File file = new File(dir,ip+"("+(count)+")"+".jpg");
while(file.exists())
file = new File(dir,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+"上传失败");
}
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10007);
while(true)
{
Socket s = ss.accept();
new Thread(new PicThread(s)).start();
}
//ss.close();
}
}
需求:客户并发登录
/*
客户端通过键盘录入用户名。
服务端对这个用户名进行校验。
如果该用户存在,在服务端显示xxx,已登陆。
并在客户端显示 xxx,欢迎光临。
如果该用户存在,在服务端显示xxx,尝试登陆。
并在客户端显示 xxx,该用户不存在。
最多就登录三次。
*/
import java.io.*;
import java.net.*;
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10008);
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();
}
}
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s = s;
}
public void run()
{
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)
break;
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
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 e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}