我目前正在使用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页面获取动态数据并将该数据作为字符串发送。我正在附加&#34;服务器&#34;和&#34;政策&#34;作为两个应该被读取的不同的宏,以及相应的以下代码。
答案 0 :(得分:0)
具有多个连接的C ++ ssl服务器https://github.com/breakermind/cppSockets/tree/master/SslServerExample
使用C ++ fork()函数或使用线程的工作示例。
fork() - 服务器为每个新连接创建新的prossess
thread() - 服务器为每个新连接创建新线程
您只需要更改此功能/方法的一部分
void ServerLoop(SSL *ssl, string ipAddress){ }
您可以测试来自openssl客户端的连接
openssl s_client -connect hostname:999
如果连接到smtp服务器,那么</ p>
openssl s_client -connect hostname:25 -starttls smtp