1. 程式人生 > >檢查源端口被占用

檢查源端口被占用

檢查源端口被占用

int socket_fd;

struct sockaddr_in sin;

socket_fd = socket(AF_INET,SOCK_STREAM,0);

if(socket_fd ==-1)

{

return -1;

}

bzero(&sin, sizeof(sin));

sin.sin_family = AF_INET;

sin.sin_port=htons(port);

inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr);


if(bind(socket_fd,(struct sockaddr*)&sin,sizeof(struct sockaddr))<0)

{

printf("The current sport is occupied !!");

return -1;

}


close(socket_fd);


return 0;


本文出自 “Linux_woniu” 博客,請務必保留此出處http://linuxcgi.blog.51cto.com/5925801/1981208

檢查源端口被占用