#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const
int process_number=4;
const
int thread_number=100;
pthread_mutex_t mutex;
int thread_index=thread_number;
void* thread_function(void* args)
char data[512];
bzero(data, sizeof(data));
snprintf(data, 512, "%ld", random());
buf<<"send data size is:"
char buffer[1024];
struct sockaddr_in receive_addr;
socklen_t len = sizeof(sockaddr_in);
buf<<"recvice data:";
std::size_t actual_len = recvfrom(my_socket, buffer, 1024, 0, (sockaddr*)&receive_addr, &len);
if (actual_len == -1)
else
pthread_mutex_lock(&mutex);
thread_index--;
buf<<"/n"
<"thread exit/n/n";
printf("%s", buf.str().c_str());
pthread_mutex_unlock(&mutex);
close(my_socket);
}int main(int argc, char* argv)
*/std::cout<" process start"
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = af_inet;
server_addr.sin_port = htons(1120);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
srand((unsigned long)time(null));
pthread_t pt[thread_number];
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0)
if (pthread_attr_setstacksize(&attr, pthread_stack_min*2) != 0)
if (pthread_mutex_init(&mutex, null) != 0)
int i;
for (i=0; i
}pthread_mutex_lock(&mutex);
std::cout<<"create thread number is:"
<"rest thread number:"
for (int j=0; j
//getchar();
pthread_mutex_destroy(&mutex);
//dup2(old_std_out, stdout_fileno);
std::cout<"process exit"
}
udp併發伺服器程式之客戶端測試 初步
include include include include include include include include include include include const int thread number 1000 pthread mutex t mutex void thread...
UDP 客戶端伺服器
udp 客戶端 include include include include include define size 100 define ip 127.0.0.1 define port 10086 int main struct sockaddr in addr 建立socket udp so...
socket實現Udp併發伺服器和客戶端
tcp 通訊 udp通訊 面向連線的,可靠資料報傳輸。對於不穩定的網路層,採取完全彌補的通訊方式。丟包重傳。無連線的,不可靠的資料報傳遞。對於不穩定的網路層,採取完全不彌補的通訊方式。預設還原網路狀況 穩定 資料流量穩定 速度穩定 順序穩定 不穩定 資料流量不穩定 速度不穩定 順序不穩定 傳輸速度慢...