会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132582个问题
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/虚拟环境 25893楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 25894楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 25895楼
Python 全系列/第五阶段:数据库编程/mysql的使用 25896楼

image.png

Python 全系列/第一阶段:Python入门/编程基本概念 25897楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 25899楼
Python 全系列/第一阶段:Python入门/编程基本概念 25900楼

老师好,请看一下:

客户端和服务器端处理异常使用了try-catch, 先启动的服务器端,然后是客户端,运行结果显示服务器端收到和客户端一样的信息。

客户端代码:

package cn.sxt.net;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestSocket {
	public static void main(String[] args) {
		// 1)创建Socket对象,指定要连接服务器的IP地址和监听的端口号
		Socket client = null;
		// 2.获取输出流
		DataOutputStream dos = null;
		// 3.获取输入流
		DataInputStream dis = null;
		try {			
			client = new Socket("localhost", 9999);
			dos = new DataOutputStream(client.getOutputStream());
			dos.writeUTF("helloworld");
			dis = new DataInputStream(client.getInputStream());
			System.out.println(dis.readUTF());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 4.关闭流
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (client!=null) {
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

服务器端代码:

package cn.sxt.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerSocket {
	public static void main(String[] args) {
		System.out.println("服务器端启动——————————————");
		// 1.创建serverSocket对象
		ServerSocket server = null;
		// 2.监听客户端的连接
		Socket client = null;
		// 3,获取输入流
		DataInputStream dis = null;
		// 4.获取输出流
		DataOutputStream dos = null;
		try {	
			server = new ServerSocket(9999);
			client = server.accept();
			dis = new DataInputStream(client.getInputStream());
			System.out.println(dis.readUTF());
			dos = new DataOutputStream(client.getOutputStream());
			dos.writeUTF("收到了");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 5释放资源
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

运行结果截图:

image.png

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 25902楼
Python 全系列/第十二阶段:Python_Django3框架/Django初级 25905楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637