如何将字符串发送到服务器并将其回显?
How to send a string to the server and echo it back?
我正在创建一个向服务器询问当前时间的程序。我想在请求中发送我的名字,并让服务器随时间回显我的名字。我在哪里输入我的名字?
一段客户端代码
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);
/* Send time request to server */
addrlen = sizeof(struct sockaddr_in);
sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);
printf("Request sent to server\n");
/* Receive time request from server */
recvfrom(sockfd, (char *) ¤t_time, (int) sizeof(current_time), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);
/* Print the time received from the server */
printf("\n The time received from the server:%s\n", ctime(¤t_time));
exit(0);
服务器代码片段
/** Message **/
printf("Server is listening on port: %d\n", PORT);
printf("Waiting for client request...\n");
printf("Press CTRL + C to exit\n");
while(1) {
addrlen = sizeof(struct sockaddr_in);
recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);
current_time = time(NULL);
sendto(sockfd, (char *) ¤t_time, (int) sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);
}
exit(0);
您可以将时间放在缓冲区中,在名称之后。
服务器:
printf("Server is listening on port: %d\n", PORT);
printf("Waiting for client request...\n");
printf("Press CTRL + C to exit\n");
while(1) {
addrlen = sizeof(struct sockaddr_in);
recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);
current_time = time(NULL);
memcpy(buffer + strlen(buffer) + 1, ¤t_time, sizeof(current_time));
sendto(sockfd, buffer, strlen(buffer) + 1 + sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);
}
exit(0);
客户端将名称放入缓冲区:
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);
/* Send time request to server */
addrlen = sizeof(struct sockaddr_in);
strcpy(buffer, name);
sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);
printf("Request sent to server\n");
/* Receive time request from server */
recvfrom(sockfd, (char *) buffer, sizeof(buffer), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);
/* Print the name received from the server */
printf("\n The name received from the server:%s\n", buffer);
memcpy(current_time, buffer + strlen(buffer) + 1, sizeof(current_time));
/* Print the time received from the server */
printf("\n The time received from the server:%s\n", ctime(¤t_time));
exit(0);
我正在创建一个向服务器询问当前时间的程序。我想在请求中发送我的名字,并让服务器随时间回显我的名字。我在哪里输入我的名字?
一段客户端代码
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);
/* Send time request to server */
addrlen = sizeof(struct sockaddr_in);
sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);
printf("Request sent to server\n");
/* Receive time request from server */
recvfrom(sockfd, (char *) ¤t_time, (int) sizeof(current_time), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);
/* Print the time received from the server */
printf("\n The time received from the server:%s\n", ctime(¤t_time));
exit(0);
服务器代码片段
/** Message **/
printf("Server is listening on port: %d\n", PORT);
printf("Waiting for client request...\n");
printf("Press CTRL + C to exit\n");
while(1) {
addrlen = sizeof(struct sockaddr_in);
recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);
current_time = time(NULL);
sendto(sockfd, (char *) ¤t_time, (int) sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);
}
exit(0);
您可以将时间放在缓冲区中,在名称之后。
服务器:
printf("Server is listening on port: %d\n", PORT);
printf("Waiting for client request...\n");
printf("Press CTRL + C to exit\n");
while(1) {
addrlen = sizeof(struct sockaddr_in);
recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);
current_time = time(NULL);
memcpy(buffer + strlen(buffer) + 1, ¤t_time, sizeof(current_time));
sendto(sockfd, buffer, strlen(buffer) + 1 + sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);
}
exit(0);
客户端将名称放入缓冲区:
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);
/* Send time request to server */
addrlen = sizeof(struct sockaddr_in);
strcpy(buffer, name);
sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);
printf("Request sent to server\n");
/* Receive time request from server */
recvfrom(sockfd, (char *) buffer, sizeof(buffer), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);
/* Print the name received from the server */
printf("\n The name received from the server:%s\n", buffer);
memcpy(current_time, buffer + strlen(buffer) + 1, sizeof(current_time));
/* Print the time received from the server */
printf("\n The time received from the server:%s\n", ctime(¤t_time));
exit(0);