1、 使用socket网络编程技术,创建服务器端和客户端。
2、 创建Student类,设置属性stuName,stuAge,stuGender,stuClassName。创建Student.xml用户保存学员姓名,学员年龄,学员性别,学员所在班级
3、 客户端发送查询请求,客户端从键盘输入查询单个学员还是全体学员,通过scoket将查询信息传递给服务器端。
4、 查询全体学员信息,服务器端判断查询请求,服务器端读取Student.xml文件,按照客户端提交的查询请求返回全体学员信息给客户端。客户端在控制台打印出全体学员信息。
5、 查询单个学员信息,客户端从键盘输入要查找的学员姓名,通过socket传递给服务器端,由服务器端读取学员姓名,在Student.xml中找到名字相符的学员。找到则将学员信息通过socket发送给客户端,由客户端打印在控制台上。如果找不到指定学员则向客户端发送查找失败信息。
跪求高手啊,过不了就要留级了,我还有200分,正确的都给你啊,速度啊。。
服务器端:
package com.lqq.service;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class DealClientRequest implements Runnable
{
private Socket s = null;
private ParserXML p = new ParserXML(new File("students.xml"));
public DealClientRequest(Socket s)
{
this.s = s;
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
QueryType qt = (QueryType) ois.readObject();
if(qt.getQueryType() == 1)//单查
{
System.out.println("执行单查,查询的名字= " + qt.getQueryName());
Student stu = p.getStudent(qt.getQueryName());
oos.writeObject(stu);
}else if(qt.getQueryType() == 2)//全查
{
System.out.println("执行全查");
List<Student> list = p.getAllStudent();
for(int i = 0; i < list.size(); i++)
{
Student stu = list.get(i);
oos.writeObject(stu);
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
*************************
package com.lqq.service;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainService
{
private ServerSocket ss;
public void startService() throws IOException
{
ss = new ServerSocket(10086);
while(true)
{
System.out.println("服务器在10086等待...");
Socket s = ss.accept();
new Thread(new DealClientRequest(s)).start();
System.out.println("启动处理线程成功");
}
}
}
******************
package com.lqq.service;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import com.lqq.vo.Student;
public class ParserXML
{
private DocumentBuilderFactory bdf;
private DocumentBuilder db;
private Document dom;
public ParserXML(File file)
{
bdf = DocumentBuilderFactory.newInstance();
try
{
db = bdf.newDocumentBuilder();
dom = db.parse(file);
} catch (Exception e)
{
e.printStackTrace();
}
}
public List<Student> getAllStudent()
{
List<Student> stu = new ArrayList<Student>();
Element root = dom.getDocumentElement();
NodeList list = root.getElementsByTagName("stu");
for(int i = 0; i < list.getLength(); i++)
{
Element e = (Element) list.item(i);
Student st = new Student();
NodeList names = e.getElementsByTagName("name");
if(names.getLength() == 1)
{
Element name = (Element)names.item(0);
Text text = (Text) name.getFirstChild();
st.setStuName(text.getNodeValue());
}
NodeList sexs = e.getElementsByTagName("sex");
if(sexs.getLength() == 1)
{
Element name = (Element)sexs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuGender(text.getNodeValue());
}
NodeList ages = e.getElementsByTagName("age");
if(ages.getLength() == 1)
{
Element name = (Element)ages.item(0);
Text text = (Text) name.getFirstChild();
st.setStuAge(Integer.parseInt(text.getNodeValue()));
}
NodeList classs = e.getElementsByTagName("class");
if(classs.getLength() == 1)
{
Element name = (Element)classs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuClassName(text.getNodeValue());
}
stu.add(st);
}
return stu;
}
public Student getStudent(String stuName)
{
List<Student> list = this.getAllStudent();
for(int i = 0; i < list.size(); i++)
{
Student st = list.get(i);
if(st.getStuName().equals(stuName))
return st;
}
return null;
}
}
*************
package com.lqq.service;
import java.io.IOException;
public class Service
{
public static void main(String[] args)
{
MainService ms = new MainService();
try
{
ms.startService();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
*******
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 单查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
************
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性别: " + stuGender
+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
*************
<?xml version="1.0" encoding="UTF-8"?>
<stus>
<stu>
<name>令狐冲</name>
<age>12</age>
<class>华山派</class>
<sex>男</sex>
</stu>
<stu>
<name>东方不败</name>
<age>22</age>
<class>日月神教</class>
<sex>女</sex>
</stu>
<stu>
<name>岳不群</name>
<age>23</age>
<class>华山派</class>
<sex>妖</sex>
</stu>
<stu>
<name>风清扬</name>
<age>88</age>
<class>华山派</class>
<sex>男</sex>
</stu>
</stus>
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
客户端:
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class AllQuery implements Runnable
{
private Socket s;
public AllQuery(QueryType qt)
{
try
{
s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Student stu = (Student) ois.readObject();
Client c = new Client();
while(stu != null)
{
c.showStudentInfo(stu);
stu = (Student) ois.readObject();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
**************
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class Client
{
public void showStudentInfo(Student stu)
{
if(stu != null)
{
System.out.println(stu);
}
}
public void showMenu()
{
System.out.println("\t\t菜 单");
System.out.println("\t1.单 查");
System.out.println("\t2.全 查");
System.out.println("\t0.退 出");
}
public String getUserInput()
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入:");
return scan.next();
}
public void requestQuery(String userSelect)
{
if(userSelect != null)
{
if("1".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)1);
Scanner san = new Scanner(System.in);
System.out.println("请输入查询的学生名字:");
String queryStuName = san.next();
qt.setQueryName(queryStuName);
this.executeQeury(qt);
}else if("2".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)2);
this.executeQeury(qt);
}else if("0".equals(userSelect.trim()))
{
System.exit(0);
}
else
{
System.out.println("输入有误");
}
}
}
public void executeQeury(QueryType qt)
{
try
{
Socket s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
System.out.println("请求发送完毕");
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
if(qt.getQueryType() == 1)
{
Student stu = (Student) ois.readObject();
showStudentInfo(stu);
}else if(qt.getQueryType() == 2)
{
new Thread(new AllQuery(qt)).start();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Client client = new Client();
while(true)
{
client.showMenu();
client.requestQuery(client.getUserInput());
}
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 单查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性别: " + stuGender
+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
时间太紧,没有完善代码,基本功能实现了,代码没有优化,你自己再改改,希望可以帮到你
根据要求编写java代码,急求,答案正确加100分!求高手!
import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.List; import com.lqq.vo.QueryType; import com.lqq.vo.Student; public class DealClientRequest implements Runnable { private Socket s ...
求高手帮忙编JAVA的程序
求和。输入的数会求其和如输入5 返回5+4+3+2+1
Java编程高手速度来啊~~帮忙写一程序,搞定了给100分!
第一题:import java.util.Scanner;\/ Created by IntelliJ IDEA.User: guangzhi Date: 11-10-19 Time: 下午2:13 To change this template use File | Settings | File Templates.\/ public class JiSuan { public static void main(String[] args){ Scanner cin = new Scanner(System.in);int ...
java编程!高手来帮忙~~
代码如下,如有不明白的地方可以Hi我,我已经测试好了的~!import java.util.ArrayList;import java.util.List;import java.util.Random;class DataException extends Exception{} class Animal { private int weight;public static int count = 0;\/\/记录成功设置的数量 public void setWeight(int weigh...
JAVA编码高手快来!!
} public static void main(String[] args) { int count=0;int right=0;while(count!=10){ int a=getNum(),b=getNum();int c=a+b;System.out.println(a+"+"+b+"=?");Scanner sc=new Scanner(System.in);int result=sc.nextInt();if(c==result){ System.out.println("答案正...
急!求教Java高手!以下是我的Java代码,我在注释中写明了我的想法,和问题...
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;class jfr extends JFrame { String getpath;String gettype;String getname;JTextField jf1 = new JTextField();\/\/ 接收用户输入的文件夹路径 JTextField jf2 = new JTextField();\/\/ 接收用户输入的文件夹下...
求一个简洁java计算器代码
import java.awt.event.*;import javax.swing.*; \/\/引入所需要的包 class MyFrame extends JFrame implements ActionListener{ private JButton b1,b2,b3,b4,b5,b6;private TextField t1,t2,t3; \/\/定义六个按钮和三个文本域 public MyFrame(String s){ super(s);b1=new JButton("加"...
写一个简单的public class A extends B 的java 文件,并加解释。求高手...
public class B{ \/\/先创建类B \/\/在类B中定义两个变量 static int a=10;static String b="helloworld";} 然后:public class A extends B{ \/\/A继承B,同时获得B中的变量 public static void main(String[] args){ \/\/在A中可以不用定义直接输出B中的两个变量 System.out.println(a)...
如下java代码,结果看不懂,求高手解释
public class SortTest { public static void main(String[] args) { \/\/ 新建一个数组 int[] array = new int[] { 9, 6, 1, 4 };\/\/ 调用方法,对数组进行排序 Sort.swapSort(array);for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " ");} System...
JAVA编程,紧急啊,马上要用,各位高手帮帮忙!!!
刚编好的,在我的机子上没有问题,不知道是不是和你要求的一模一样?自己试试吧。注意"-"是英文输入法的,不要在中文输入下用。import java.util.Scanner;public class TimeSwitch { \/ param args \/ public static void main(String[] args) { Scanner keyboard=new Scanner(System.in);System....