7

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值