gpt4 book ai didi

java - C 服务器与 Java 客户端套接字连接。在客户端上阅读消息时,我看到实际消息之前有空格

转载 作者:行者123 更新时间:2023-11-30 16:12:02 24 4
gpt4 key购买 nike

我有一个 C 语言的服务器和一个 Java 语言的客户端(TCP 连接)。连接它们后,我从客户端发送第一条消息,然后从服务器发送响应,此时一切正常(客户端在控制台中写入收到的字符串),当我向服务器发送另一条消息并且服务器给出响应时,这响应被放置在第一个字符未知的长字符串的末尾。例如在客户端我看到:

Send a message: type and press Enter key
HELLO
Server: HI
HELLO AGAIN
Server:
HELLO HELLO
Server:

服务器端的输出正常(客户端和服务器的消息都可以看到)

这是我的服务器:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 10011

int main(){

int socketfd, ret;
struct sockaddr_in serverAddr;

int newSocket;
struct sockaddr_in newAddr;

socklen_t addr_size;

char buffer[1024];
pid_t childpid;

socketfd = socket(AF_INET,SOCK_STREAM,0);

if(socketfd < 0){
printf("\n error in socket creation");
return -1;
}
printf("\n Server socket is created\n");

memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

ret = bind(socketfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

if(ret < 0){
printf("Error in binding\n");
return -1;
}
printf("[*]Bind to port %d\n", PORT);

if(listen(socketfd, 10) == 0){
printf("Listening...\n");
}else{
printf("Error in binding\n");
}

newSocket = accept(socketfd, (struct sockaddr*)&newAddr, &addr_size);
if( newSocket < 0){
printf("No socket\n");
exit(1);
}
int size = 1024;
char buff[size];
char sbuff[size];
int n;
int reader;
memset(buff, 0, size);
memset(sbuff, 0, size);
// infinite loop for receiving and sending
for (;;) {
// read the message from client and copy it in buffer
reader = recv(newSocket, buff, 1024 * sizeof(char), 0);
if (reader == -1) {
perror("recv()");
break;
} else if (reader == 0) {
break;
} else {
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
bzero(buff, size);

n = 0;
// copy server message in the buffer
while ((sbuff[n++] = getchar()) != '\n');

// and send that buffer to client
write(newSocket, sbuff, sizeof(sbuff));
bzero(sbuff,size);



}

}
close(newSocket);

return 0;
}

这是我的客户:

import java.io.*;
import java.net.*;
public class ClientJava
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 10011);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream), 1024);

System.out.println("Send a message: type and press Enter key");

String receiveMessage, sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data

// for(int i=0; i<1024;i++){
// String s=receiveRead.read();
// receiveMessage[i]=s;
// if(s == ">") {
// return i;
// }
// }
// System.out.println(receiveMessage);


receiveMessage = receiveRead;
if((receiveMessage) != "0") //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
//removeNonAscii(receiveMessage);
//replaceUnreadable(receiveMessage);
receiveMessage = receiveMessage.substring(0,0);


}

}
private static String removeNonAscii(String s){
StringBuffer sb = new StringBuffer();
for(int i=0; i<s.length(); ++i){
if(s.charAt(i) < 128){
sb.append(s.charAt(i));
}
}
return sb.toString();
}
private static String replaceUnreadable(String s){
String clean = s.replaceAll("\\P{Print}", "");
return clean;
}
}

调试后发现,当第二次接收消息时,recieveMessage充满了空字符,并且实际消息到达了字符串的末尾(因此在控制台中看不到) )。如何清理字符串或将消息放在其开头?谢谢。

最佳答案

已解决:我用了这个方法

private static String replaceUnreadable(String s){
String clean = s.replaceAll("\\P{Print}", "");
return clean;
}

成功了

关于java - C 服务器与 Java 客户端套接字连接。在客户端上阅读消息时,我看到实际消息之前有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com