目前,我正在构建一个API,以便我可以使用创建的函数轻松连接到套接字。
目前,我有一个代码:
int main(int argc, char *argv[]) {
...
//Assume bind() and listen() successfully returned.
//print message to show connection has been made
printf("Ready and Listening...\n");
//keep server alive with while loop
while(1) { ///////This is where a thread comes in and accept() connection
//accept incoming connections
connection = (connection_t *)malloc(sizeof(connection_t));
connection->addr_len = sizeof(&connection->address);
connection->sock = accept(socket_file_descriptor, (struct sockaddr *)&connection->address, &connection->addr_len);
connection->addr = (long)((struct sockaddr_in *)&connection->address)->sin_addr.s_addr;
connection->client_port_number = ntohs(((struct sockaddr_in *)&connection->address)->sin_port);
if(getsockname(connection->sock,&connection->address, &connection->addr_len) == -1) {
printf("Error getting client socket address: %s\n", strerror(errno));
} else {
//client_port_number = ntohs((struct sockaddr_in *)&connection->address).sin_port);
}
if(connection->sock <= 0) {
error("ERROR on accept");
free(connection);
} else {
//pthread is created so that each connection established is managed by individual thread, and pthread function is invoked here
if(pthread_create(&thread, 0, pthread, (void *)connection) < 0)
error("ERROR creating thread");
printf("Thread Created... Listening on Client:[%d.%d.%d.%d:%d]\n",
(int)((connection->addr ) & 0xff),
(int)((connection->addr>> 8) & 0xff),
(int)((connection->addr>>16) & 0xff),
(int)((connection->addr>>24) & 0xff),
connection->client_port_number);
pthread_detach(thread);
}
}
return 0;
}
while循环是我的服务器开始接受连接的地方。 如果建立了任何连接,则调用(void * pthread)函数来处理单个连接。
这里是参考的线程函数:
void * pthread(void * ptr) {
char buffer[256];
int n;
char m1[2], m2[10];
connection_t *conn;
char *fname;
if(!ptr) pthread_exit(0);
conn = (connection_t *) ptr;
bzero(buffer,256);
//read message
n = read(conn->sock, buffer, 255);
sscanf(buffer,"%[^ ] %s",m1,m2);
printf("%s\n",m2);
if(n > 0) {
//SEND TO CLIENT
if(strcmp(m1, "get") == 0) {
if (strcmp(m2, "file") == 0) {
fname = "tosendfromserver.txt";
sender(fname,conn->sock);
printf("file [\"%s\"] has been sent to [%d.%d.%d.%d:%d]\n", fname,
(int)((conn->addr ) & 0xff),
(int)((conn->addr >> 8) & 0xff),
(int)((conn->addr >> 16) & 0xff),
(int)((conn->addr >> 24) & 0xff),
conn->client_port_number);
}
}
}
close(conn->sock);
free(conn);
pthread_exit(0);
}
因此,pthread函数处理连接,并发送客户端响应。 我现在正在修改它,以便我可以使用简单的函数:
int main(int argc, char * argv[]) { //argv[0] == server //argv[1] == port number
listenv = setServer(argv[1],&serverAddress,&sfd);
//int port = ntohs(serverAddress.sin_port);
printf("sfd = %d port = %d\n", sfd, ntohs(serverAddress.sin_port));
printf("listenv = %d\n",listenv);
while (listenv == 0) {
connection_t conn;
if (connector(&conn,&sfd) == 1){
}
}
}
其中connector()允许我使用线程连接到连接,如上面的代码所示。
我如何实现connector()以允许这种情况发生?
之后,我想做类似的事情:
while (listenv == 0) {
connection_t conn;
if (**connector(&conn,&sfd**) == 1){
n = read(conn->sock, buffer, 255);
sscanf(buffer,"%[^ ] %s",m1,m2);
printf("%s\n",m2);
if(n > 0) {
//SEND TO CLIENT
if(strcmp(m1, "get") == 0) {
if (strcmp(m2, "file") == 0) {
fname = "tosendfromserver.txt";
sender(fname,conn->sock);
printf("file [\"%s\"] has been sent to [%d.%d.%d.%d:%d]\n", fname,
(int)((conn->addr ) & 0xff),
(int)((conn->addr >> 8) & 0xff),
(int)((conn->addr >> 16) & 0xff),
(int)((conn->addr >> 24) & 0xff),
conn->client_port_number);
}
}
}
任何人都可以帮我解决这个问题吗?