小编use*_*367的帖子

Python中的UDP客户端/服务器套接字

我是python和套接字的新手,我正在尝试编写一个回显客户端/服务器套接字.我编写了服务器,以便丢失30%的数据包.由于数据包可能丢失,我在一秒钟后将客户端编程为超时.但是,每当我运行客户端套接字时,我的输出都是100%REQUEST TIMED OUT.我假设我得到这个输出,因为我的服务器永远不会收到消息.我多次查看我的代码,无法弄清楚为什么我不断得到这个输出.下面是我的服务器和客户端套接字的代码.任何帮助,将不胜感激.

服务器套接字:

 # We will need the following module to generate randomized lost packets
    import random
    from socket import *

    # Create a UDP socket
    # Notice the use of SOCK_DGRAM for UDP packets
    serverSocket = socket(AF_INET, SOCK_DGRAM)

    # Assign IP address and port number to socket
    serverSocket.bind(('', 12000))

    while True:
        # Generate random number in the range of 0 to 10
        rand = random.randint(0, 10)

        # Receive the client packet along with the address it is coming …
Run Code Online (Sandbox Code Playgroud)

python sockets

15
推荐指数
2
解决办法
7万
查看次数

简单的餐饮哲学家使用pthreads

我正在研究餐饮哲学家计划.然而,我遇到了一个问题,即我的程序在所有哲学家都吃过之前停止了,我不明白为什么.这是我现在的代码:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void *func(int n);
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];

int main()
{
     int i;
     void *msg;
     for(i=1;i<=5;i++)
     {
          pthread_mutex_init(&chopstick[i],NULL);
     }
     for(i=1;i<=5;i++)
     {
          pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);
     }
     for(i=1;i<=5;i++)
     {
          pthread_join(philosopher[i],&msg);
     }
      for(i=1;i<=5;i++)
      {
          pthread_mutex_destroy(&chopstick[i]);
      }
     return 0;
}

void *func(int n)
{
     printf ("\nPhilosopher %d is thinking ",n);
     pthread_mutex_lock(&chopstick[n]);//when philosopher 5 is eating he takes fork 1 and fork 5
     pthread_mutex_lock(&chopstick[(n+1)%5]);
     printf ("\nPhilosopher %d is eating ",n);
     sleep(3);
     pthread_mutex_unlock(&chopstick[n]);
     pthread_mutex_unlock(&chopstick[(n+1)%5]);
     printf ("\nPhilosopher %d finished eating ",n);
}
Run Code Online (Sandbox Code Playgroud)

c pthreads dining-philosopher

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

dining-philosopher ×1

pthreads ×1

python ×1

sockets ×1