gpt4 book ai didi

c - 套接字未收到消息 AF_UNIX

转载 作者:行者123 更新时间:2023-11-30 17:43:46 24 4
gpt4 key购买 nike

       Server Side:
void getMessage(int aSock){
int n=0;
char recvline[1025];
if ( (n = read(aSock, recvline, 1025) ) <0 ) {
perror("Erro: Nao recebida Mensagem do Servidor");

printf("%s",recvline);
}
}


.
.
. ( MAIN FUNCTION with all the sockets declarations etc( Its working )

for(;;){

clilen = sizeof(cli_addr);
mysock=accept(serverSocket,(struct sockaddr *) &cli_addr, &clilen);

if(mysock== -1) {
perror(" Falhou a Ligacao ");
}
else {
if ( (childpid = fork()) < 0 ) {
system ("clear");
printf("\t\tServidor: Erro");
exit(0);
}
else if ( childpid == 0 ) {
close(serverSocket);
getMessage(mysock);
exit(0);

}
}
close(mysock);
}
}
.
.

CLIENT SIDE

void socketSender(int idAcont, int aClientId,char aProdnome,int numRestock){
char buffer[1024];
int tamanho;
sprintf(buffer,"%d %d %c %d",idAcont,aClientId,aProdnome,numRestock);
int sockfd, servlen;
struct sockaddr_un serv_addr;
if ((sockfd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
perror("erro ao criar socket cliente");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, UNIXSTR_PATH);
servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
if (connect(sockfd, (struct sockaddr *) &serv_addr, servlen) < 0)
perror("erro ao fazer connect no cliente");

tamanho=strlen(buffer)+1;
if(write(sockfd,buffer,tamanho)!=tamanho)
perror("Erro ao escrever no cliente");
close(sockfd);
}

服务器端一直在等待 for(;;) 中的消息,它创建了 mysocket,该 mysocket 应该使用 socketsender() 从客户端获取消息,但消息没有弹出:/任何想法出了什么问题吗?预先感谢您

最佳答案

您确定 fork() 离开服务器进程不仅仅只是结束吗?

您的服务器的日志记录代码似乎放错了地方。这不应该是:

if ((n = read(aSock, recvline, 1025) ) <0 ) {
perror("Erro: Nao recebida Mensagem do Servidor");
printf("%s",recvline);
}

是:

if ( (n = read(aSock, recvline, 1025) ) <0 ) {
perror("Erro: Nao recebida Mensagem do Servidor");
}
printf("%s",recvline);

关于c - 套接字未收到消息 AF_UNIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176918/

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