so habe ich einen sehr einfachen TCP-Server, der zwei Threads auf zwei separate Ports hören. Wenn der Code jedoch ausgeführt wird, wird der zweite Thread nie erstellt, und es scheint, dass der erste Thread das gesamte Programm daran hindert, den nächsten Thread zu erstellen. Hier ist der Code ist ... (ganz einfach)Raspberry PI Multithread Socket Ausgabe C
void *Listener1();
void *Listener2();
int main()
{
pthread_t thread1, thread2;
pthread_create(&thread1, NULL,Listener1(), NULL);
pthread_create(&thread2, NULL,Listener2(), NULL);
sleep(50);
return 0;
}
void *Listener1()
{
int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
puts("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(12346);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
puts("ERROR on binding");
listen(sockfd,1);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
puts("ERROR on accept");
else
puts("Client connected!");
close(newsockfd);
close(sockfd);
return 0;
}
void *Listener2()
{
puts("hi)");
int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
puts("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(12345);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
puts("ERROR on binding");
listen(sockfd,1);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
puts("ERROR on accept");
else
puts("Client connected!");
close(newsockfd);
close(sockfd);
return 0;
}