gpt4 book ai didi

c - 通过 C 语言的套接字编程处理多个客户端

转载 作者:行者123 更新时间:2023-11-30 16:38:37 24 4
gpt4 key购买 nike

我目前正在用 C 语言开发一个服务器(基本上是一个允许写入数据库的后台服务),它从两个不同的 python 客户端以字符串形式接收数据流,而客户端又使用分隔符来分隔数据流。从字符串生成标记并将它们写入各自/不同的 xml 文件。代码部分可能看起来很长,但它是一个非常简单的程序,所以请耐心等待并尝试提供帮助,因为我是客户端-服务器架构的新手,并且希望了解所有这些复杂性。提前致谢。

我的 C 程序:

#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>



int main(int argc , char *argv[])
{
int socket_desc , new_socket , c, valread;
struct sockaddr_in server, client ;
char *message;
char buffer[1024] = {0};
char *hello = "Hello from server";
int opt = 1;
char *string;
const char s[2] = ";";
char *token;
char *sid;
char *name;
char *host;
char *port;
char *proto;
char *user;
char *password;
char *key;
char *companyName;
char *officeAddress;
char *state;
char *country;
char *ladmin;
char *phone;
char *mobile;
char *email;
char *designation;
char *rmanager;
char *sip;
char *polname;
char *device;
char *status;
char *srczone;
char *dstzone;
char *srcaddr;
char *dstaddr;
char *srcuser;
char *app;
char *service;
char *urlcategory;
char *action;






//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == 0)
{
printf("Could not create socket");
}


if (setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}


//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );



//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}


puts("bind done");




//Listen
listen(socket_desc , 3);
while(1){
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
}

puts("Connection accepted\n");




valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
string = buffer;

//Macro that I am using to distinguish between clients and the work to be done
token = strtok(string,s);

//If server is passed as the first token, following code to be executed
printf("%s", token);
if( token == "server" ){
sid = strtok(NULL, s);
printf("\nSERVER-ID: %s\n", sid);
name = strtok(NULL, s);
printf("\nName: %s\n", name);
host = strtok(NULL,s);
printf("\nHost: %s\n", host);
port = strtok(NULL,s);
printf("\nPort: %s\n", port);
proto = strtok(NULL,s);
printf("\nProto: %s\n", proto);
user = strtok(NULL,s);
printf("\nUser: %s\n", user);
password = strtok(NULL, s);
printf("\nPassword: %s\n", password);
key = strtok(NULL,s);
printf("\nKey: %s\n", key);
companyName = strtok(NULL,s);
printf("\nCompanyName: %s\n", companyName);
officeAddress = strtok(NULL,s);
printf("\nOfficeAddress: %s\n", officeAddress);
state = strtok(NULL,s);
printf("\nState: %s\n", state);
country = strtok(NULL,s);
printf("\nCountry: %s\n", country);
email = strtok(NULL,s);
printf("/nEmail: %s\n", email);
ladmin = strtok(NULL,s);
printf("\nLAdmin: %s\n", ladmin);
phone = strtok(NULL,s);
printf("\nPhone: %s\n", phone);
mobile = strtok(NULL,s);
printf("\nMobile: %s\n", mobile);
designation = strtok(NULL, s);
printf("\nDesignation: %s\n", designation);
rmanager = strtok(NULL, s);
printf("\nReportingManager: %s\n", rmanager);

FILE *xml_file;
xml_file = fopen("/var/www/db/db_server_list.xml", "a+");
fseek(xml_file, -8, SEEK_END);

ftruncate(fileno(xml_file),ftello(xml_file));





fprintf(xml_file, "\n<server id='%s'>\n", sid);
fprintf(xml_file, " <name>%s</name>\n", name);
fprintf(xml_file, " <host>%s</host>\n", host);
fprintf(xml_file, " <port>%s</port>\n", port);
fprintf(xml_file, " <proto>%s</proto>\n", proto);
fprintf(xml_file, " <user>%s</user>\n", user);
fprintf(xml_file, " <password>%s</password>\n", password);
fprintf(xml_file, " <key>%s</key>\n", key);
fprintf(xml_file, " <companyName>%s</companyName>\n", companyName);
fprintf(xml_file, " <address>\n");
fprintf(xml_file, " <officeAddr>%s</officeAddr>\n", officeAddress);
fprintf(xml_file, " <state>%s</state>\n", state);
fprintf(xml_file, " <country>%s</country>\n", country);
fprintf(xml_file, " </address>\n");
fprintf(xml_file, " <contacts>\n");
fprintf(xml_file, " <ladmin>%s</ladmin>\n", ladmin);
fprintf(xml_file, " <phone>%s</phone>\n", phone);
fprintf(xml_file, " <mobile>%s</mobile>\n", mobile);
fprintf(xml_file, " <email>%s</email>\n", email);
fprintf(xml_file, " <designation>%s</designation>\n", designation);
fprintf(xml_file, " <rManager>%s</rManager>\n", rmanager);
fprintf(xml_file, " </contacts>\n");
fprintf(xml_file, " </server>\n", key);
fprintf(xml_file, "</data>");

fclose(xml_file);
send(new_socket, hello, 17, 0);
printf("\nSocket: Sent data to the client!\n");
}
else if (token == "policy"){
sip = strtok(NULL, s);
printf("\nserverip: %s\n", sip);
polname = strtok(NULL, s);
printf("\nPolicy Name: %s\n", polname);
device = strtok(NULL,s);
printf("\nDevice Name: %s\n", device);
status = strtok(NULL,s);
printf("\nStatus: %s\n", status);
srczone = strtok(NULL,s);
printf("\nSrcZone: %s\n", srczone);
dstzone = strtok(NULL,s);
printf("\nDstZone: %s\n", dstzone);
srcaddr = strtok(NULL, s);
printf("\nSrcAddr: %s\n", srcAddr);
dstaddr = strtok(NULL,s);
printf("\nDstAddr: %s\n", dstAddr);
srcuser = strtok(NULL,s);
printf("\nSrcUser: %s\n", srcuser);
app = strtok(NULL,s);
printf("\nApplication: %s\n", app);
service = strtok(NULL,s);
printf("\nService: %s\n", service);
urlcategory = strtok(NULL,s);
printf("\nUrlCategory: %s\n", urlcategory);
action = strtok(NULL,s);
printf("/nAction: %s\n", action);

FILE *xml_file;
xml_file = fopen("/var/www/db/db_policy.xml", "a+");
fseek(xml_file, -14, SEEK_END);

ftruncate(fileno(xml_file),ftello(xml_file));





fprintf(xml_file, "\n<policyDecryption>\n",);
fprintf(xml_file, " <policyName>%s</policyName>\n", polname);
fprintf(xml_file, " <deviceName>%s</deviceName>\n", device);
fprintf(xml_file, " <status>%s</status>\n", status);
fprintf(xml_file, " <srcZone>%s</srcZone>\n", srczone);
fprintf(xml_file, " <dstZone>%s</dstZone>\n", dstzone);
fprintf(xml_file, " <srcAddr>%s</srcAddr>\n", srcaddr);
fprintf(xml_file, " <dstAddr>%s</dstAddr>\n", dstaddr);
fprintf(xml_file, " <srcUser>%s</srcUser>\n", srcuser);
fprintf(xml_file, " <application>%s</application>\n", app);
fprintf(xml_file, " <service>%s</service>\n", service);
fprintf(xml_file, " <urlCategory>%s</urlCategory>\n", urlcategory);
fprintf(xml_file, " <action>%s</action>\n", action);
fprintf(xml_file, " </policyDecryption>\n");
fprintf(xml_file, "</policyList>");

fclose(xml_file);
send(new_socket, hello, 17, 0);
printf("\nSocket: Sent data to the client!\n");


}
}

return 0;
}

当我尝试在不使用宏的情况下将数据发送到该服务器时,它也会打印宏,现在我已经添加了第二个宏以及后面的代码,客户端被卡住并且无法打印/写入数据库发生,我必须使用 Ctrl+C 结束执行才能停止。

这是解决我的问题的正确方法吗?我做错了什么吗?我知道这不是完成这项工作的最佳方式,因此欢迎任何和所有建议。

客户端是基于Python的CGI脚本,它从UI页面获取动态数据并将该数据作为字符串发送。我将“服务器”和“策略”附加为两个不同的宏,应该读取它们并相应地执行以下代码。

最佳答案

具有多个连接的 C++ ssl 服务器 https://github.com/breakermind/cppSockets/tree/master/SslServerExample

Working example with C++ fork() function or with threads.

fork() - server create new prossess for each new connection

thread() - server create new thread for each new connection

您只需更改此函数/方法中的部分

void ServerLoop(SSL *ssl, string ipAddress){ }

您可以测试来自 openssl 客户端的连接

openssl s_client -connect hostname:999

如果连接到 smtp 服务器则

openssl s_client -connect hostname:25 -starttls smtp

关于c - 通过 C 语言的套接字编程处理多个客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47431265/

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