我有一個將JSON字符串發送到Java客戶端的C++套接字服務器。對於Java部分我用下面的代碼:Java Socket Read在第二次讀取時返回奇怪字符
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
while((inString = in.readLine()) != null) {
Log.i("JSON", inString);
C++代碼:
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { return false; }
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { return false; }
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(13377);
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { return false; }
listen(s, 3);
c = sizeof(struct sockaddr_in);
new_socket = accept(s, (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET) { return false; }
while (listenSocket) {
if(...){
char sendData[] = "hallo";
send(new_socket, sendData, sizeof(sendData), NULL);
}
當接收到第一次一切都按計劃進行接收。但是,第二次它只打印出正方形「 」中的問號。那是因爲我發送了一個2048個字符的char數組,它可能只包含比這個數量更少的數字,或者這個問題會是什麼?
請同時提供您的C++代碼。 https://stackoverflow.com/help/mcve – Tezirg