通过 C 中的套接字编程处理多个客户端
Posted
技术标签:
【中文标题】通过 C 中的套接字编程处理多个客户端【英文标题】:Handling multiple clients through socket programming in C 【发布时间】:2017-11-22 09:42:28 【问题描述】:我目前正在使用 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 页面获取动态数据并将该数据作为字符串发送。我将“服务器”和“策略”附加为两个不同的宏,它们应该被读取并相应地执行以下代码。
【问题讨论】:
我认为您需要退后几步并get a couple of good beginners books。像token == "server"
这样的表达式不会像你想象的那样做。
比较字符串:***.com/questions/8004237/…
其实你已经不远了。大多数是正确的C,除了比较字符串,需要使用strcmp
,例如if( strcmp(token, "server")==0 )
。
这对伙计们有很大帮助,我很感谢 @Someprogrammerdude 实际上告诉我要通读书籍以增强我的基础知识。 Klas Lindback 和 Paul Ogilvie 感谢您指出我的错误。一旦我实施了这些建议,我会回复你们。
@DevanshuMisra:等到 Nagle 介入。否则您的 IP 帧会碎片化。
【参考方案1】:
具有多个连接的 C++ ssl 服务器https://github.com/breakermind/cppSockets/tree/master/SslServerExample
使用 C++ fork() 函数或线程的工作示例。
fork() - 服务器为每个新连接创建新进程
thread() - 服务器为每个新连接创建新线程
你只需要改变这个函数/方法的一部分
void ServerLoop(SSL *ssl, string ipAddress)
您可以从 openssl 客户端测试连接
openssl s_client -connect hostname:999
如果连接到 smtp 服务器则
openssl s_client -connect hostname:25 -starttls smtp
【讨论】:
以上是关于通过 C 中的套接字编程处理多个客户端的主要内容,如果未能解决你的问题,请参考以下文章