Java中网络编程之TCP协议
一、TCP的基本概念TCP是专门设计用于在不可靠的英特网上提供可靠的、端到端的字节流通信的协议,是一个面向连接的协议,TCP连接是字节流而非报文流。UDP和TCP各有65536个端口号互不影响。二、单线程服务端以下代码只能实现服务端和客户端的同步对话。服务端处理完一个客户端请求,才会处理另一个客户端请求。服务器端的输出效果是Client1阻塞20秒,Client2不会执行。必须等Client1阻塞结束之后,Client2才会执行。该例子可用来学习TCP的基本语法。[java]/*** TCP客户端1** @author 徐越**/public class Client1{public static void main(String[] args) throws Exception{Socket socket = new Socket("127.0.0.1", 8821);OutputStream os = socket.getOutputStream();String msg = "我是徐越1";Thread.sleep(20000);os.write(msg.getBytes());os.flush();os.close();socket.close();}}/*** TCP客户端2** @author 徐越**/public class Client2{public static void main(String[] args) throws Exception{Socket socket = new Socket("127.0.0.1", 8821);OutputStream os = socket.getOutputStream();String msg = "我是徐越2";os.write(msg.getBytes());os.flush();os.close();socket.close();}}/*** 单线程TCP服务端** @author 徐越**/public class Server{public static void main(String[] args) throws Exception{// 创建端口为8821的TCP服务器端对象// 8821是服务器端的端口号而客户端从某端口A连到8821,端口A是随机的ServerSocket serverSocket = new ServerSocket(8821);while (true){// 若无客户端发送请求则线程在此阻塞,方法不继续执行Socket socket = serverSocket.accept();System.out.println("connected");InputStream instream = socket.getInputStream();ByteArrayOutputStream bos = new ByteArrayOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = instream.read(buffer)) != -1){bos.write(buffer, 0, len);}instream.close();bos.flush();bos.close();String msg = bos.toString();System.out.println("客户端的IP:" + socket.getInetAddress().getHostAddress());System.out.println("客户端的端口:" + socket.getPort());System.out.println("客户端的信息:" + msg);}}}/*** TCP客户端1** @author 徐越**/public class Client1{public static void main(String[] args) throws Exception{Socket socket = new Socket("127.0.0.1", 8821);OutputStream os = socket.getOutputStream();String msg = "我是徐越1";Thread.sleep(20000);os.write(msg.getBytes());os.flush();os.close();socket.close();}}/*** TCP客户端2** @author 徐越**/public class Client2{public static void main(String[] args) throws Exception{Socket socket = new Socket("127.0.0.1", 8821);OutputStream os = socket.getOutputStream();String msg = "我是徐越2";os.write(msg.getBytes());os.flush();os.close();socket.close();}}/*** 单线程TCP服务端** @author 徐越**/public class Server{public static void main(String[] args) throws Exception{// 创建端口为8821的TCP服务器端对象// 8821是服务器端的端口号而客户端从某端口A连到8821,端口A是随机的ServerSocket serverSocket = new ServerSocket(8821);while (true){// 若无客户端发送请求则线程在此阻塞,方法不继续执行Socket socket = serverSocket.accept();System.out.println("connected");InputStream instream = socket.getInputStream();ByteArrayOutputStream bos = new ByteArrayOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = instream.read(buffer)) != -1){bos.write(buffer, 0, len);}instream.close();bos.flush();bos.close();String msg = bos.toString();System.out.println("客户端的IP:" + socket.getInetAddress().getHostAddress());System.out.println("客户端的端口:" + socket.getPort());System.out.println("客户端的信息:" + msg);}}}执行结果connected客户端的IP:127.0.0.1客户端的端口:3775客户端的信息:我是徐越1connected客户端的IP:127.0.0.1客户端的端口:3787客户端的补充:软件开发 , Java ,
上一个:排序算法之冒泡排序(JAVA)
下一个:TWaver Java内存占用测试
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值