我有以下问题:
0xC3
0x84
。0xC3
0x84
作为数据包,一切正常。FFFFFFC3FFFFFF84
我不知道出了什么问题?
我的C-Server的代码:
#include "stdafx.h"
#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define DEFAULT_BUFLEN 512
#define TOOBIG 100000
int main(int argc, char *argv[])
{
WSADATA wsa;
int i, c, iResult, iSendResult, outputCounter;
SOCKET listenSocket = INVALID_SOCKET, clientSocket = INVALID_SOCKET;
struct sockaddr_in server, client;
char *message;
char sendbuf[DEFAULT_BUFLEN];
unsigned char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != NO_ERROR)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
if((listenSocket = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP )) == INVALID_SOCKET) //IPv4, TCP
{
printf("Could not create socket : %d" , WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket created.\n");
memset(&server, '0', sizeof(server));
memset(sendbuf, '0', sizeof(sendbuf));
server.sin_family = AF_INET;
//server.sin_addr.s_addr = htonl(0x7F000001); //localhost - 127.0.0.1
server.sin_addr.s_addr = inet_addr("127.0.0.1");
//server.sin_addr.s_addr = inet_addr("10.48.53.166");
server.sin_port = htons(7368);
if( bind(listenSocket ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
puts("Bind done");
listen(listenSocket, 10);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
message = "{\"id\":2,\"result\":{\"code\":\"999\",\"message\":\"AddPageRange StartIndex don't match Inspection Counter\"},\"Client_ID\":\"PQCS1\",\"jsonrpc\":\"2.0\",\"Protocol\":\"2H\"}";
while( (clientSocket = accept(listenSocket , (struct sockaddr *)NULL, NULL)) != INVALID_SOCKET )
{
puts("Connection accepted");
do {
iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{
printf("Bytes received: %d\n", iResult);
recvbuf[iResult] = '\0';
outputCounter = 0;
while(recvbuf[outputCounter] != '\0')
printf("%X", (unsigned char)recvbuf[outputCounter++]);
iSendResult = send( clientSocket, message, strlen(message), 0 );
if (iSendResult == SOCKET_ERROR)
{
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while( iResult > 0 );
}
if (clientSocket == INVALID_SOCKET)
{
printf("accept failed with error code : %d" , WSAGetLastError());
return 1;
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
答案 0 :(得分:1)
您的recvbuf是char类型,可疑是在您的平台上签名。消息的两个字节都是&gt; 127(十进制)等被视为负面。 printf(“%X”)格式说明符需要一个整数(平台上的32位),所以数字得到符号扩展(用字节的最高位填充整数的最高24位)
尝试打印出您收到的字节数。同时将printf更改为
printf("%X", ((int)recvbuf[outputCounter++]) & 0xff);
答案 1 :(得分:0)
printf
出现问题。
查看The Man
o,u,x,X
unsigned int参数转换为无符号八进制(o),无符号十进制(u)或无符号十六进制(x和X)表示法。字母abcdef用于x次转换;字母ABCDEF用于X转换。精度(如果有)给出必须出现的最小位数;如果转换后的值需要较少的数字,则在左侧用零填充。默认精度为1.当使用显式精度0打印0时,输出为空。
强调我的
因此,使用%X
作为格式说明符,您的数据会被提升为unsigned int
,您只需选择要打印的字节即可传递数据:
printf("Byte %d: %X\n", outputCounter, recvbuf[outputCounter++] & 0xFF);