import java.io.*;
import java.io.Serializable;
import java.net.*;
import java.util.*;
import java.util.Vector;
//客户类
class client implements Serializable{
String name,telephonenumber,address;
public client(String a, String b, String c){
name=a;telephonenumber=b;address=c;
}
public boolean equals(Object o)
{
if ((o instanceof client)&&((client)o).name.equals(this.name) &&((client)o).telephonenumber.equals(this.telephonenumber)&&((client)o).address.equals(this.address))
{
return true;
}
else return false; }
}
//服务器类
public class Socketserver {
public static void main(String[] args){
File f =new File("J:\\xmj.txt");
int size=0;
Vector vc=new Vector();
StringBuffer data = new StringBuffer();
String recv="";
String read="";
ObjectInputStream objectin=null;
ServerSocket ss=null;
try{
if(!f.exists())
{
f.createNewFile();
}
objectin=new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
vc=(Vector)objectin.readObject();
objectin.close();
}catch(Exception e){
}
try{
ss=new ServerSocket(3000);
}catch(Exception e){
System.out.println("can't receive the require:"+e.toString());
}
try{
Socket s =ss.accept(); //接受来自客户端的请求
System.out.println("connect sucess!");
//创建获得套接字S的输入输出流
BufferedReader sis = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter sos = new PrintWriter(s.getOutputStream());
recv = sis.readLine(); //接收来自客户的数据
while(!recv.equals("end")){
//insert the client's message 插入客户信息
if(recv.equals("insert"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
System.out.println("the name of client is "+a+'\n'+"the telephonenumber of client is "+b+'\n'+"the address of client is "+c);
client T=new client(a,b,c);
boolean have=vc.contains(T);
if(have){
sos.println("the message has exist!");
sos.flush();
}
else
{
vc.add(T);
sos.println("success!");
sos.flush();
}
recv=sis.readLine();
}
//delete the client's message 删除客户信息
else if(recv.equals("delete"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
client T=new client(a,b,c);
if(vc.removeElement(T))
System.out.println("delete success!");
sos.println("successfull!");
sos.flush();
recv=sis.readLine();
}
//modify the client's message 修改客户信息
else if(recv.equals("modify"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
client T=new client(a,b,c);
int i=vc.indexOf(T,0);
sos.println("modify the name");
sos.flush();
String d=sis.readLine();
sos.println("modify the telephonenumber");
sos.flush();
String e=sis.readLine();
sos.println("modify the address");
sos.flush();
String h=sis.readLine();
client TT=new client(d,e,h);
vc.set(i,TT);
client TTT=(client)vc.get(i);
System.out.println("the modify name,telephonenumber and address is "+TTT.name+""+TTT.telephonenumber+""+TTT.address);
sos.println("modify successfull");
sos.flush();
recv=sis.readLine();
}
//consult the client's message 查询客户信息
else if(recv.equals("consult"))
{int i=0;
int k=0;
sos.println("insert the name");
sos.flush();
String a=sis.readLine();
for(i=0;i<vc.size();i++)
{
client pp=(client)vc.get(i);
if(pp.name.equals(a))
k=i;
}
client po=(client)vc.get(k);
sos.println("the telephonenumber and address is "+po.telephonenumber+""+po.address);
sos.flush();
recv=sis.readLine();
}
}
if(recv.equals("end"))
sos.println("end");
sos.flush();
try{
ObjectOutputStream objectout=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
objectout.writeObject(vc);
objectout.close();
}catch(Exception e){
}
sos.close();
sis.close();
ss.close();
s.close();
}catch(Exception e){
System.out.println("Exception:"+e.toString());
}
}
}
import java.io.Serializable;
import java.net.*;
import java.util.*;
import java.util.Vector;
//客户类
class client implements Serializable{
String name,telephonenumber,address;
public client(String a, String b, String c){
name=a;telephonenumber=b;address=c;
}
public boolean equals(Object o)
{
if ((o instanceof client)&&((client)o).name.equals(this.name) &&((client)o).telephonenumber.equals(this.telephonenumber)&&((client)o).address.equals(this.address))
{
return true;
}
else return false; }
}
//服务器类
public class Socketserver {
public static void main(String[] args){
File f =new File("J:\\xmj.txt");
int size=0;
Vector vc=new Vector();
StringBuffer data = new StringBuffer();
String recv="";
String read="";
ObjectInputStream objectin=null;
ServerSocket ss=null;
try{
if(!f.exists())
{
f.createNewFile();
}
objectin=new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
vc=(Vector)objectin.readObject();
objectin.close();
}catch(Exception e){
}
try{
ss=new ServerSocket(3000);
}catch(Exception e){
System.out.println("can't receive the require:"+e.toString());
}
try{
Socket s =ss.accept(); //接受来自客户端的请求
System.out.println("connect sucess!");
//创建获得套接字S的输入输出流
BufferedReader sis = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter sos = new PrintWriter(s.getOutputStream());
recv = sis.readLine(); //接收来自客户的数据
while(!recv.equals("end")){
//insert the client's message 插入客户信息
if(recv.equals("insert"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
System.out.println("the name of client is "+a+'\n'+"the telephonenumber of client is "+b+'\n'+"the address of client is "+c);
client T=new client(a,b,c);
boolean have=vc.contains(T);
if(have){
sos.println("the message has exist!");
sos.flush();
}
else
{
vc.add(T);
sos.println("success!");
sos.flush();
}
recv=sis.readLine();
}
//delete the client's message 删除客户信息
else if(recv.equals("delete"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
client T=new client(a,b,c);
if(vc.removeElement(T))
System.out.println("delete success!");
sos.println("successfull!");
sos.flush();
recv=sis.readLine();
}
//modify the client's message 修改客户信息
else if(recv.equals("modify"))
{
sos.println("insert the name");
sos.flush();
String a= sis.readLine();
sos.println("insert the telephonenumber");
sos.flush();
String b=sis.readLine();
sos.println("insert the address");
sos.flush();
String c=sis.readLine();
client T=new client(a,b,c);
int i=vc.indexOf(T,0);
sos.println("modify the name");
sos.flush();
String d=sis.readLine();
sos.println("modify the telephonenumber");
sos.flush();
String e=sis.readLine();
sos.println("modify the address");
sos.flush();
String h=sis.readLine();
client TT=new client(d,e,h);
vc.set(i,TT);
client TTT=(client)vc.get(i);
System.out.println("the modify name,telephonenumber and address is "+TTT.name+""+TTT.telephonenumber+""+TTT.address);
sos.println("modify successfull");
sos.flush();
recv=sis.readLine();
}
//consult the client's message 查询客户信息
else if(recv.equals("consult"))
{int i=0;
int k=0;
sos.println("insert the name");
sos.flush();
String a=sis.readLine();
for(i=0;i<vc.size();i++)
{
client pp=(client)vc.get(i);
if(pp.name.equals(a))
k=i;
}
client po=(client)vc.get(k);
sos.println("the telephonenumber and address is "+po.telephonenumber+""+po.address);
sos.flush();
recv=sis.readLine();
}
}
if(recv.equals("end"))
sos.println("end");
sos.flush();
try{
ObjectOutputStream objectout=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
objectout.writeObject(vc);
objectout.close();
}catch(Exception e){
}
sos.close();
sis.close();
ss.close();
s.close();
}catch(Exception e){
System.out.println("Exception:"+e.toString());
}
}
}
import java.io.*;
import java.net.*;
public class Socketclient {
public static void main(String[] args){
Socket cs;
String data="";
String recvdata="";
try{
cs=new Socket("127.0.0.1",3000);
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
BufferedReader cis=new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter cos=new PrintWriter(cs.getOutputStream());
System.out.println("please choose:insert/delete/modify/consult");
do{
data=is.readLine();
cos.println(data);
cos.flush();
recvdata=cis.readLine();
System.out.println(recvdata);
}
while(!recvdata.equals("end"));
is.close();
cis.close();
cos.close();
cs.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}
从 vector 里检索信息的时候 应该用你在 client类中实现的equals方法,
你在modify的时候好像并没有执行成功