Linux网络编程基础实验(19)
时间:2025-04-21
时间:2025-04-21
了解SOCKET编程,熟悉C/S模式服务器和客户端。
printf ("OK: Obtain Socket Despcritor sucessfully\n"); }
/* Fill the local socket address struct */
addr_local.sin_family = AF_INET;
// Protocol Family
addr_local.sin_port = htons(PORT);
// Port number
addr_local.sin_addr.s_addr = INADDR_ANY;
// AutoFill local address
bzero(&(addr_local.sin_zero), 8);
// Flush the rest of struct
/* Blind a special Port */
if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 ) {
printf ("ERROR: Cannot bind Port %d\n", PORT); return (0);
} else {
printf("OK: Bind the Port %d sucessfully\n",
PORT);
}
/* Listen remote connect/calling */
if(listen(sockfd,BACKLOG) == -1) {
printf ("ERROR: Cannot listen Port %d\n", PORT); return (0);
} else {
printf ("OK: Listening the Port %d sucessfully\n", PORT); }
while(1){
sin_size = sizeof(struct sockaddr_in);
/* Wait a connection, and obtain a new socket file despriptor for single connection */
if ((nsockfd = accept(sockfd, (struct sockaddr
*)&addr_remote, &sin_size)) == -1){
上一篇:用友系列产品报价表
下一篇:牙龈萎缩的治疗方法是什么