Client stub Vs Client socket & Server stub Vs Server socket

我与影子孤独终老i 提交于 2019-12-11 14:13:31

问题


Background

Any client socket program(C) over TCP/IP looks like,

 /* Socket creation */
 sockfd = socket(AF_INET, SOCK_STREAM, 0);

 /* Do nothing for dynamic address assignment to that client socket */

 /* Identify the server, we use to send a request for connection */
   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(8000);
   inet_pton(AF_INET, serv_ip, &servaddr.sin_addr);

 /* Connect request to listening socket of server */ 
   ret_val = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

 /* 
    *
       Communication code
       ================== 
       Here comes the code for application layer protocol using read() & write() call that follow FTP, HTTP, SMTP specific rules.
    *
    */

Any server socket program(C) over TCP/IP looks like,

  /* Create listen socket */
    listfd = socket(AF_INET, SOCK_STREAM, 0);


  /* Assign protocol family(AF_INET) & address to that socket */
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(8004);
    retval = bind(listfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

  /* Enable the communication on that socket */
    listen(listfd, 5);

  /* with a specific server model(iterative/threaded/multiprocess/..) accept 
 client request */
     connfd = accept(listfd, (struct sockaddr *) &cliaddr, &clilen);

  /* 
     *
       Communication code
       ==================
       Here comes the code for application layer protocol using read() & write() call that follow FTP, HTTP, SMTP specific rules.
     *
     */

Early host-to-host protocols focussed on human-to-computer communications Ex: Email 1971, FTP and interoperable Telnet: 1973

There was interest in app-to-app protocol - RFC 707 that describes the manner in which a networked procedure call to be done.

RPC is the transfer of a data structure from client side stub to server side stub via libnsl.so.1, in ansi C world(say). Data structure could hold a message for add operation(say), as shown below,

 -------------
| proc: "add" |
 -------------
| int: val(i) |
 -------------
| int: val(j) |
 -------------

Typical flow of an RPC for a remote add(i, j) operation,

Client & server can differ in data representations(Big endian & Little endian). The external data representation (add_xdr.c) is an data abstraction needed for machine independent communication.


  1. Is RPC, a machine independent communication?

  2. In RPC world, Does packet handling code between client stub & server stub behave similar to client socket & server socket? with the difference in communication code carrying a data structure that holds procedure information add(i, j)


回答1:


RPC is machine-independent, in that 32-bit and 64-bit systems can communicate, it's not as interoperable as pure sockets between different OSes, as the two sides have to agree on a lot more details.

RPC works over transports between endpoints, some of which are not even representible as sockets (local memory as an example).

You can read about RPC at https://msdn.microsoft.com/en-us/library/windows/desktop/aa373935(v=vs.85).aspx

RPC code does look quite different from socket code in that RPCs in the calling code look like any other function, it is only in those (usually tool-generated) functions that the data is bundled for transport.



来源:https://stackoverflow.com/questions/48030186/client-stub-vs-client-socket-server-stub-vs-server-socket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!