Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要在服务器上接收由客户端发送的消息包。。。。在C中 /*internet域中使用TCP的简单服务器 端口号作为参数传递*/ #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效错误(常量字符*消息) { 佩罗尔(味精); 出口(1); } int main(int argc,char*argv[]) { int sockfd、newsockfd、端口号; socklen_t clilen; 字符缓冲区[256]; 服务地址中的结构sockaddr\u,cli\u addr; int n; 如果(argc_C_Sockets_Tcp - Fatal编程技术网

要在服务器上接收由客户端发送的消息包。。。。在C中 /*internet域中使用TCP的简单服务器 端口号作为参数传递*/ #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效错误(常量字符*消息) { 佩罗尔(味精); 出口(1); } int main(int argc,char*argv[]) { int sockfd、newsockfd、端口号; socklen_t clilen; 字符缓冲区[256]; 服务地址中的结构sockaddr\u,cli\u addr; int n; 如果(argc

要在服务器上接收由客户端发送的消息包。。。。在C中 /*internet域中使用TCP的简单服务器 端口号作为参数传递*/ #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效错误(常量字符*消息) { 佩罗尔(味精); 出口(1); } int main(int argc,char*argv[]) { int sockfd、newsockfd、端口号; socklen_t clilen; 字符缓冲区[256]; 服务地址中的结构sockaddr\u,cli\u addr; int n; 如果(argc,c,sockets,tcp,C,Sockets,Tcp,//我是新手,所以请忽略愚蠢的提问方式 此代码用于获取n/w上的简单字符串。现在我想得到一个预定义的标准消息,用于发送和接收,为此我必须定义一个结构 现在我的问题是如何在C语言中发送和获取n/w上的结构。请帮帮我。 strong text好吧,看看write prototype: /* A simple server in the internet domain using TCP The port number is passed as an argument */ #

//我是新手,所以请忽略愚蠢的提问方式

此代码用于获取n/w上的简单字符串。现在我想得到一个预定义的标准消息,用于发送和接收,为此我必须定义一个结构

现在我的问题是如何在C语言中发送和获取n/w上的结构。请帮帮我。
strong text

好吧,看看write prototype:

    /* A simple server in the internet domain using TCP
   The port number is passed as an argument */
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include <unistd.h>
     #include <sys/types.h> 
     #include <sys/socket.h>
     #include <netinet/in.h>



void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(newsockfd,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockfd);
     return 0; 
}










======>>>now client code


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}
第二个参数是一个通用的void指针,因此可以发送任何需要的内容。把你想要传输的结构的地址和它的大小放在那里

还有需要考虑的endian(小endian与大endian)。如果正在通信的机器具有不同的终止性,则此方法不起作用。看看那些像HTON,NTOH

如果要发送的数据结构使用机器字节顺序存储基元类型,则发送整个结构不起作用。您应按网络顺序存储每个值,例如:

ssize_t write(int fd, const void *buf, size_t count);

我认为值得指出的是,使用这种方法,您有责任确保连接两端的结构填充是相同的。如果服务器和客户端使用不同的编译器和/或操作系统,则可能会出现此问题。同样,endianness可能也需要注意。@aix:i修复了我的答案。请检查一下,如果发现任何错误,请告诉我。
struct foo{
   uint32_t a;
   uint16_t b;
}foo; 

foo f;
f.a = htonl(10);
f.b = htons(5);

write(fd, (void *)&f, sizeof(foo));