1、Java支持基于流的通信和基于包的通信
基于流的通信使用TCP协议(传输控制协议)进行数据传输,传输是无损可靠的
基于包的通信使用UDP协议(用户数据报协议)进行数据传输,不能保证传输没有丢失
2、服务端和客户端套接字--基于流的通信
客户端代码
package com.wangluo;importjava.io.*;import java.net.Socket;import java.net.UnknownHostException;publicclass Client {
publicstaticvoid main(String[] args) {
try {
//1.创建客户端Socket,指定服务器端地址和端口Socket socket =newSocket("localhost",8888);
//2.获取输出流,向服务器端发送信息OutputStream os = socket.getOutputStream();//字节输出流PrintWriter pw =newPrintWriter(os);//将输出流包装为打印流pw.write("用户名:admin;密码:123");
pw.write("用户名:admin2;密码:456");
pw.flush();
socket.shutdownOutput();//关闭输出流,对于 TCP 套接字,任何以前写入的数据都将被发送
//3.获取输入流,并读取服务器的响应信息InputStream is = socket.getInputStream();
BufferedReader br =newBufferedReader(new InputStreamReader(is));
String info =null;
while((info = br.readLine()) !=null) {
System.out.println("我是客户端,服务器端说:"+info);
}
//4.关闭资源 br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果服务端的端口服务没有起来,运行客户端的程序会抛异常
java.net.ConnectException: Connection refused: connect
服务端代码--服务多个客户
package com.wangluo;importjava.io.*;import java.net.ServerSocket;import java.net.Socket;publicclass Server {
publicstaticvoid main(String[] args) {
try {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口ServerSocket serverSocket =newServerSocket(8888);
//2.调用accept()方法开始监听,等待客户端 的连接System.out.println("***服务器即将启动,等待客户端的连接***");
while(true) {
//监听端口Socket socket = serverSocket.accept();
new MyThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
staticclassMyThreadextends Thread{
private Socket socket;
public MyThread(Socket socket){
this.socket=socket;
}
@Override
publicvoid run() {
try {
server(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
privatevoidserver(Socket socket)throws IOException {
//3.获取输入流,并读取客户端信息InputStream is = socket.getInputStream();//字节输入流InputStreamReader isr =newInputStreamReader(is);//将字节输入流转换为字符流BufferedReader br =newBufferedReader(isr);//为输入流添加缓冲String info =null;
while((info=br.readLine())!=null) {//循环读取客户端的信息System.out.println("我是服务器,客户端说:"+info);
}
OutputStream os = socket.getOutputStream();//字节输出流PrintWriter pw =newPrintWriter(os);//将输出流包装为打印流pw.write("我是服务端");
pw.flush();
socket.shutdownInput();//关闭输入流
//4.关闭资源 br.close();
isr.close();
is.close();
socket.close();
}
}
}
如果服务端监听的端口已经被使用了,在启动服务端的服务时候会抛异常
java.net.BindException
输出结果:(先启动服务端,再启动客户端)
服务端输出:我是服务器,客户端说:用户名:admin;密码:123用户名:admin2;密码:456
客户端输出:我是客户端,服务器端说:我是服务端
3、套接字发送和接受对象
对象必须实现Serializable接口
package com.wangluo;import java.io.Serializable;publicclassTestimplements Serializable{
privatestaticlongserialVersionUID=1L;
privateint num;
private String name;
publicTest(int num, String name){
this.num=num;
this.name=name;
}
publicint getNum() {
return num;
}
publicvoidsetNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
@Override
public String toString() {
return"Test{" + "num=" + num + ", name='" + name + '\'' + '}';
}
}
客户端代码
package com.wangluo;importjava.io.*;import java.net.Socket;import java.net.UnknownHostException;publicclass Client {
publicstaticvoid main(String[] args) {
try {
//1.创建客户端Socket,指定服务器端地址和端口Socket socket =newSocket("localhost",8888);
//2.获取输出流,向服务器端发送信息OutputStream os = socket.getOutputStream();//字节输出流// PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流// pw.write("用户名:admin;密码:123");// pw.write("用户名:admin2;密码:456");// pw.flush();ObjectOutputStream objectOutputStream=new ObjectOutputStream(os);
objectOutputStream.writeObject(newTest(123,"deng"));
objectOutputStream.flush();
socket.shutdownOutput();//关闭输出流
//3.获取输入流,并读取服务器的响应信息InputStream is = socket.getInputStream();
BufferedReader br =newBufferedReader(new InputStreamReader(is));
String info =null;
while((info = br.readLine()) !=null) {
System.out.println("我是客户端,服务器端说:"+info);
}
//4.关闭资源 br.close();
is.close();// pw.close(); objectOutputStream.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端代码
package com.wangluo;importjava.io.*;import java.net.ServerSocket;import java.net.Socket;publicclass Server {
publicstaticvoid main(String[] args) {
try {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口ServerSocket serverSocket =newServerSocket(8888);
//2.调用accept()方法开始监听,等待客户端 的连接System.out.println("***服务器即将启动,等待客户端的连接***");
while(true) {
//监听端口Socket socket = serverSocket.accept();
new MyThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
staticclassMyThreadextends Thread{
private Socket socket;
public MyThread(Socket socket){
this.socket=socket;
}
@Override
publicvoid run() {
try {
server(socket);
} catch (Exception e) {
e.printStackTrace();
}
}
privatevoidserver(Socket socket)throws Exception {
//3.获取输入流,并读取客户端信息InputStream is = socket.getInputStream();//字节输入流// InputStreamReader isr = new InputStreamReader(is);//将字节输入流转换为字符流// BufferedReader br = new BufferedReader(isr);//为输入流添加缓冲// String info = null;// while((info=br.readLine())!=null) {//循环读取客户端的信息// System.out.println("我是服务器,客户端说:"+info);// } ObjectInputStream objectInputStream=new ObjectInputStream(is);
Object test=objectInputStream.readObject();
System.out.println("我是服务器,客户端说:"+test);
OutputStream os = socket.getOutputStream();//字节输出流PrintWriter pw =newPrintWriter(os);//将输出流包装为打印流pw.write("服务端应答");
pw.flush();
socket.shutdownInput();//关闭输入流
//4.关闭资源// br.close();// isr.close(); objectInputStream.close();
is.close();
socket.close();
} 欢迎工作一到五年的Java工程师朋友们加入Java群: 891219277
群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!