Socket简介
Socket的底层机制复杂,Java平台提供了一些简单的API,可以更简单有效的使用Socket开发而无需了解底层机制
通信链路的端点就被称为“套接字”(英文名Socket)
是提供给应用程序的接口
什么是Socket
简单来说就是IP地址与端口的结合协议 一种地址与端口的结合描述协议
TCP/IP协议的相关API的总称;是网络Api的集合实现
涵盖了:Stream Socket/ Datagram Socket
Socket的作用与组成
在网络传输中用于唯一标识两个端点之间的连接
端点:包括(IP+Port)
4个要素:客户端地址、客户端端口、服务器地址、服务器端口
Socket之TCP
TCP是面向连接的通信协议
通过三次握手建立连接,通讯完成时要拆除连接
由于TCP是面向连接的所以只能用于端到端的通讯
Socket之UDP
UDP是面向无连接的通讯协议
UDP数据包括目的端口号和源端口号信息
由于通讯不需要连接,所以可以实现广播发送,并不局限于端到端
java.net包
Socket
ServerSocket
DatagramPacket
DatagramSocket
InetAddress
…………
基于TCP协议的Socket编程
Socket网络编程一般可以分成如下步骤进行
建立连接
打开Socket关联的输入输出流
数据流中读写信息
关闭所有的数据流和Socket
Socket中实现对象的传递
序列化传递对象信息 :ObjectOutputStream oos = new ObjectOutputStream(…);
oos.writeObject(…);
ObjectInputStream ois = new ObjectInputStream(…);
Object = ois.readObject();
多线程处理多请求
采用多线程的方式
一个专门负责监听的应用主服务程序
一个专门负责处理请求的线程程序
public static void main(String[] args) {
// 创建通信链路的端点客户端套接字
Socket socket =null;
OutputStream os = null;
InputStream is =null;
BufferedReader br =null;
try {
socket = new Socket("localhost",10086);
//获取输出流将数据发送出去
os= socket.getOutputStream();
String str = "用户名:芝士雪豹,密码:123456";
byte[] bytes=str.getBytes();
//通过输出流调用方法将数据发送出去
os.write(bytes);
System.out.println("数据发送完毕");
//关闭通道
socket.shutdownOutput();
is =socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String result =br.readLine();
System.out.println("我是客户端,接收到的服务器端响应信息为:"+result);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
is.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 创建服务器端套接字ServerSocket
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
BufferedReader br = null;
OutputStream os = null;
try {
ss = new ServerSocket(10086);
//服务器通过调用侦听方法来获取客户端请求
socket = ss.accept();
//通过返回的Socket对象调用方法获取一个输入流来读取客户端发送过来的信息
is = socket.getInputStream();
//通过输入流读取客户端发送的消息
br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
System.out.println("我这边是服务器,我接受到的消息是"+str);
os= socket.getOutputStream();
String result = "密码正确,我测你们码";
byte[] bytes=result.getBytes();
//通过输出流调用方法将数据发送出去
os.write(bytes);
System.out.println("给客户端的响应信息发送成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
os.close();
br.close();
is.close();
socket.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Student implements Serializable{
/** 用户名 */
private String loginName;
/** 用户密码 */
private String age;
public Student() {
super();
}
public Student(String loginName, String age) {
super();
this.loginName = loginName;
this.age = age;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
基于UDP协议的Socket编程
基于UDP协议的Socket网络编程步骤
利用 DatagramPacket 对象封装数据包
利用 DatagramSocket 发送数据包
利用 DatagramSocket 接收数据包
利用 DatagramPacket 处理数据包
public class Receive {
public static void main(String[] args) {
DatagramSocket ds = null;
//创建DatagramPacket对象,用来准备接收数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
try {
ds = new DatagramSocket(8800);
ds.receive(dp);
//显示接收到的信息
String mess = new String(dp.getData(), 0, dp.getLength());
System.out.println(dp.getAddress().getHostAddress()+"说:"+mess);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ds.close();
}
}
}
public class Send {
public static void main(String[] args) {
String mess = "您好,我想咨询一个问题";
//创建inetAddress,获取本地主机地址
InetAddress ia = null;
DatagramSocket ds = null;
try {
ia = InetAddress.getByName("localhost");
//创建DataGramPacket对象,封装数据
DatagramPacket dp = new DatagramPacket(mess.getBytes(), mess.getBytes().length, ia,8800);
//创建DatagramSocket对象,向服务器发送数据
ds = new DatagramSocket();
ds.send(dp);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ds.close();
}
}
}