我正在编写客户机/服务器应用程序,其中客户机只需向服务器发送一个字符串,服务器就会重新发送相同的字符串。我的运行方式如下:./server localhost 8000 ./client localhost 8000 StringToSend
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv){
int sd, err, on, len;
int GO = 1;
struct addrinfo hints, *res;
if(argc != 3){
printf("errore\n");
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
fprintf(stderr, "Error bind: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}
if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
perror("Error in socket");
exit(EXIT_FAILURE);
}
on = 1;
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
perror("setsockopt");
exit(EXIT_FAILURE);
}
if (bind(sd, res->ai_addr, res->ai_addrlen) < 0){
perror("Error in bind");
exit(EXIT_FAILURE);
}
if(listen(sd, SOMAXCONN) < 0){
printf("error in listen\n");
}
int ns;
while(GO){
ns = accept(sd, NULL, NULL);
}
char* buff= malloc(100 *sizeof(char));
read(sd, buff, sizeof(buff));
write(sd, buff, sizeof(buff));
}和客户。
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv){
int sd, err, on, len;
struct addrinfo hints, *res;
if(argc != 4){
printf("errore\n");
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
fprintf(stderr, "Error after bind: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}
if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
perror("error when creating a socket");
exit(EXIT_FAILURE);
}
if (connect(sd, res->ai_addr, res->ai_addrlen) == 0){
printf("connected\n");
}
char* buff = malloc(100 * sizeof(char));
buff = argv[3];
write(sd, buff, sizeof(buff));
char* risp = malloc(sizeof(char) * 100);
read(sd, risp, sizeof(risp));
puts(risp);
}所发生的情况是客户端接收一个随机字符串,因为"\0“中存在一些我不知道如何修复的问题,我已经尝试手动将"\0”与strcat连接,但没有发生任何更改。
发布于 2019-11-26 18:02:42
你必须修改内存分配/读/写
当前代码假定最大缓冲区大小为100个字符。然后发出指针大小的读取(4或8个字节)。然后无条件地将4或8个字节写回客户端。
char* buff= malloc(100 *sizeof(char));
read(sd, buff, sizeof(buff));
write(sd, buff, sizeof(buff));您需要以下内容:读取最多100个字符,返回相同数量的字符(包括终止NULL)。
服务器:
int MSGSIZE = 100 ;
char* buff= malloc(MSGSIZE *sizeof(char));
while(GO){
ns = accept(sd, NULL, NULL);
int nread = read(ns, buff, MSGSIZE);
if ( nread > 0 ) write(ns, buff, nread);
}服务器:
int MSGSIZE = 100 ;
char* buff= malloc(MSGSIZE *sizeof(char));
while(GO){
ns = accept(sd, NULL, NULL);
int nread = read(ns, buff, MSGSIZE);
if ( nread > 0 ) write(ns, buff, nread);
}客户端
int MSGSIZE = 100 ;
char* buff= malloc(MSGSIZE *sizeof(char));
strcpy(buff, argv[3]) ;
write(sd, buff, MSGSIZE) ;
char* resp= malloc(MSGSIZE *sizeof(char));
int nread = read(resp, buff, MSGSIZE);
if ( nread > 0 ) puts(resp) ;还有一些其他的问题,你想看看-关闭服务器套接字,释放内存等。我希望让读/写工作将帮助你前进!
https://stackoverflow.com/questions/59055572
复制相似问题