无聊写的 一个Soeckt 客户端例子

	/**
	 * 发送请求
	 * @param ip
	 * @param port
	 * @param msg
	 * @throws Exception
	 */
	private String  sendMsg(String ip, int port, String vreqxml) throws Exception{    
		String result="";
		Socket socket=null;
		DataOutputStream dos=null;
		 DataInputStream dis=null;
		try{
			InetSocketAddress it = new InetSocketAddress(ip,port);
	        //InetSocketAddress it = new InetSocketAddress("172.16.xxxx.xxx",6688);
	        socket=new Socket();
	        logger.info("---sendMsg.timeout="+timeout);
	        socket.connect(it,timeout * 1000);
	        socket.setSoTimeout(timeout * 1000);
	             
	        DigitSignature ds=new DigitSignature("C:\\icbc\\");
	        byte[] data_sign= ds.genValiInfo(vreqxml.getBytes("utf-8") );
	        dos=new DataOutputStream(socket.getOutputStream());
	        
	 
	        byte[] data=new byte[vreqxml.getBytes("utf-8").length+data_sign.length];
	        System.arraycopy(vreqxml.getBytes("utf-8"),0,data,0,vreqxml.getBytes("utf-8").length);
	        System.arraycopy(data_sign,0,data,vreqxml.getBytes("utf-8").length,data_sign.length);
	        logger.info("data="+ HexTool.bytesToHexString(data)  );
	        logger.info("data_sign="+ HexTool.bytesToHexString(data_sign)  );

	        int length=data.length;
	        logger.info("length="+data.length);
	        dos.writeInt(length);
	        //dos.write(int2byte(length),0,4);
	        dos.write(data, 0, data.length);
	        dos.flush();
	       
	        while (true) {    
	        	dis=new DataInputStream(socket.getInputStream());
	        	int rsplength=dis.readInt();
	        	
	        	logger.info("rsplength="+rsplength);
	            byte rspbyte[]=new byte[rsplength];
	            byte content[]=new byte[rsplength-128];
	            dis.read(rspbyte);
	            logger.info("content-->"+content.length);
	            System.arraycopy(rspbyte,0,content,0,rsplength-128);
	            result=new String(content,"utf-8");
	            logger.info("rsp="+result);
	            break;
	        }
	        dis.close();
	        dos.close();
	        
		}catch(Exception e){
			e.printStackTrace();
			dis.close();
			dos.close();
			socket.close();
			 
			throw new Exception ("请求出现异常:"+e.toString());
			
		}finally{
			if(dis!=null)dis.close();
			if(dos!=null)dos.close();
			if(socket!=null)socket.close();
			
		}
		return result;

    }



简单例子
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一个简单的电子宠物系统Java代码示例,仅供参考: ``` import java.util.Scanner; public class VirtualPet { private String name; private int hunger; private int thirst; private int boredom; public VirtualPet(String name) { this.name = name; this.hunger = 50; this.thirst = 50; this.boredom = 50; } public void feed() { hunger -= 10; thirst += 5; boredom += 5; } public void water() { thirst -= 10; boredom += 5; } public void play() { boredom -= 10; hunger += 5; thirst += 5; } public void tick() { hunger += 5; thirst += 5; boredom += 5; } public String getName() { return name; } public int getHunger() { return hunger; } public int getThirst() { return thirst; } public int getBoredom() { return boredom; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to Virtual Pet!"); System.out.print("Enter pet name: "); String petName = scanner.nextLine(); VirtualPet pet = new VirtualPet(petName); while (true) { System.out.println("\n" + pet.getName() + " status:"); System.out.println("Hunger: " + pet.getHunger()); System.out.println("Thirst: " + pet.getThirst()); System.out.println("Boredom: " + pet.getBoredom()); System.out.println("\nWhat do you want to do?"); System.out.println("1. Feed " + pet.getName()); System.out.println("2. Water " + pet.getName()); System.out.println("3. Play with " + pet.getName()); System.out.println("4. Do nothing"); int choice = scanner.nextInt(); switch (choice) { case 1: pet.feed(); break; case 2: pet.water(); break; case 3: pet.play(); break; default: break; } pet.tick(); } } } ``` 这个电子宠物系统有三个需要关注的状态:饥饿、口渴和无聊。您可以通过输入不同的数字来决定喂食、给水或者玩耍。每个操作都会对状态有不同的影响,同时系统也会定期自动增加状态值,如果任何一个状态达到了100,你的电子宠物就会死亡。 这只是一个简单的示例,您可以根据自己的需要来修改和扩展代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值