我有这个代码
Server:
//"sock" has already been created with no problem and binding as well
void Connection::bind_connection(int port){
cout << "Port " << port << endl;
server.sin_family = AF_INET ;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port);
/* Bind socket to address */
struct sockaddr *serverptr = ( struct sockaddr *) &server;
if ( bind ( sock , serverptr , sizeof(server) ) < 0) {
perror("Binding error"); exit(BE);
}
cout << "Server binding completed!" << endl;
}
struct sockaddr_in client ;
socklen_t clientlen ;
struct sockaddr * clientptr =( struct sockaddr *) &client ;
if ( listen (sock , MAX_CONNECTIONS) < 0) {perror("listening error"); exit(LE);}
cout << "Listening for connections to port " << port << endl;
while (1) {
/* accept connection */
if (( newsock = accept ( sock , clientptr , &clientlen ) ) < 0) {
perror("Accepting error"); cout << "Errno = " << errno << endl; exit(AE);
}
//Code
....
}
和
Client:
//struct sockaddr_in server; <--- declared as data member of class "Connection"
//"sock" has already been created with no problem
void Connection::do_connect(){
struct sockaddr *serverptr = ( struct sockaddr *) &server;
if ( connect ( sock , serverptr , sizeof (server) ) < 0)
perror("client connect"); exit(CE);
cout << "Client connecting to the server" << endl;
}
我得到以上消息。大多数代码是从课程幻灯片中复制的,但可以根据我的需要进行修改。谷歌搜索问题,似乎我在syscall上给出了错误的大小。知道问题出在哪里吗?提前致谢。
这些行总是退出:
if ( connect ( sock , serverptr , sizeof (server) ) < 0)
perror("client connect"); exit(CE);
您可能会开始考虑使用其他缩进方案。通常,每行不使用一个以上的语句。
对于上面的代码indent
,像这样的工具会像这样格式化它:
if (connect (sock, serverptr, sizeof (server)) < 0)
perror("client connect");
exit(CE);
这种格式使编程错误很明显。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句