我想知道如何:
将执行某种编程语言的脚本代码执行到其他编程语言服务器,例如:
我们在3台服务器之间创建连接,两台服务器具有相同的编程语言(C ++),另一台服务器具有不同的编程语言(PHP),然后,当c ++服务器执行某项操作时,它会通知另一台c ++服务器和php服务器也是。
答案 0 :(得分:1)
int startOthers (char* host, unsigned int port, char* msg) {
int sockfd;
struct sockaddr_in serv_addr;
sockfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd > 0) {
memset ((void *) &serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr (host);
serv_addr.sin_port = htons (port);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == 0) {
write(sockfd, msg, strlen(msg));
close (sockfd);
return 0;
}
}
return -1;
}
int startServer(unsigned int port) {
int n, sockfd, cl_sockfd;
socklen_t clilen;
char buffer[256 + 1];
struct sockaddr_in server_addr, client_addr;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd > 0) {
memset((void *) &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(port);
if (bind(sockfd, (sockaddr *) & server_addr, sizeof(server_addr)) == 0) {
listen(sockfd, 5);
clilen = sizeof(client_addr);
cl_sockfd = accept(sockfd, (struct sockaddr *) &client_addr, &clilen);
if (cl_sockfd > 0) {
while ( (n = read(cl_sockfd, buffer, 256)) <= 0); /* a client communicated with me */
/* Do something */
/* Inform others */
startOthers(/* other C++ host */, /* port */, buffer);
startOthers(/* php host */, /* port */, buffer);
close(cl_sockfd);
close(sockfd);
return 0;
}
}
close(sockfd);
}
return -1;
}
startOthers函数可以与pthreads一起使用,以同时启动多个连接。它也可以连接到使用php创建的套接字。
重要的是所有服务器都遵循协议进行通信,尽管它们的实现细节。