package com.example.sharingcontact;
import java.io.*;
import java.net.Socket;
public class testjava {
/**
* @param args
* @throws EOFException
*/
public static void main(String[] args) {
// TODO Auto-generated method
//temp[] test=query("123","user05");
//System.out.println(test[0].tel);
/*
contact[] test=new contact[10];
for(int i=0;i<10;i++){
test[i]=new contact();
test[i].name="user"+i;
test[i].nickName=null;
test[i].tel="147258369";
test[i].fixedTel=null;
test[i].company=null;
test[i].address=null;
test[i].contactId=i;
}
sync("1345021",test);
*/
System.out.println(register("1345021","123456",3));
}
//share 1 for private,2 for public,3 for "just logon"
//tel for telephone number
/*
* return:
* "Logon OK" for logon success.
* "Password incorrect" for password incorrect.
* "No account" for account not exist when logging.
* "Register success" for new account register success.
*/
public static String register(String tel,String pw,int share){
String getMsg;
try{
Socket socket=new Socket("109.131.14.239",10086);
OutputStream os=socket.getOutputStream();
PrintWriter sos=new PrintWriter(os);
DataOutputStream dos=new DataOutputStream(os);
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
dos.writeInt(1);
dos.flush();
sos.println(tel);
sos.println(pw);
sos.flush();
dos.writeInt(share);
dos.flush();
getMsg=is.readLine();
os.close();sos.close();dos.close();is.close();socket.close();
}catch(Exception e){
getMsg="Connection ERROR";
}
return getMsg;
}
//insert or update contact
//utel for user's telephone number
//contact for sync
public static String sync(String utel,contact[] contact){
String getMsg;
if(contact==null)
return null;
int count=contact.length;
try{
Socket socket=new Socket("109.131.14.239",10086);
OutputStream os=socket.getOutputStream();
PrintWriter sos=new PrintWriter(os);
DataOutputStream dos=new DataOutputStream(os);
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//set the symbol and length
dos.writeInt(2);
dos.flush();
dos.writeInt(count);
dos.flush();
//transmission
sos.println(utel);
sos.flush();
for(int i=0;i<count;i++){
sos.println(contact[i].name);
sos.println(contact[i].nickName);
sos.println(contact[i].tel);
sos.println(contact[i].fixedTel);
sos.println(contact[i].company);
sos.println(contact[i].address);
sos.flush();
dos.writeInt(contact[i].contactId);
dos.flush();
}
getMsg=is.readLine();
os.close();sos.close();dos.close();is.close();socket.close();
}catch(Exception e){
getMsg="Connection ERROR";
}
return getMsg;
}
//delete contact
//utel for user's tel,contactId for contactid
public static String deleteContact(String utel,int contactId){
String getMsg;
try{
Socket socket=new Socket("109.131.14.239",10086);
OutputStream os=socket.getOutputStream();
PrintWriter sos=new PrintWriter(os);
DataOutputStream dos=new DataOutputStream(os);
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
dos.writeInt(3);
dos.flush();
sos.println(utel);
sos.flush();
dos.writeInt(contactId);
getMsg=is.readLine();
os.close();sos.close();dos.close();is.close();socket.close();
}catch(Exception e){
getMsg="Connection ERROR";
}
return getMsg;
}
//utel for user's telephone number
public static contact[] getContact(String utel){
int count;
contact[] getArray=null;
try{
Socket socket=new Socket("109.131.14.239",10086);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
PrintWriter sos=new PrintWriter(os);
DataOutputStream dos=new DataOutputStream(os);
DataInputStream dis=new DataInputStream(is);
dos.writeInt(4);
dos.flush();
sos.println(utel);
sos.flush();
count=dis.readInt();
getArray=new contact[count];
for(int i=0;i<count;i++){
getArray[i]=new contact();
getArray[i].name=dis.readLine();
getArray[i].nickName=dis.readLine();
getArray[i].tel=dis.readLine();
getArray[i].fixedTel=dis.readLine();
getArray[i].company=dis.readLine();
getArray[i].address=dis.readLine();
getArray[i].contactId=dis.readInt();
}
os.close();is.close();sos.close();dos.close();dis.close();socket.close();
}catch(Exception e){
System.out.println("ERROR "+e);
}
return getArray;
}
//search online
//utel for user's telephone number
//name for the user's name being searched
public static temp[] query(String utel,String sname){
int count;
temp[] result=null;
try{
Socket socket=new Socket("109.131.14.239",10086);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
PrintWriter sos=new PrintWriter(os);
DataOutputStream dos=new DataOutputStream(os);
DataInputStream dis=new DataInputStream(is);
dos.writeInt(5);
dos.flush();
sos.println(utel);
sos.println(sname);
sos.flush();
count=dis.readInt();
result=new temp[count];
//receive the result
for(int i=0;i<count;i++){
result[i]=new temp();
result[i].name=dis.readLine();
result[i].tel=dis.readLine();
result[i].count=dis.readInt();
}
dis.close();dos.close();sos.close();is.close();os.close();socket.close();
}catch(Exception e){
System.out.println("Connection ERROR"+e);
}
return result;
}
}
class contact{
public String name;
public String nickName;
public String tel;
public String fixedTel;
public String company;
public String address;
public int contactId;
}
class temp{
//public String utel;
public String name;
public String tel;
public int count;
}
7
最新推荐文章于 2025-11-27 11:05:17 发布
1万+

被折叠的 条评论
为什么被折叠?



