我需要帮助我必须从PHP传递给C的代码。 该代码允许通过SOCKET向远程设备发送指令。 它在PHP中非常适合我,但我必须将它转换为C.一切都很好,除了PHP的pack()函数,我没有看到如何在C中替换它。 基本上我必须通过这个:
$message = pack("H2H8H2H4H4", "a5", "00000001", "5e", "0000", "bc19");
mesaje_in_c =¿?
非常感谢你能给我的支持,谢谢!
这是PHP代码:
<?php
$host = "192.168.0.140";
$port = 8081;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
print "socket_create() falló: razón: " . socket_strerror(socket_last_error()) ."\n";
} else {
print "create socket OK.\n";
}
socket_set_option(
$socket,
SOL_SOCKET, // socket level
SO_RCVTIMEO, // timeout option
array(
"sec"=>10, // Timeout in seconds
"usec"=>0 // I assume timeout in microseconds
)
);
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
if ($result === false) {
print "socket_connect() falló.\nRazón: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
print "connect to server OK.\n";
}
$message = pack("H2H8H2H4H4", "a5", "00000001", "5e", "0000", "bc19");
echo "Message To server Bin: ".$message."\n";
echo "Message dec: ";
print_r(unpack("H*", $message))."\n";
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read($socket, 1024) or die("Could not read server response\n");
print "Reply From Server Bin:".$result."\n";
$resultHex = unpack("H*", $result);
print "Reply From Server Hex:";
print_r($resultHex)."\n";
// close socket
socket_close($socket);
?>
这是要完成的C代码:
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[]){
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message , server_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("192.168.0.140");
server.sin_family = AF_INET;
server.sin_port = htons(8081);
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
//Send some data
message = "a5000000015e0000bc19";
// ???? message = pack("H2H8H2H4H4", "a5", "00000001", "5e", "0000", "bc19");
if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
system("pause");
return 1;
}
puts("Data Send\n");
//Receive a reply from the server
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}
puts("Reply received\n");
//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
puts(server_reply);
system("pause");
return 0;
}
非常感谢你能给我的支持,谢谢!
答案 0 :(得分:0)
有效吗?
char message[] = "\xa5\x00\x00\x00\x01\x5e\x00\x00\xbc\x19";