java 和 c 用socket通讯没有任何问题,两边都是采用字节流进行通讯,主要的就是你定义一个两边都可以识别的包的格式即可。
可以用xml进行数据交互用web service 解决跨平台问题
多谢大家的指点啊,听你们的意见我基本上是明白了, 通信协议就是我上面放的那段代码的对象(CNotify )形成的字节流, 按照axman 的说法,是不是只要给对象本身的参数赋一下值,不用考虑该对象的函数,然后以对象流的方式发给服务端就可以了? 比如C对象里定义了 int m_nType; int m_nLen; int m_nBlock; char* m_pData; 这四个参数,我在JAVA里该怎么组织这个流呢?有高手可以给点JAVA的代码吗?
int m_nType;
int m_nLen;
int m_nBlock;
char* m_pData;
和这些参数的GET方法,
这是C的对象,我在JAVA里该怎么组织啊?
=======================
这句话搞不懂了,具我了解socket并不能传递对象, 我估计是服务器端接收到数据(字节流),然后分解,把数据放到一个自己定义好的对象而已. 你并不能从客户端直接发送一个对象给服务器.我的理解是,客户端是什么语言并没关系,关键是你要知道服务器端的通信规则(就是说通信协议),你要在根据这个协议向服务器发送流数据就行了. socket通讯的协议完全可以自己定的,要看服务器端的文档或找相关的开发人员就会知道.
class CNotify
{
public:
enum { DATA_CONNECTION = 0, FRONT_CONNECTION, BACK_CONNECTION };public:
void register_connection (int nType) { m_nType = nType; }public:
enum
{
NOTIFY_LEN = 12
};public:
CNotify():m_nType(0),m_nLen(0),m_nBlock(0),m_pData(NULL){} CNotify(int nType, int nLen, int nBlock = 0)
{
m_nType = nType;
m_nLen = nLen;
m_nBlock = nBlock;
if( nLen > 0 )
{
m_pData = new char[nLen];
}
else
{
m_pData = NULL;
}
} CNotify( const CNotify& cNotify )
{
m_nType = cNotify.m_nType;
m_nLen = cNotify.m_nLen;
m_nBlock = cNotify.m_nBlock;
m_pData = new char[m_nLen];
memcpy( m_pData, cNotify.m_pData, m_nLen );
}
CNotify& operator = (const CNotify& cNotify)
{
if ( m_pData != NULL )
delete[] m_pData;
m_nType = cNotify.m_nType;
m_nLen = cNotify.m_nLen;
m_nBlock = cNotify.m_nBlock;
m_pData = new char[m_nLen];
memcpy( m_pData, cNotify.m_pData, m_nLen );
return *this;
}
~CNotify()
{
if ( m_pData != NULL )
delete[] m_pData;
}public:
int get_type() { return m_nType; }
int get_len() { return m_nLen<0?0:m_nLen; }
int is_block() { return m_nBlock; }
int resize(int nLen)
{
if ( m_pData!=NULL)
delete[] m_pData;
m_pData = new char[nLen];
m_nLen = nLen;
return nLen ;
}
char* get_data() { return m_pData; }
void set_data(char* pData,int nLen)
{
if ( m_pData != NULL )
delete[] m_pData;
m_nLen = nLen ;
m_pData = new char[nLen];
memcpy( m_pData, pData, m_nLen);
}
void set_data(char* pData )
{
if( m_pData == NULL )
{
m_pData = new char[m_nLen];
}
memcpy( m_pData, pData, m_nLen );
}
void set_data( const CBufStruct& clsBufStruct )
{
if( m_pData != NULL )
{
delete[] m_pData;
}
m_nLen = clsBufStruct.m_nRealLen;
m_pData = new char[m_nLen];
memcpy( m_pData, clsBufStruct.m_pData, m_nLen);
}
void n2h()
{
m_nType = ntohl( m_nType );
m_nLen = ntohl( m_nLen );
m_nBlock= ntohl( m_nBlock );
}
void h2n()
{
m_nType = htonl( m_nType );
m_nLen = htonl( m_nLen );
m_nBlock= htonl( m_nBlock );
}
private:
int m_nType;
int m_nLen;
int m_nBlock;
char* m_pData;
};
至少我在JAVA里建立SOCKET服务的时候是可以客户端传递对象,服务器将流强制转换成该对象,
ObjectInputStream oos = new ObjectInputStream(socketclient.getInputStream());
ObjectTest ot;
ot = (ObjectTest)oos.readObject();
System.out.println("服务器返回:"+ot.getValue());
对于对象本身,函数在对象中没有任何信息,也就是对象本身不包括函数的信息,其实都是定义的成员按类型大小排列的.
对于对象本身,函数在对象中没有任何信息,也就是对象本身不包括函数的信息,其实都是定义的成员按类型大小排列的.
通信协议就是我上面放的那段代码的对象(CNotify )形成的字节流,
按照axman 的说法,是不是只要给对象本身的参数赋一下值,不用考虑该对象的函数,然后以对象流的方式发给服务端就可以了?
比如C对象里定义了
int m_nType;
int m_nLen;
int m_nBlock;
char* m_pData;
这四个参数,我在JAVA里该怎么组织这个流呢?有高手可以给点JAVA的代码吗?
服务端是已经封装好的,通信协议就是上面代码的对象(CNotify )
通信协议的结构应该就是对象的字节流吧??客户端以这个对象的字节流结构组织一个流发送给服务端,是不是这个理?另您看一下服务端的解析代码函数是:
CNotify* CSockApi::receive_notify()
{
char szBuf[CNotify::NOTIFY_LEN]; if( readN( szBuf, CNotify::NOTIFY_LEN) != CNotify::NOTIFY_LEN )
{
m_nStatus = STS_ER;
return NULL;
} int* pInt = (int*)szBuf;
int nType = *pInt++;
int nLen = *pInt++;
int nBlock = *pInt;
if ( nLen <= 0 ) return NULL ;
CNotify* pNotify = new CNotify( nType, nLen, nBlock);
if( readN( pNotify->get_data(), nLen) != nLen)
{
delete pNotify;
m_nStatus = STS_ER;
return NULL;
}
m_nStatus = STS_OK;
return pNotify;
}
int CSockApi::readN( char* pBuf, int nBytes)
{
int nLeft = nBytes;
int nRead = 0;
while( nLeft > 0 )
{
nRead = read( m_nSock, pBuf, nLeft);
if( nRead < 0 )
{
if( errno == EINTR )
continue; perror("Read error - little than zero");
perror( strerror( errno));
return nRead; // error, return < 0
}
else if( nRead == 0 )
{
break; // EOF
} nLeft -= nRead;
pBuf += nRead;
}
return ( nBytes - nLeft); // return >= 0
}
int m_nType; (类型)
int m_nLen; (m_pData的长度)
int m_nBlock; (默认为O)
char* m_pData; (实际数据)上面这部分就是通信协议的结构,请问用JAVA来组合这个数据流的话应该怎么写代码呢?
注:服务端是在64位机的IBM AIX系统
下面是JAVA客户端传递流的代码:
Socket sock = new Socket("127.0.0.1", 10000);
sock.setSoTimeout(3000);
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
dos.writeInt(120);
dos.writeInt(8);
dos.writeInt(0);
dos.writeLong(Long.valueOf("1387891****"));
dos.writeChars("\0");
dos.flush();
DataInputStream dis = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
System.out.println("服务器返回:"+dis.read());
dis.close();
dos.close();
sock.close();
http://cuisuqiang.iteye.com/blog/1434416
http://cuisuqiang.iteye.com/blog/1434442
参考一下