我正在努力尝试通过for循环创建套接字;循环将为每个套接字创建一个线程。
这是我目前正常运作的代码:
输入; ./client1 2344
或./client1 2343
。 (2344和2343是港口)。
处理套接字,每个都有一个线程:
struct sockaddr_in serv; /* socket info about our server */
struct sockaddr_in serv2; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
int mysocket2;
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv.sin_port = htons(PORTNUM);
memset(&serv2, 0, sizeof(serv2)); /* zero the struct before filling the fields */
serv2.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv2.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv2.sin_port = htons(PORTNUM2); /* set the server port number */
// see http://stackoverflow.com/questions/15976137/local-host-server-socket-program-not-working
mysocket = socket(AF_INET, SOCK_STREAM, 0);
mysocket2 = socket(AF_INET, SOCK_STREAM, 0);
int optVal = 1;
if (setsockopt(mysocket, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
perror("Error setting socket option");
exit(1);
}
if (setsockopt(mysocket2, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
perror("Error setting socket option");
exit(1);
}
if (bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))){
perror("Error binding socket to port");
exit(1);
}
if (bind(mysocket2, (struct sockaddr *)&serv2, sizeof(struct sockaddr_in))){
perror("Error binding socket to port");
exit(1);
}
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, SOMAXCONN);
listen(mysocket2, SOMAXCONN);
pthread_t inc_x_thread1;
pthread_t inc_x_thread2;
pthread_create(&inc_x_thread1, NULL, run, &mysocket);
pthread_create(&inc_x_thread2, NULL, run, &mysocket2);
pthread_join(inc_x_thread1, NULL);
pthread_join(inc_x_thread2, NULL);
以下代码是我尝试在for循环中重新分解代码:
int socketDeckPair = 0;
int threads = 2;
pthread_t* tid[2];
char* ports[] = {"2343", "2344"};
//This is where the information about the incoming connection will
for(;socketDeckPair < 2; socketDeckPair=socketDeckPair+1){
struct sockaddr_in serv; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv.sin_port = htons(atoi(ports[socketDeckPair])); /* set the server port number */
mysocket = socket(AF_INET, SOCK_STREAM, 0);
int optVal = 1;
if (setsockopt(mysocket, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) < 0) {
perror("Error setting socket option");
exit(1);
}
if (bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))){
perror("Error binding socket to port");
exit(1);
}
listen(mysocket, SOMAXCONN);
pthread_create(&tid[socketDeckPair], NULL, run, &mysocket);
}
int i = 0;
for(; i < threads; i++){
pthread_join(tid[i], NULL);
}
现在,如果我尝试运行./client1 2344
,我会得到奇怪的结果。每次我运行./client1
线程ID的替代:
f77fe700
f6dfd700
f77fe700
f6dfd700
f77fe700
我做错了什么?
感谢 丹尼尔
答案 0 :(得分:2)
问题1:
pthread_t* tid[2];
必须是:
pthread_t tid[2];
问题2:
在for
循环内的以下行中:
pthread_create(&tid[socketDeckPair], NULL, run, &mysocket);
当您的代码到达下一个mysocket
(即下一行)时,您正在使用超出范围的本地变量}
。
您正在传递变量的地址,即在下一个}