绑定:地址已在使用中
我正在尝试运行我的服务器程序,但我不断收到此错误:
绑定时出错:地址已在使用中
int main(int argc, char *argv[]){
if (argc < 6){
printf("usage: stringSearcher <filename> <stringLength> <searchLength> <nChildren> <nThreads> <blockSize>\n");
exit(0);
}
char* fileName = argv[1];
int stringLen = atoi(argv[2]);
int searchLen = atoi(argv[3]);
nChildren = atoi(argv[4]);
if (nChildren == 0)
nChildren = 1;
int nThreads = atoi(argv[5]);
int blockSize = atoi(argv[6]);
int childNo = 0;
//Opens new socket
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
perror("ERROR opening socket");
exit(0);
}
int newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
portno = 5000;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
int opt = 1;
setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
perror("ERROR on binding\n");
exit(1);
}
I'm trying to run my server program but I keep getting this error:
ERROR on binding: Address already in use
int main(int argc, char *argv[]){
if (argc < 6){
printf("usage: stringSearcher <filename> <stringLength> <searchLength> <nChildren> <nThreads> <blockSize>\n");
exit(0);
}
char* fileName = argv[1];
int stringLen = atoi(argv[2]);
int searchLen = atoi(argv[3]);
nChildren = atoi(argv[4]);
if (nChildren == 0)
nChildren = 1;
int nThreads = atoi(argv[5]);
int blockSize = atoi(argv[6]);
int childNo = 0;
//Opens new socket
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
perror("ERROR opening socket");
exit(0);
}
int newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
portno = 5000;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
int opt = 1;
setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
perror("ERROR on binding\n");
exit(1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个参数必须是 sockfd 而不是“1”
1st argument must be sockfd and not '1'
一个优秀的winsock开发工具是TCPView。
它可以轻松查看哪个进程正在侦听哪个端口、套接字状态等。
an excellent tool for winsock development is TCPView.
it makes it simple to see which process is listening on which port, socket status, etc.