会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132382个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3346楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3347楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3348楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3351楼

老师好,请看一下,

异常名称:Exception in thread "main" java.net.SocketException: Connection reset

想要知道怎么避免,怎么操作,请演示一下

已通过防火墙

image.png

客户端:

package cn.sxt.entity;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client2 {
	public static void main(String[] args) throws IOException {
		//1创建socket对象,用于连接服务器
		Socket client = new Socket("localhost", 9999);
		//2获取输出流(对象流)
		ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
		//3创建user对象
					//获取user对象的方法
		User2 user = getUser();//new User2("sxt", "sxt");
		//4user对象发送到服务器
		oos.writeObject(user);//发生了向上转型
		//5获取输入流(数据流)
		DataInputStream dis = new DataInputStream(client.getInputStream());
		System.out.println(dis.readUTF());
		//6关闭流
		if (dis!=null) {
			dis.close();
		}
		if (oos!=null) {
			oos.close();
		}
		if (client!=null) {
			client.close();
		}
	}
	public static User2 getUser() {//获取对象的方法
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入账户名");
		String userName = sc.next();
		System.out.println("请输入密码");
		String passWord = sc.next();
		return new User2(userName, passWord);
	}
}

服务器端:

package cn.sxt.server;

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


import cn.sxt.entity.User2;

public class Server2 {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		System.out.println("----------服务器端已启动---------");
		//1.创建ServerSocket对象
		ServerSocket server = new ServerSocket(9999);
		Socket client = server.accept();
		//2.创建输入流--->ObjectInputStream
		ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
		User2 user = (User2)ois.readObject();//向下转型
		System.out.println(client.getInetAddress().getHostAddress()+"请求登录:用户名:"+user.getUserName()+"\t密码:"+user.getPassWord());
		//3.对用户名和密码进行验证
		String str = "";
		if ("sxt".equals(user.getUserName())&&"sxt".equals(user.getPassWord())) {
			str = "登录成功";
		}else {
			str = "对不起,用户名或密码错误";
		}
		//4.获取输出流(数据流)
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
		dos.writeUTF(str);
		//5.关闭流
		if (dos!=null) {
			dos.close();
		}
		if (ois!=null) {
			ois.close();
		}
		if (client!=null) {
			client.close();
		}
	}
}

运行效果:

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3353楼

老师好,请看一下:

客户端和服务器端处理异常使用了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 基础深化和提高/网络编程(旧) 3354楼

老师好,请看一下:

问题:无法获取Goods类的brand属性

商品类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cn.sxt.thread;
 
public class Goods {
    private String name;
    private String brand;
    private boolean flag;
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3355楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3356楼

老师好,请看一下:

问题:无法获取Goods类的brand属性

商品类:

package cn.sxt.thread;

public class Goods {
	private String name;
	private String brand;
	private boolean flag;//标志位,用来表示是否存在商品  flag = true表示有商品
	public Goods(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	//同步的用来获取商品的方法,用来避免数据错乱,线程通信notify和wait用来防止数据重复
	public synchronized void get() {
		if (!flag) {//相当于flag = false没有商品,消费者等待
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//消费者线程被唤醒,开始消费
		System.out.println("消费者消费:"+this.getBrand()+"\t"+this.getName());
		//通知生产者生产
		super.notify();
		flag = false;//表示没有商品了
	}
	//同步方法用来生产商品,同步用来避免数据错乱,线程通信notify/wait用来防止数据重复生产或者消费
	public synchronized void set(String Brand,String name) {
		if (flag) {//表示有商品,生产等待
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//生产者线程被唤醒,开始生产
		this.name = name;
		this.brand = brand;
		System.out.println("生产者生产:"+this.getBrand()+"\t"+this.getName());
		//通知消费者消费
		super.notify();
		flag = true;//表示 有商品
		
	}
}

生产者:

package cn.sxt.thread;

public class Producer implements Runnable {
	private Goods g;
	
	public Producer(Goods g) {
		super();
		this.g = g;
	}

	public Producer() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++) {
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (i%2==0) {//偶数				
				g.set("旺仔", "小馒头");
			}else {
				g.set("哇哈哈", "矿泉水");
			}
			
		}
	}
	
}

消费者:

package cn.sxt.thread;

public class Consumer implements Runnable {
	private Goods g ;

	public Consumer(Goods g) {
		super();
		this.g = g;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++) {
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			g.get();
		
		}
	}
	
}

测试类:

package cn.sxt.thread;

public class TestGoods {
	public static void main(String[] args) {
		//创建 商品类
		Goods g  =  new Goods();
		//创建生产者,消费者线程
		Producer p = new Producer(g);
		Consumer c = new Consumer(g);
		//启动线程
		new Thread(p).start();
		new Thread(c).start();
	}
}

运行效果截图:

image.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3357楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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