【发布时间】:2016-11-03 09:00:55
【问题描述】:
我有以下问题:
- 我有一个使用 apache mina 框架发送字符串的 Java 客户端。
- 我写了一个C服务器来接收字符串。
- 所以,我尝试向服务器发送一个 utf8 字符:myStringToSend = "Ä"
- 我调试了 mina 源以查看字节或十六进制的数据包,并且 mina 很好地转换了字符串,因此它发送了
0xC30x84。 - 我还在wireshark网络查看器中检查了发送的数据包,它还看到
0xC30x84作为数据包,一切正常。 - 但我的 C 服务器收到以下字节:
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;
}
【问题讨论】:
-
对于
sendbuf[]和recvbuf[],最好使用unsigned char而不是char。