套接字(Socket)开发网络应用程序被广泛采用,以至于成为事实上的标准。2.通信的两端都要有Socket,是两台机器间通信的端点
套接字(套接字)开发网络应用程序被广泛采用,以至于成为事实上的标准.2.通信的两端都要有套接字,是两台机器间通信的端点
网络通信其实就是Socket间的通信
Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输。5.一般主动发起通信的应用程序属客户端,等待通信请求的为服务端
示意图:
客户端向服务器端发送hello,服务器端接收
思路:
服务器端:
在本机监听9999端口,等待连接
ServerSocket serverSocket = new ServerSocket(9999);
注:9999端口没被监听或占用
当程序没有被客户端连接时,会阻塞,等待连接,如果有客户端连接,会返回一个Socket对象,程序继续
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] buff = new byte[1024]; int len=0; while ((len=inputStream.read(buff))!=-1){ System.out.println(new String(buff,0,len)); }
serverSocket.close(); socket.close(); inputStream.close();
代码:
public static void main(String[] args) throws IOException { //1.在本机监听9999端口,等待连接 //注:9999端口没被监听或占用 //serverSocket可以通过accept()得到多个socket,可以有多个客户端来访问 ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端在9999端口监听,等待连接"); //2.当程序没有被客户端连接时,会阻塞,等待连接 //如果有客户端连接,会返回一个Socket对象,程序继续 Socket socket = serverSocket.accept(); //当客户端没有连接时,下面这句话不会执行 System.out.println("服务器socket="+socket.getClass()); //3.通过socket.getInputStream()读取客户端写入数据通道的数据 InputStream inputStream = socket.getInputStream(); //4.读取(字节方式) byte[] buff = new byte[1024]; int len=0; while ((len=inputStream.read(buff))!=-1){ System.out.println(new String(buff,0,len)); } //关闭流 注:服务器多关一个流 serverSocket.close(); socket.close(); inputStream.close(); }
客户端:
连接服务器(ip+端口)
InetAddress.getLocalHost()获取本机ip,连接成功,返回Socket对象
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
连接上以后,通过getOutputStream()得到和socket对象关联的输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes());
outputStream.close(); socket.close(); System.out.println("客户端退出");
代码:
public static void main(String[] args) throws IOException { //1.连接服务器(ip+端口) //InetAddress.getLocalHost()获取本机ip,连接成功,返回Socket对象 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("客户端Socket="+socket.getClass()); //2.连接上以后,通过getOutputStream()得到和socket对象关联的输出流 OutputStream outputStream = socket.getOutputStream(); //3.通过输出流,写入数据到数据通道 outputStream.write("hello".getBytes()); //4.关闭流 outputStream.close(); socket.close(); System.out.println("客户端退出"); }
客户端向服务器端发送hello,服务器端接收,服务器向客户端发送helloClient
思路:
OutputStream outputStream = socket.getOutputStream(); outputStream.write("helloClient".getBytes());
注:这里写完后一定要关闭,即结束标记,不然就会阻塞在这里,有点向线程死锁
socket.shutdownOutput();
outputStream.close();
代码:
public static void main(String[] args) throws IOException { //1.在本机监听9999端口,等待连接 //注:9999端口没被监听或占用 //serverSocket可以通过accept()得到多个socket,可以有多个客户端来访问 ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端在9999端口监听,等待连接"); //2.当程序没有被客户端连接时,会阻塞,等待连接 //如果有客户端连接,会返回一个Socket对象,程序继续 Socket socket = serverSocket.accept(); //当客户端没有连接时,下面这句话不会执行 System.out.println("服务器socket="+socket.getClass()); //3.通过socket.getInputStream()读取客户端写入数据通道的数据 InputStream inputStream = socket.getInputStream(); //4.读取(字节方式) byte[] buff = new byte[1024]; int len=0; while ((len=inputStream.read(buff))!=-1){ System.out.println(new String(buff,0,len)); } //5.向客户端发送helloClient OutputStream outputStream = socket.getOutputStream(); outputStream.write("helloClient".getBytes()); //一定要关闭,不然就会阻塞在这里,有点向线程死锁 socket.shutdownOutput(); //关闭流 注:服务器多关一个流 outputStream.close(); serverSocket.close(); socket.close(); inputStream.close(); }
InputStream inputStream = socket.getInputStream(); byte[] buff = new byte[1024]; int len=0; while ((len = inputStream.read(buff))!=-1){ System.out.println(new String(buff,0,len)); }
outputStream.write("hello".getBytes()); socket.shutdownOutput();
inputStream.close();
代码:
public static void main(String[] args) throws IOException { //1.连接服务器(ip+端口) //InetAddress.getLocalHost()获取本机ip,连接成功,返回Socket对象 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("客户端Socket="+socket.getClass()); //2.连接上以后,通过getOutputStream()得到和socket对象关联的输出流 OutputStream outputStream = socket.getOutputStream(); //3.通过输出流,写入数据到数据通道 outputStream.write("hello".getBytes()); //注意: //一定要关闭,不然就会阻塞在这里,有点向线程死锁 socket.shutdownOutput(); //4.接收服务器端的信息 InputStream inputStream = socket.getInputStream(); byte[] buff = new byte[1024]; int len=0; while ((len = inputStream.read(buff))!=-1){ System.out.println(new String(buff,0,len)); } //4.关闭流 inputStream.close(); outputStream.close(); socket.close(); System.out.println("客户端退出"); }
在使用字节流数据传输编程2改变输入输出流就行,即将字节流转换为字符流
服务器端:
public static void main(String[] args) throws IOException { //1.在本机监听9999端口,等待连接 //注:9999端口没被监听或占用 //serverSocket可以通过accept()得到多个socket,可以有多个客户端来访问 ServerSocket serverSocket = new ServerSocket(9999); System.out.println("服务端在9999端口监听,等待连接"); //2.当程序没有被客户端连接时,会阻塞,等待连接 //如果有客户端连接,会返回一个Socket对象,程序继续 Socket socket = serverSocket.accept(); //当客户端没有连接时,下面这句话不会执行 System.out.println("服务器socket="+socket.getClass()); //3.通过socket.getInputStream()读取客户端写入数据通道的数据 InputStream inputStream = socket.getInputStream(); //4.读取(字符方式) BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s = bufferedReader.readLine(); System.out.println(s); //5.向客户端发送helloClient OutputStream outputStream = socket.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("helloClient"); bufferedWriter.newLine(); bufferedWriter.flush(); bufferedWriter.close(); bufferedWriter.close(); socket.close(); inputStream.close(); }
客户端:
public static void main(String[] args) throws IOException { //1.连接服务器(ip+端口) //InetAddress.getLocalHost()获取本机ip,连接成功,返回Socket对象 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println("客户端Socket="+socket.getClass()); //2.连接上以后,通过getOutputStream()得到和socket对象关联的输出流 OutputStream outputStream = socket.getOutputStream(); //3.通过输出流,写入数据到数据通道 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("helloServer"); bufferedWriter.newLine();//插入一个换行符,表示内容结束,要求对方使用readLine来读 bufferedWriter.flush();//使用字符流一定要刷新,否则数据不会写入 //4.接收服务器端的信息 InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s = bufferedReader.readLine(); System.out.println(s); //4.关闭流,关闭外层流就行 bufferedReader.close(); bufferedWriter.close(); socket.close(); System.out.println("客户端退出"); }