错误消息是:
Connect: Socket operation on non-socket
这是服务器端代码:
if ((ListeningSocket = socket(AF_INET,SOCK_STREAM,0 )) == -1){ printf("socket Failed!\n"); exit(1); } else printf("Server: socket() is OK!\n"); ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(5000); ServerAddr.sin_addr.s_addr = INADDR_ANY; // any one for any network can connect memset(&(ServerAddr.sin_zero),'if(SendingSocket = socket(AF_INET,0) == -1) { printf("ClIEnt: socket() Failed!"); exit (1); } else printf("ClIEnt: socket() is OK!\n"); ClIEntAddr.sin_family = AF_INET; ClIEntAddr.sin_port = htons(5000); ClIEntAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); memset(&(ClIEntAddr.sin_zero),8); if ( connect (SendingSocket,sizeof(struct sockaddr)) == -1) { perror("Connect"); exit(1); } else { printf("ClIEnt: connect() is OK,got connected...\n"); printf("ClIEnt: Ready for sending and/or receiving data...\n"); }....',8); // if (bind (ListeningSocket,(struct sockaddr *)&ServerAddr,sizeof(struct sockaddr))==-1) { printf("Server: bind() Failed!\n"); exit (1); } else printf("Server: bind() is OK!\n"); if (Listen(ListeningSocket,5)== -1){ printf("Server: Error Listening on socket\n"); exit (1); } else{ printf("Server: Listen() is OK,I'm waiting for connections...\n"); printf("Server: accept() is ready...\n");} sin_size = sizeof(struct sockaddr_in); NewConnection = accept(ListeningSocket,(struct sockaddr *)&ClIEntAddr,(socklen_t *)&sin_size); printf("Server: accept() is OK...\n"); printf("Server: ClIEnt connected,ready for receiving and sending data...\n");............. //}
这是客户端代码:
if(SendingSocket = socket(AF_INET,0) == -1)解决方法 你缺少一个括号.代码
if((SendingSocket = socket(AF_INET,0)) == -1)
应该读
if(SendingSocket = (socket(AF_INET,0) == -1))
你写它的方式,就意味着
这意味着:将socket()结果与-1进行比较,给出0或1,并将其分配给SendingSocket;然后测试它是真还是假.因此,假设socket()调用成功,则SendingSocket可能为0.
总结以上是内存溢出为你收集整理的服务器/客户端c程序出错:“连接:非套接字上的套接字 *** 作”全部内容,希望文章能够帮你解决服务器/客户端c程序出错:“连接:非套接字上的套接字 *** 作”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)