【问题标题】:server recieving some junk value from the client?服务器从客户端收到一些垃圾值?
【发布时间】:2014-01-27 11:12:36
【问题描述】:
#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;
}

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

【问题讨论】:

    标签: c sockets operating-system ip


    【解决方案1】:

    这是因为,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);
    }
    

    【讨论】:

      【解决方案2】:

      '\0' 是唯一与 char 数组和字符串不同的字符。

      由于您使用 %s 打印字符串,因此需要在末尾添加 '\0' 字符。

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-31
        • 2017-12-29
        • 1970-01-01
        相关资源
        最近更新 更多