JAVA网络编程基础应用练习,200分! 大家帮我做一下。 谢谢了 就是我服务端有一些学生信息。
发布网友
发布时间:2022-04-25 21:29
我来回答
共4个回答
热心网友
时间:2023-11-06 10:34
一下代码仅作参考 swing界面使用NetBeans生成的
ClientView.java见附件
Client.java
package ;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
private ClientView clientView;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
public Client() {
clientView = new ClientView(this);
clientView.setVisible(true);
}
public ClientView getClientView() {
return clientView;
}
public void setClientView(ClientView clientView) {
this.clientView = clientView;
}
public static void main(String[] args) {
new Client();
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
/**
* 连接服务器
* @return 成功返回true 否则返回false
*/
public boolean connectServer() {
try {
socket = new Socket("127.0.0.1", 8853);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 从服务端读取消息
* @return 成功返回对应的字符串 否则返回null
*/
public String getMessage() {
String result;
try {
result = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return result;
}
/**
* 发送消息到服务端
* @param message
* @return
*/
public boolean sendMessage(String message) {
try {
dos.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 把字符串解码成学生类
*
* @param s
* @return
*/
public Student deCode(String s) {
String[] ss = s.split(",");
Student student = new Student(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), ss[3], ss[4]);
return student;
}
}
Server.java
package ;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Server {
private int port;
private String ip;
private ServerSocket serverSocket;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
private Map<Integer, Student> studentMap;
/**
* 下一个新学生的学号
*/
private static int nextId = 101;
public Server(int port) throws IOException {
this.port = port;
serverSocket = new ServerSocket(port);
studentMap = new HashMap<>();
studentMap.put(101, new Student(101, "吴冰", 25, "男", "04软件3班"));
studentMap.put(102, new Student(102, "曾小梅", 21, "女", "01软件4班"));
studentMap.put(103, new Student(103, "廖蒋龙", 22, "男", "01软件1班"));
studentMap.put(104, new Student(104, "李世强", 24, "男", "04软件2班"));
studentMap.put(105, new Student(105, "曹鹏", 20, "男", "01软件2班"));
studentMap.put(106, new Student(106, "卢刚", 23, "男", "01软件3班"));
studentMap.put(107, new Student(107, "吴伟坚", 20, "男", "04软件3班"));
nextId += studentMap.size();
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public ServerSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public Map<Integer, Student> getStudentMap() {
return studentMap;
}
public void setStudentMap(Map<Integer, Student> studentMap) {
this.studentMap = studentMap;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public Socket accept() throws IOException {
socket = serverSocket.accept();
System.out.println("客户端" + socket.getRemoteSocketAddress() + "连接上来了.");
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
return socket;
}
public String readUTF() throws IOException {
return dis.readUTF();
}
public boolean sendMessage(String message) {
try {
dos.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
Server server;
try {
server = new Server(8853);
server.accept();
String message = "";
while (!message.equals("quit")) {
try {
message = server.readUTF();
System.out.println(message);
} catch (IOException e) {
server.close();
// e.printStackTrace();
System.out.println("接收客户端命令时出错,客户端可能已经关闭.");
break;
}
server.doCommond(message);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void close() {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 服务端与客户端的交互处理方法
*
* @param message
* @return
*/
public boolean doCommond(String message) {
if (message.startsWith("getAll")) {
/**
* 查询所有学生
*/
String result = "";
Collection<Student> students = studentMap.values();
Iterator<Student> studentIte = students.iterator();
while (studentIte.hasNext()) {
result += studentIte.next().coding();
}
return sendMessage("true:" + result);
} else if (message.startsWith("getStu:id=")) {
/**
* 按学号查找
*/
int id;
try {
id = Integer.parseInt(message.substring(10));
} catch (NumberFormatException e) {
sendMessage("false:参数不合法");
e.printStackTrace();
return false;
}
Student student = studentMap.get(id);
if (student == null) {
sendMessage("false:不存在此学生");
return false;
} else {
sendMessage("true:" + student.coding());
}
return true;
} else if (message.startsWith("addStu:{")) {
/**
* 添加学生信息
*/
String stuMessage = message.substring(message.indexOf("{")+1, message.indexOf("}"));
Student student = deCode(stuMessage);
student.setId(nextId);
nextId++;
studentMap.put(student.getId(), student);
String result = "";
Collection<Student> students = studentMap.values();
Iterator<Student> studentIte = students.iterator();
while (studentIte.hasNext()) {
result += studentIte.next().coding();
}
return sendMessage("true:" + result);
} else if (message.startsWith("updateStu:{")) {
/**
* 更新学生信息
*/
String stuMessage = message.substring(message.indexOf("{") + 1, message.indexOf("}"));
Student student = deCode(stuMessage);
if (studentMap.get(student.getId()) == null) {
sendMessage("false:不存在该学号.");
return false;
}
studentMap.put(student.getId(), student);
sendMessage("true");
return true;
} else {
System.out.println("没有对应的命令");
return false;
}
}
/**
* 把字符串解码生学生类
*
* @param s
* @return
*/
public Student deCode(String s) {
String[] ss = s.split(",");
Student student = new Student(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), ss[3], ss[4]);
return student;
}
}
class Student {
private int id;
private String name;
private int age;
private String sex;
private String className;
public Student(int id, String name, int age, String sex, String className) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.className = className;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "{" + id + "," + name + "," + age + "," + sex + "," + className + "}";
}
/**
* 编码方式
*
* @return
*/
public String coding() {
return "{" + id + "," + name + "," + age + "," + sex + "," + className + "}";
}
}
热心网友
时间:2023-11-06 10:34
这个程序代码还有点多,要不给个邮箱给我,刚好今天有空追问我扣 317563384 我手机端上面只能加一百分。放学后回家把提问加到200分,实在感谢,主要是我不是很会。
热心网友
时间:2023-11-06 10:35
你觉得这样能问出什么来吗?
热心网友
时间:2023-11-06 10:35
实现后告诉你追问我扣 317563384 我手机端上面只能加一百分。放学后回家把提问加到200分,实在感谢,主要是我不是很会。