/* IP socket address structure */ structsocketaddr_in { uint16_t sin_family; /* Protocol family (always AF_INET) */ uint16_t sin_port; /* Port number in network byte order */ structin_addrsin_addr;/* IP address in network byte order */ unsignedchar sin_zero[8]; /* Pad to sizeof(struct sockaddr) */ }
/* Generic socket address structure (for connect, bind, and accept) */ structsockaddr { uint16_t sa_family; /* Protocol family */ char sa_data[14]; /* Address data */ }
/* client.c */ voidmain(int argc, char *argv[]) { structsockaddr_insad;/* structure to hold an IP address */ int clientSocket; /* socket descriptor */ structhostent *ptrh;/* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128];
/* determine the server's address */ memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host);
/* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
gets(Sentence); /* Get input stream from user */ addr_len = sizeof(struct sockaddr); n = sendto(clientSocket, Sentence, strlen(Sentence)+1, (struct sockaddr *) &sad, addr_len); /* Send line to server */
n = recvfrom(clientSocket, modifiedSentence, sizeof(modifiedSentence), (struct sockaddr *) &sad, &addr_len); /* Read line from server */
printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); /* Close connection */ }
/* server.c */ voidmain(int argc, char *argv[]) { structsockaddr_insad;/* structure to hold an IP address */ structsockaddr_incad; int serverSocket; /* socket descriptor */ structhostent *ptrh;/* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128];
port = atoi(argv[1]);
/* Create welcoming socket at port & Bind a local address */ serverSocket = socket(PF_INET, SOCK_DGRAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port); /* set the port number */ bind(serverSocket, (struct sockaddr *)&sad, sizeof(sad));
while(1) { n = recvfrom(serverSocket, clientSentence, sizeof(clientSentence), 0 (struct sockaddr *) &cad, &addr_len); /* Receive messages from clients */
/* capitalize Sentence and store the result in capitalizedSentence*/
n = sendto(serverSocket , capitalizedSentence, strlen(capitalizedSentence)+1, (struct sockaddr *) &cad, &addr_len); /* Write out the result to socket */ } /* End of while loop, loop back and wait for another client connection */ }
/* client.c */ /* 1. 建立 socket 2. 隐式捆绑 socket 3. 连接 socket 4. 写和读 5. 关闭 socket */ voidmain(int argc, char *argv[]) { structsockaddr_insad;/* structure to hold an IP address of server */ int clientSocket; /* socket descriptor */ structhostent *ptrh;/* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128]; host = argv[1]; port = atoi(argv[2]); /* 服务器的主机域名与端口号 */ /* Create client socket, connect to server */ clientSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host); /* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); /* 将IP地址拷贝到sad.sin_addr */ connect(clientSocket, (struct sockaddr *)&sad, sizeof(sad));
gets(Sentence); /* Get input stream from user */
n = write(clientSocket, Sentence, strlen(Sentence)+1); /* Send line to server */
n = read(clientSocket, modifiedSentence, sizeof(modifiedSentence)); /* Read line from server */ printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); /* Close connection */ }
/* server.c */ /* 1. 建立 socket 2. 绑定 socket 3. 等待并建立连接 socket 4. 读和写 5. 关闭 (connection)socket */ voidmain(int argc, char *argv[]) { structsockaddr_insad;/* structure to hold an IP address of server*/ structsockaddr_incad;/* client */ int welcomeSocket, connectionSocket; /* socket descriptor */ structhostent *ptrh;/* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128]; port = atoi(argv[1]); /* Create welcoming socket at port & Bind a local address */ welcomeSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port); /* set the port number */ bind(welcomeSocket, (struct sockaddr *)&sad, sizeof(sad)); /* Specify the maximum number of clients that can be queued */ listen(welcomeSocket, 10)
while(1) { connectionSocket = accept(welcomeSocket, (struct sockaddr *)&cad, &alen); /* Wait, on welcoming socket for contact by a client */ n = read(connectionSocket, clientSentence, sizeof(clientSentence));
/* capitalize Sentence and store the result in capitalizedSentence*/
n = write(connectionSocket, capitalizedSentence, strlen(capitalizedSentence)+1); /* Write out the result to socket */ close(connectionSocket); /* End of while loop, loop back and wait for another client connection */ }