Caching
 

キャッシング - ソケットコード分析

次のコードでは、いくつかは完全に、実際には、いくつかのAPIは完全に理解していない理解していないクライアント側とサーバー側、として使用している限りに分割されて見つけるために、インターネットの一例であり、すべてを理解することができます当実際にどれだけのエネルギーが、この技術は、最終的に製品が彼の収入をもたらす、トラフィックをもたらし、サイトにユーザーを持って来る必要がある場合は、製品の需要を満たすために使用されていることを、完全に重要ではない理解していないが、それは問題を知ることは重要です問題は、することができます。
gbvy[W
int socket(int domain, int type, int protocol);
通常、家族、PF_INETので使用されるプロトコルで指定されたドメインは言った、インターネットプロトコル群(TCP / IPプロトコルスイート);型パラメータは、ソケットのタイプを指定します:SOCK_STREAMまたはSOCK_DGRAMの、ソケットインターフェイスも定義されて元のソケット(SOCK_RAWである)、アプリケーション低レベルアグリーメントを使用して、プロトコルは通常、"0"を割り当てる。socket()の呼び出しが返される整数のソケット記述子は、呼び出しの後に使用することができます。
。。。
次の5種類の情報間のネットワーク接続を含む2つのネットワークプログラム:通信プロトコルは、ローカルのプロトコルアドレスは、ローカルホストのポート、リモートアドレスとリモートホストポートプロトコル。socketデータ構造体は5つの情報が含まれています。




debian:/home/server# cat server.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#SERVPORT 3333ポート番号*リスニング/ *サーバを定義/
#バックログは10の同時接続要求*の/ *の最大数を定義/
main()
{




my_addrをsockaddr_in構造体* /ローカルのアドレス情報* /
sockaddr_in構造REMOTE_ADDRの構造体/ *クライアントのアドレス情報* /
//int socket(int domain, int type, int protocol);
/ /ソケット機能概要:
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);

my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
//int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
//
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
perrorは("バインドエラー!");
exit(1);
}
//int listen(int sockfd, int backlog);
//
if (listen(sockfd, BACKLOG) == -1) {
perrorは("エラーを聞いてくれ!");
exit(1);
}
while(1) {
int sin_size = sizeof(struct sockaddr_in);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
//
if ((client_fd = accept(sockfd, (struct sockaddr *) &remote_addr, &sin_size)) == -1) {

continue;
}
printf( "received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
//ssize_t send(int sockfd, const void *buf, size_t len, int flags);
//
場合(!のfork()){/ *子コード* /
if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1)
perrorは("エラーを送信します!");
close(client_fd);
exit(0);
}
close(client_fd);
}
}

/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};
Bの構造が/ include / netinet /内部in.h / usrに定義されているin_addr形式構造体
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
/ include /にstdint.h、実際には、unsigned int型(符号なし整数変数という)は、/ usrで定義されてUint32_t変数
typedef unsigned int uint32_t;
2.client.c解析
debian:/home/server# cat client.c
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 3333

main(int argc, char *argv[]){
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"Please enter the server's hostname!\n");
exit(1);
}
//int gethostname(char *name, size_t len);
//
if ((host = gethostbyname(argv[1])) == NULL) {

exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){

exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host -> h_addr);
bzero(&(serv_addr.sin_zero), 8);
//
//int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
if (connect(sockfd, (struct sockaddr *) &serv_addr, \
sizeof(struct sockaddr)) == -1) {
perrorは("接続エラー");
exit(1);
}
//ssize_t recv(int sockfd, void *buf, size_t len, int flags);
//
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perrorは("recvをエラー!");
exit(1);
}
buf[recvbytes] = '\0';
printf("Received: %s", buf);
close(sockfd);
}

/* Description of data base entry for a single host. */
struct hostent
{
char *h_name; /* Official name of host. */
char **h_aliases; /* Alias list. */
int h_addrtype; /* Host address type. */
int h_length; /* Length of address. */
char **h_addr_list; /* List of addresses from name server. */
#define h_addr h_addr_list[0] /* Address, for backward compatibility. */
};
さらに読み:
私は、関連するログを持っている:

| C言語、入出力機能の一般的な用法
| C言語は、externの使用方法
| PHPの使用例と静的にC