如何初始化缓冲区

时间:2018-04-30 19:39:03

标签: c sockets buffer

这是我的服务器/客户端套接字代码。 我有缓冲区问题,当客户端收到来自服务器的消息时,缓冲区(我将其初始化为" 12")输出显示如下:

[+]Client Socket is created.
[+]Connected to Server.
Temp : 
  12121212121212121212121212121212121212121212121212121212121212121212121212121212 
  12121212121212�L��Q� 

服务器:

#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 4444

int main(){

int sockfd, ret;
 struct sockaddr_in serverAddr;

int clientSocket;
struct sockaddr_in newAddr;

socklen_t addr_size;

char buffer[1024];
    char Temp[4]= "12";
pid_t childpid;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
    printf("[-]Error in connection.\n");
    exit(1);
}
printf("[+]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(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if(ret < 0){
    printf("[-]Error in binding.\n");
    exit(1);
}
printf("[+]Bind to port %d\n", 4444);

if(listen(sockfd, 10) == 0){
    printf("[+]Listening....\n");
}else{
    printf("[-]Error in binding.\n");
}


while(1){
    clientSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
    if(clientSocket < 0){
        exit(1);
    }
    printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), 
   ntohs(newAddr.sin_port));

    if((childpid = fork()) == 0){
        close(sockfd);

        while(1){



        send(clientSocket, Temp, strlen(Temp), 0);
                bzero(buffer, sizeof(buffer));
            if(strcmp(Temp, ":exit") == 0){
                printf("Disconnected from %s:%d\n", 
 inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
                break;
        }
    }

 }



}
return 0;
}

客户:

#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 4444

int main(){

int clientSocket, ret;
struct sockaddr_in serverAddr;
char buffer[1024];
    char Temp[4];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if(clientSocket < 0){
    printf("[-]Error in connection.\n");
    exit(1);
}
printf("[+]Client 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 = connect(clientSocket, (struct sockaddr*)&serverAddr, 
sizeof(serverAddr));
if(ret < 0){
    printf("[-]Error in connection.\n");
    exit(1);
}
printf("[+]Connected to Server.\n");
   bzero(buffer,512);

   int words = 0;
    char c;

while(1){


             recv(clientSocket, Temp, 1024, 0);
    if(recv(clientSocket, Temp, 1024, 0) < 0){
        printf("[-]Error in receiving data.\n");
    }
           printf("Temp : %s \n",Temp);
       if(strcmp(Temp, ":exit") == 0){
        close(clientSocket);
        printf("[-]Disconnected from server.\n");

    } exit(1);
}



return 0;
}

拜托,任何人都可以告诉我如何解决这个问题,任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我发现您的客户端代码有几个问题:

char Temp[4];
...
recv(clientSocket, Temp, 1024, 0);

尽管只为Temp分配了4个字节,但您在这里读取的最多1024个字节为Temp。如果读取超过4个字节,则其他数据结构将填充读取数据,即经典缓冲区溢出。

printf("Temp : %s \n",Temp);

这里假设Temp是一个\0终止的字符串,它不是。因此,它会打印出数据,从&Temp的位置开始,直到\0字节,它会在某处找到(根据您的输出在Temp之外的方式)。

if(strcmp(Temp, ":exit") == 0){

即使Temp只有4个字节,此语句也假设至少6个字节(5个字节的字符串和\0用于字符串标记的结尾)。

服务器代码也存在类似的问题。同样奇怪的是,您的buffer变量已初始化,但从未使用过。我的猜测是你最初想用这个而不是Temp来发送和接收。