gpt4 book ai didi

c - 循环客户端到服务器

转载 作者:行者123 更新时间:2023-12-04 19:07:22 24 4
gpt4 key购买 nike

我想用 ubuntu c 制作一个简单的服务器客户端。我想要实现的是服务器将成为“eshop”。如您所见,它有 4 种产品。客户端将自动生成随机数(0 到 3),将它们发送到服务器和服务器,检查可用性并响应客户端。到目前为止一切顺利,但我想给客户打电话 5 次。每次客户订购 10 件产品。我该如何接近它?
服务器

#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>


struct items{
char *description;
int price;
int item_count;
};



int main(){




//items
struct items catalog[20];
catalog[0].item_count = 2;
catalog[0].description = "psigio";
catalog[0].price = 100;
catalog[1].item_count = 2;
catalog[1].description = "thleorash";
catalog[1].price = 150;
catalog[2].item_count = 2;
catalog[2].description = "kinito";
catalog[2].price = 120;
catalog[3].item_count = 2;
catalog[3].description = "pc";
catalog[3].price = 300;

char server_message[256] = "you have reached the server!";
char item_name[256];

// create the server socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);

printf("pending connection...");

//define the server address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;

//bind the socket to our specified IP and port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));

listen(server_socket, 0);


int client_socket;
client_socket = accept(server_socket, NULL, NULL);
printf("Connection accepted\n");
printf("Listening...\n");

//send message to client
send(client_socket, server_message, sizeof(server_message), 0);

//recieve item and check
int ran, stock;
recv(client_socket, &ran, sizeof(ran), 0);
printf("Server random: %d\n", ran);
if (catalog[ran].item_count>0){
catalog[ran].item_count--;
stock = 1;
}else{
stock =0;
}
send(client_socket, &stock, sizeof(stock), 0);
send(client_socket, catalog[ran].description, sizeof(catalog[ran].description), 0);

//close the socket
close(server_socket);
return 0;
}
客户
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>

int main(){

//create a socket
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);

//specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;

int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
//check for errors in connection
if (connection_status == -1) {
perror("Connection error: \n");
}sleep(1);


//recieve data from the server
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);

//print the server's response
printf("the server send the data: %s\n", server_response);

//generate random numbers
int random,stock;
char test[256];

srand(time(NULL));
random = rand() %4;
printf("client random: %d\n", random);
send(network_socket, &random, sizeof(random), 0);

//recieve stock of item
recv(network_socket, &stock, sizeof(stock), 0);
recv(network_socket, &test, sizeof(test), 0);
if (stock==1){
printf("%s in stock\n", test);
}else{
printf("%s item out of stock\n", test);
}

//close the socket
close(network_socket);

return 0;



}

最佳答案

您可以将“生成随机数并发送到服务器并接收响应”放在 for 中。环形 !

关于c - 循环客户端到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65667677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com