[英]Segmentation fault using the buffer recived from recv() tcpServer c
我正在尝试在c中创建应用程序客户端/服务器,但是在recv()之后,当我尝试使用接收到的缓冲区时,程序给出了分段错误(创建了核心转储),我无法解决。 这是我在服务器端的代码:
int req_socket_id;
int comunication_socket_id;
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t client_add_size;
char buffer[255];
//char mess[1024];
int i, n;
//int index;
unsigned int num;
// AF_INET = famiglia di indirizzi iPv4
req_socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(req_socket_id<0)
{
printf("Socket initialization failed !!");
return -1;
}
e
memset(&server_add, 0, sizeof(server_add)); // azzeramento struttura
server_add.sin_family = AF_INET; // dominio indirizzi IP
server_add.sin_addr.s_addr = 0; // indirizzo IP
server_add.sin_port = htons(23165); // numero di porta UDP
if(bind(req_socket_id, (struct sockaddr*) &server_add, sizeof(server_add)) < 0)
{
perror("\nErrore associazione porta e socket!\n");
close(req_socket_id);
return -1;
}
if(listen(req_socket_id, 1)<0)
{
perror("\nErrore nell'ascolto!\n");
close(req_socket_id);
return -1;
}
while(1)
{
client_add_size = sizeof(client_add);
comunication_socket_id = accept(req_socket_id, (struct sockaddr*) &client_add, &client_add_size);
if(comunication_socket_id>=0)
{
//index = 0;
char tmp[100];
while(1)
{
printf("\nOk");
//n = recv(comunication_socket_id, (char*) buffer, sizeof(buffer)+1, 0);
//n = recv(comunication_socket_id, (void*) buffer, sizeof(buffer)+1, 0);
n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);
printf("\nReceved! n: %d", n);
printf("\nReceved: %s", buffer);
if(strcmp(buffer, "end")==0)
{
close(comunication_socket_id);
printf("\n...socket closed");
return -1;
}
[...]
这是客户端代码:
unsigned long start, now;
unsigned int *num = (unsigned int*)buffer;
int i, n;
TCPclient_send(buffer, strlen(buffer)+1);
printf("\nSent: |%s|\n", buffer);
start = clock(); // tempo iniziale attesa
now = clock(); // tempo attuale
while ((now - start) < TIMEOUT)
{
if ((n = TCPclient_receive(&buffer[i], sizeof(buffer)-i)) > 0)
{
i += n;
if (i >= sizeof(unsigned int))
{
// risposta completa
printf("Receved number %u.\r\n", ntohl(*num));
TCPclient_disconnect();
return 0;
}
}
now = clock();
}
printf("No answer receved!\r\n");
TCPclient_disconnect();
控制台服务器端输出:
davide@davide-VirtualBox:~/Documenti/RubricaTCP$ ./TCPserver
Ok
Receved! n: 11
Errore di segmentazione (core dump creato) //-->segmentation fault, end of process
控制台客户端输出:
Insert the command (end to close): SET=A;A;A;
Sent: |SET=A;A;A;|
No answer receved!
PS:这是我第一个使用套接字的应用程序,因此我可能犯了一些愚蠢的错误。 我在许多主题中寻找答案,但没有找到任何可行的方法。 非常感谢你
n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);
sizeof(buffer)+1
本质上是不正确的。 它应该是sizeof(buffer)
。 您正在使用不存在的内存。
printf("\nReceved! n: %d", n);
好。
printf("\nReceved: %s", buffer);
错误。 这应该是
printf("\nReceved: %.*s", n, buffer);
然后:
if(strcmp(buffer, "end")==0)
这也是错误的。 无法保证您会收到一个以空值终止的字符串或完整的命令。 这是一个字节流协议。 如果需要消息,则必须自己实施。 对于没有读取或接收循环的问题,编写网络代码或任何I / O代码很少是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.