有人能帮我把这段Java代码翻译成C语言吗?我试过很多不同的方法,但都没有成功。我在缓冲区部分有问题,我不知道如何存储数据,然后使用C套接字发送它。
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("127.0.0.1", 6633));
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
byte version = 0x01;
short length = 8;
byte type = 0;
buf.put(version);
buf.put(type);
buf.putShort(length);
buf.putInt(12356);
buf.flip();
socketChannel.write(buf);谢谢。
发布于 2014-03-28 17:11:44
代码如下:
SOCKET sock = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in thataddr;
thataddr.sin_addr.s_addr = inet_addr("127.0.0.1");
thataddr.sin_family = AF_INET;
thataddr.sin_port = htons(6633);
connect(sock, (LPSOCKADDR) &thataddr, sizeof(thataddr));
typedef struct SendThis
{
unsigned char version;
unsigned int length;
unsigned char type;
};
SendThis sendThis;
sendThis.version = '1';
sendThis.length = 8;
sendThis.type = 0;
send(sock,(char *)&sendThis,sizeof(SendThis),0);它没有经过测试,还可以在需要的地方添加错误检查。
https://stackoverflow.com/questions/21322537
复制相似问题