Esempio n. 1
0
int ServerSocket::accept()throw(ChatException)
{
	int cfd;
	cfd=::accept(fd,NULL,0);  // ±íÃ÷ÊÇÈ«¾Öº¯Êý
	if(cfd==-1) throw ChatException("accept´íÎó");
	return cfd;
}
Esempio n. 2
0
void ClientSocket::initSocket() throw (ChatException)
{
    //建立socket
    fd = socket(AF_INET, SOCK_STREAM, 0);
    int r;
    if (fd == -1) {
        throw ChatException("socket错误");
    }
    //建立connect
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);

    r = ::connect(fd, (struct sockaddr*)&addr, sizeof(addr));
    if(r==-1) {
        close(fd);
        throw ChatException("connect错误");
    }
}
Esempio n. 3
0
void ServerSocket::initSocket()throw(ChatException)
{
	struct sockaddr_in addr;
	int r;
	fd=socket(AF_INET,SOCK_STREAM,0);
	if(fd==-1) throw ChatException("socket´íÎó");
	
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=inet_addr(ip);
	addr.sin_port=htons(port);
	r=bind(fd,(struct sockaddr*)&addr,sizeof(addr));
	if(r==-1) {
		close(fd);
		throw ChatException("bind´íÎó");
	}
	
	r=listen(fd,10);
	if(r==-1){
		close(fd);
		throw ChatException("listen´íÎó");
	}
}