C语言写一个股票服务器代码
时间: 2024-05-01 10:23:22 浏览: 90
股票服务器的实现需要涉及到网络编程和多线程编程,下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
struct stock {
char name[10];
float price;
};
struct stock stocks[] = {
{"AAPL", 123.45},
{"GOOG", 678.90},
{"MSFT", 111.22},
{"AMZN", 999.99},
{"FB", 234.56}
};
void *handle_client(void *arg) {
int client_sock = *(int*)arg;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
while (1) {
int bytes_received = recv(client_sock, buffer, BUFFER_SIZE, 0);
if (bytes_received < 0) {
printf("Error receiving data from client\n");
break;
} else if (bytes_received == 0) {
printf("Client disconnected\n");
break;
}
// Parse the client request and send the stock price
char *symbol = strtok(buffer, " ");
if (symbol == NULL) {
printf("Invalid request from client\n");
break;
}
int i;
for (i = 0; i < sizeof(stocks)/sizeof(stocks[0]); i++) {
if (strcmp(stocks[i].name, symbol) == 0) {
char response[BUFFER_SIZE];
snprintf(response, BUFFER_SIZE, "The price of %s is %.2f", stocks[i].name, stocks[i].price);
send(client_sock, response, strlen(response), 0);
break;
}
}
if (i == sizeof(stocks)/sizeof(stocks[0])) {
char response[BUFFER_SIZE];
snprintf(response, BUFFER_SIZE, "Invalid symbol: %s", symbol);
send(client_sock, response, strlen(response), 0);
}
}
close(client_sock);
pthread_exit(NULL);
}
int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
pthread_t tid[MAX_CLIENTS];
int i = 0;
// Create the server socket
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
printf("Error creating server socket\n");
exit(1);
}
// Bind the server socket to a port
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(8000);
if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("Error binding server socket to port\n");
exit(1);
}
// Listen for incoming connections
if (listen(server_sock, MAX_CLIENTS) < 0) {
printf("Error listening for incoming connections\n");
exit(1);
}
printf("Server started, listening on port 8000\n");
// Accept incoming connections and handle them in separate threads
while (1) {
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_len);
if (client_sock < 0) {
printf("Error accepting incoming connection\n");
continue;
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
if (pthread_create(&tid[i], NULL, handle_client, &client_sock) != 0) {
printf("Error creating thread\n");
continue;
}
i++;
if (i == MAX_CLIENTS) {
i = 0;
while (i < MAX_CLIENTS) {
pthread_join(tid[i], NULL);
i++;
}
i = 0;
}
}
close(server_sock);
return 0;
}
```
该代码创建了一个股票服务器,监听在8000端口,接收来自客户端的请求,并返回相应的股票价格。当有新的客户端连接时,将在单独的线程中进行处理,以便同时服务多个客户端。可以通过telnet或其他客户端程序连接到该服务器并发送请求,以测试其功能。
阅读全文