c-将客户端循环到服务器



我想用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 acceptedn");
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: %dn", 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: %sn", server_response);

//generate random numbers
int random,stock;
char test[256];
srand(time(NULL));
random = rand() %4;
printf("client random: %dn", 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 stockn", test);
}else{
printf("%s item out of stockn", test);
}

//close the socket
close(network_socket);

return 0;

}

您可以将您的"生成随机数&发送到服务器&接收响应";在for循环中!

相关内容

  • 没有找到相关文章

最新更新