Java套接字到C套接字

Java套接字到C套接字,java,sockets,Java,Sockets,有谁能帮我把这段Java代码翻译成C语言吗?我尝试了很多不同的方法,但都没有成功。我在缓冲区部分遇到了问题,我不知道如何存储数据,然后使用C套接字发送数据 SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1", 6633)); ByteBuffer buf = ByteBuffer.allocate(48); buf.clear();

有谁能帮我把这段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);
谢谢。

下面是代码:

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

它没有经过测试,还可以在需要的地方添加错误检查。

这是否与您正在寻找的类似: