改错:)
//实现简单的TCP SERVER, 至少8个错误.
#include <sys/types.h>
#include <sys/socket.h>
const int PORT_NUM=10000;
int buggy_echo_server(void){
sockaddr s_addr;
int length; //1
char buf[BUFSIZ];
int s_fd, n_fd;
//Create a local endpoint of communication.
if (s_fd = socket(AF_INET, SOCK_DGRAM, 0) == -1)
return -1;
//Set up the address information to become a server.
//(2)
s_addr.sin_family = PF_UNIX;//(3)
s_addr.sin_port = PORT_NUM; // (4)
s_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(s_fd, (sockaddr*) &s_addr, sizeof s_addr) == -1)
perror("bind"), exit(1);
// (5)
// Create a new endppoint of communication.
if(n_fd=accept(s_fd, &s_addr, &length) != -1){
int n;
while ((n = read(s_fd, buf, sizeof buf)) > 0) //(7)
write(n_fd, buf,n); //(8)
//remainder omitted...
}
}