赞
踩
SocketTCP01Server.java
package java学习.网络编程.TCP; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class SocketTCP01Server { public static void main(String[] args) throws IOException { // 编写一个服务器端 // 服务器端在9999端口监听 // 在本机 的 9999 端口监听, 等待连接 // 要求在本机没有其它服务在监听 9999 // 这个 ServerSocket 可以通过 accept() 返回多个 Socket[多个客户端连接服务器的并发] ServerSocket serverSocket = new ServerSocket(9999); System.out.println( "服务端,在9999端口监听,等待连接..." ); // 当没有客户端连接 9999 端口时,程序会 阻塞, 等待连接 // 如果有客户端连接,则会返回 Socket 对象,程序继续 Socket socket = serverSocket.accept(); System.out.println( "服务端 socket=" + socket.getClass() ); // 服务器端接收到客户端发送的信息,输出,并退出 // 通过 socket.getInputStream() 读取客户端写入到数据通道的数据, 显示 InputStream inputStream = socket.getInputStream(); // IO 读取 byte[] buf = new byte[1024]; int readLen = 0; while ( (readLen=inputStream.read(buf))!=-1 ) { //根据读取到的实际长度,显示内容. System.out.println( new String(buf, 0, readLen) ); } // 关闭流和 socket inputStream.close(); socket.close(); serverSocket.close(); } }
SocketTCP01Client.java
package java学习.网络编程.TCP; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class SocketTCP01Client { public static void main(String[] args) throws IOException { // 连接服务端 (ip , 端口) // 连接本机的 9999 端口, 如果连接成功,返回 Socket 对象 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println( "客户端 socket=" + socket.getClass() ); // 连接上后,生成 Socket, 通过 socket.getOutputStream() // 得到 和 socket 对象关联的输出流对象 OutputStream outputStream = socket.getOutputStream(); // 通过输出流,写入数据到 数据通道 outputStream.write("hello server".getBytes()); // 关闭流对象和 socket, 必须关闭 outputStream.close(); socket.close(); System.out.println( "客户端退出" ); } }
SocketTCP02Server
package java学习.网络编程.TCP; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class SocketTCP02Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println( "服务端在9999端口监听..." ); Socket socket = serverSocket.accept(); System.out.println( "服务端 socket=" + socket.getClass() ); // 输入流,读取 InputStream inputStream = socket.getInputStream(); byte[] buf = new byte[1024]; int readLen = 0; while ( (readLen=inputStream.read(buf))!=-1 ) { System.out.println( new String(buf, 0, readLen) ); } // 输出流,输出 OutputStream outputStream = socket.getOutputStream(); outputStream.write( "hello client".getBytes() ); // 输出结束标志 socket.shutdownOutput(); // 关闭流 outputStream.close(); inputStream.close(); socket.close(); serverSocket.close(); } }
SocketTCP02Client
package java学习.网络编程.TCP; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class SocketTCP02Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(), 9999); System.out.println( "客户端 socket=" + socket.getClass() ); OutputStream outputStream = socket.getOutputStream(); outputStream.write("hello server".getBytes()); // 输出结束标志 socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); int readLen = 0; byte[] buf = new byte[1024]; while ( (readLen=inputStream.read(buf))!=-1 ) { System.out.println( new String(buf, 0, readLen) ); } inputStream.close(); outputStream.close(); socket.close(); System.out.println("客户端退出..."); } }
SocketTCP03Server
package java学习.网络编程.TCP; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class SocketTCP03Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9999); System.out.println( "服务端在9999端口监听" ); Socket accept = serverSocket.accept(); InputStream inputStream = accept.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s = bufferedReader.readLine(); System.out.println(s); // 输出 OutputStream outputStream = accept.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("hello client 字符流"); // 插入一个换行符,表示回复内容的结束 bufferedWriter.newLine(); bufferedWriter.flush(); // 关闭 bufferedWriter.close(); bufferedReader.close(); accept.close(); serverSocket.close(); } }
SocketTCP03Client
package java学习.网络编程.TCP; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class SocketTCP03Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(), 9999); OutputStream outputStream = socket.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("hello server 字符流"); // 插入一个换行符,表示写入的内容结束, 注意,要求对方使用 readLine()!!!! bufferedWriter.newLine(); bufferedWriter.flush(); InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = bufferedReader.readLine(); System.out.println(line); // 关闭 bufferedReader.close(); bufferedWriter.close(); socket.close(); System.out.println("客户端退出..."); } }
编写一个服务端,和一个客户端
服务器端在8888端口监听
客户端连接到服务端,发送一张图片 e:\bg.jpg
服务器端接收到客户端发送的图片,保存到src下,发送"收到图片”再退出
客户端接收到服务端发送的“收到图片”,再退出
该程序要求使用StreamUtils.java,我们直接使用
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * 此类用于演示关于流的读写方法 * */ public class StreamUtils { /** * 功能:将输入流转换成byte[], 即可以把文件的内容读入到byte[] * @param is * @return * @throws Exception */ public static byte[] streamToByteArray(InputStream is) throws Exception{ ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象 byte[] b = new byte[1024];//字节数组 int len; while((len=is.read(b))!=-1){//循环读取 bos.write(b, 0, len);//把读取到的数据,写入bos } byte[] array = bos.toByteArray();//然后将bos 转成字节数组 bos.close(); return array; } /** * 功能:将InputStream转换成String * @param is * @return * @throws Exception */ public static String streamToString(InputStream is) throws Exception{ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder builder= new StringBuilder(); String line; while((line=reader.readLine())!=null){ builder.append(line+"\r\n"); } return builder.toString(); } }
TCPFileUploadServer.java
TCPFileUploadClient.java
package java学习.网络编程.TCP; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TCPFileUploadServer { public static void main(String[] args) throws IOException { // 服务器在8888端口监听 ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器在8888端口监听"); // 等待连接 Socket socket = serverSocket.accept(); System.out.println( "服务端 socket=" + socket.getClass() ); } }
package java学习.网络编程.TCP; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class TCPFileUploadClient { public static void main(String[] args) throws Exception { // 连接服务端的8888端口 Socket socket = new Socket(InetAddress.getLocalHost(), 8888); // 文件的路径 String filePath = "e:\\bg.jpg"; // 输入流,读取图片 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); // 调用StreamUtils.streamToByteArray读取图片,获取二进制数组 byte[] bytes = StreamUtils.streamToByteArray(bis); // 通过 socket 获取到输出流, 将 bytes 数据发送给服务端 BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); bos.write(bytes);//将文件对应的字节数组的内容,写入到数据通道 socket.shutdownOutput();//设置写入数据的结束标记 // 关闭流 bos.close(); bis.close(); socket.close(); } }
package java学习.网络编程.TCP; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TCPFileUploadServer { public static void main(String[] args) throws Exception { // 服务器在8888端口监听 ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器在8888端口监听"); // 等待连接 Socket socket = serverSocket.accept(); System.out.println( "服务端 socket=" + socket.getClass() ); // 输入流,读取流中数据 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); // 调用StreamUtils.streamToByteArray将输入流中的数据读到数组中 byte[] bytes = StreamUtils.streamToByteArray(bis); // 将得到 bytes 数组,写入到指定的路径,就得到一个文件了 // 路径 String filePath = "src\\bg.jpg"; // 输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); bos.write(bytes); // 关闭 bos.close(); bis.close(); socket.close(); serverSocket.close(); } }
package java学习.网络编程.TCP; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TCPFileUploadServer { public static void main(String[] args) throws Exception { // 服务器在8888端口监听 ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器在8888端口监听"); // 等待连接 Socket socket = serverSocket.accept(); System.out.println( "服务端 socket=" + socket.getClass() ); // 输入流,读取流中数据 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); // 调用StreamUtils.streamToByteArray将输入流中的数据读到数组中 byte[] bytes = StreamUtils.streamToByteArray(bis); // 将得到 bytes 数组,写入到指定的路径,就得到一个文件了 // 路径 String filePath = "src\\bg.jpg"; // 输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); bos.write(bytes); // 回复信息 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bw.write("收到图片"); bw.flush();//把内容刷新到数据通道 // 客户端以字节流读取信息,可以设置如下结束标记 socket.shutdownOutput();//设置写入结束标记 // 关闭 bw.close(); bos.close(); bis.close(); socket.close(); serverSocket.close(); } }
package java学习.网络编程.TCP; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class TCPFileUploadClient { public static void main(String[] args) throws Exception { // 连接服务端的8888端口 Socket socket = new Socket(InetAddress.getLocalHost(), 8888); // 文件的路径 String filePath = "e:\\bg.jpg"; // 输入流,读取图片 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); // 调用StreamUtils.streamToByteArray读取图片,获取二进制数组 byte[] bytes = StreamUtils.streamToByteArray(bis); // 通过 socket 获取到输出流, 将 bytes 数据发送给服务端 BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); bos.write(bytes);//将文件对应的字节数组的内容,写入到数据通道 socket.shutdownOutput();//设置写入数据的结束标记 // 接收服务端回复的信息 InputStream inputStream = socket.getInputStream(); // 使用 StreamUtils 的方法,直接将 inputStream 读取到的内容 转成字符串 String s = StreamUtils.streamToString(inputStream); System.out.println(s); // 关闭流 inputStream.close(); bos.close(); bis.close(); socket.close(); } }
说明:
(1) Listening表示某个端口在监听
(2)如果有一个外部程序(客户端)连接到该端口,就会显示一条连接信息.
(3)可以输入ctrl +c退出指令
netstat -an
netstat -an | more
netstat -anb
可以查看哪些应用在监听端口(要用管理员的身份打开命令窗口)
当客户端连接到服务端后,实际上客户端也是通过一个端口和服务端进行通讯的,这个端口是TCP/IP来分配的,是不确定的,是随机的.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。