java c++ socket通讯

26 篇文章 1 订阅

一般是java端为server,c++客户端与其通信,server端是mina,jetty等。

这个一般用protobuf,在此不多讲。

下面介绍c++为server端,java客户端与其通信。

以结构体为例说明:

struct Employee {
      char name[20];
      int id;
      float salary;
};

java client 测试源码(为说明问题,假设struct字节对齐,sizeof(Employee)=28):

import java.net.*; 

/** 
* 与C语言通信(java做Client,c/c++做Server,传送一个结构) 
* @author kingfish 
* @version 1.0 
*/ 
class Employee { 
private byte[] buf = new byte[28]; //为说明问题,定死大小,事件中可以灵活处理 

/** 
* 将int转为低字节在前,高字节在后的byte数组 
*/ 
private static byte[] toLH(int n) { 
	byte[] b = new byte[4]; 
	b[0] = (byte) (n & 0xff); 
	b[1] = (byte) (n >> 8 & 0xff); 
	b[2] = (byte) (n >> 16 & 0xff); 
	b[3] = (byte) (n >> 24 & 0xff); 
	return b; 
} 

/** 
* 将float转为低字节在前,高字节在后的byte数组 
*/ 
private static byte[] toLH(float f) { 
	return toLH(Float.floatToRawIntBits(f)); 
} 

/** 
* 构造并转换 
*/ 
public Employee(String name, int id, float salary) { 
	byte[] temp = name.getBytes(); 
	System.arraycopy(temp, 0, buf, 0, temp.length); 

	temp = toLH(id); 
	System.arraycopy(temp, 0, buf, 20, temp.length); 

	temp = toLH(salary); 
	System.arraycopy(temp, 0, buf, 24, temp.length); 
} 

/** 
* 返回要发送的数组 
*/ 
public byte[] getBuf() { 
	return buf; 
} 

/** 
* 发送测试 
*/ 
public static void main(String[] args) { 
	try { 
		Socket sock = new Socket("127.0.0.1", 8888); 
		sock.getOutputStream().write(new Employee("kingfish", 123456789, 8888.99f). 
		getBuf()); 
		sock.close(); 
	} 
	catch (Exception e) { 
		e.printStackTrace(); 
	} 
} //end 


 

c++ server端:

typedef struct STRU_RESULT
{
	int id;
	int result;
}STRU_RESULT;

	
 
	SOCKET sockUDP = socket(AF_INET,SOCK_DGRAM,0);

struct sockaddr_in sin;   sin.sin_family = AF_INET;   sin.sin_addr.s_addr = INADDR_ANY;    sin.sin_port = htons(JWYG_UDPBROADCASTTCPPORT);   if ( bind(sockUDP,(struct sockaddr *)&sin,sizeof(struct sockaddr_in)) == SOCKET_ERROR)   {    CCLOG("UDP bind failed!");

   return;   }

  struct sockaddr_in addrServer;   int lenAddrServer = sizeof(struct sockaddr_in);   int recvLen = sizeof(JWYG_STRU_MsgHead) + sizeof(JWYG_STRU_ServerIPPort);   char *pBufferRecv = new char[recvLen+1];

 
	int nRet = recvfrom( sockUDP, pBufferRecv, recvLen, 0,(struct sockaddr *)&addrServer,	(socklen_t *)&lenAddrServer);
	if (nRet == sizeof(STRU_RESULT))
	{
		STRU_RESULT resultX;
		memcpy((char *)&resultX,pBufferRecv,sizeof(STRU_RESULT));

		if (resultX.id == 1)  // resultX.id: 1,支付成功 0,支付失败 -1,取消支付
		{
			CCLog("----------------> chargeMoney success :%d---------",resultX.result);
		}else if (resultX.id == -1){
			CCLog("----------------> chargeMoney cancled! resultX.id :% ---------",resultX.id);
		}else if (resultX.id == 0){
			CCLog("----------------> chargeMoney failed ! resultX.id :%d---------",resultX.id);
		}			
	}

 轻量级,不用加一堆protobuf库之类的了,此处是UDP,tcp与此类似,打完收工

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值