c - 服务器从客户端接收到一些垃圾值?

标签 c sockets operating-system ip

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>


#pragma once
#pragma comment (lib, "ws2_32.lib")
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

#include <winsock.h>
#include <io.h>




SOCKET sock;
SOCKET fd;
char recv_data[10];
int port = 18001;

void CreateSocket()
{
   struct sockaddr_in server, client;  // creating a socket address structure: structure contains ip address and port number


    printf("Initializing Winsock\n");
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD (1, 1);
    if (WSAStartup (wVersionRequested, &wsaData) != 0){
        printf("Winsock initialised failed \n");
    } else {
        printf("Initialised\n");
    }


    // create socket
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0)    {
        printf("Could not Create Socket\n");
        //return 0;
    }

    printf("Socket Created\n");


    // create socket address of the server
    memset( &server, 0, sizeof(server));
    // IPv4 - connection
    server.sin_family = AF_INET;
    // accept connections from any ip adress
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    // set port
    server.sin_port = htons(port);



    //Binding between the socket and ip address
    if(bind (sock, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        printf("Bind failed with error code: %d", WSAGetLastError());
    }

    //Listen to incoming connections
    if(listen(sock,3) == -1){
        printf("Listen failed with error code: %d", WSAGetLastError());
    }

    printf("Server has been successfully set up - Waiting for incoming connections");
    int len;


        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*) &client, &len);
        if (fd < 0){
            printf("Accept failed");
        }
        //echo(fd);
        printf("\n Process incoming connection from (%s , %d)", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
        //closesocket(fd);



}


int main()
{

    CreateSocket();
    while(1)
    {
        if(fd == -1)
        {
            printf("socket error\n");

        }
        else
        {
            recv(fd, recv_data, 9, 0);

            printf("value is %s", recv_data);


        }


    }


    return 0;
}

上面是服务器代码:我正在创建一个套接字并接受来自客户端的数据。客户端正在发送数据,服务器正在接受数据。 如果客户端向服务器发送 a,那么服务器将在其中添加一些垃圾字符。如果客户端发送 4 个字符,那么它将收到所有这四个字符。如果客户端发送一两个字符:为什么服务器收到一些垃圾值??

最佳答案

这是因为,recv 不会在字符串末尾附加 NULL 字符。您必须显式添加 NULL 字符。因此,使用 recv 调用的返回值并使用它来附加 NULL 字符。

int retval;


retval = recv(fd, recv_data, 9, 0);

if(retval != SOCKET_ERROR) {
    recv_data[retval] = '\0';
    printf("value is %s", recv_data);
}

关于c - 服务器从客户端接收到一些垃圾值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379360/

相关文章:

c - 警告 : format ‘%llx’ expects argument of type ‘long long unsigned int *’ , 但参数 3 的类型为 ‘off64_t *’ [-Wformat]

c - 一次增加两个索引

c++ - 为 C++ 中的应用程序最佳使用而打开的并行套接字/TCP 连接数

c - 使用 O_APPEND 打开文件后写入文件时出错 | O_CREATE

c++ - switch语句有问题

连接拒绝连接到 1234 以外的任何端口

php - 如何为 set socks 5 或 php stream_socket_client 的 http 代理制作上下文数组

java - 接收整数序列的服务器

c++ - 测量内存的潜伏期

assembly - 如何关闭机器?我正在构建自己的微型操作系统