Ejemplo n.º 1
0
void ClientSocket::sendState()
{
	Unit* p = g.player;
	DataWriter w;
	w.writeByte(CLI_STATE);
	w.writeInt(p->movex);
	w.writeInt(p->movey);
	w.writeDouble(p->d);
	w.writeByte(p->shooting);
	w.writeInt(g.weapon);
	conn.write(w);
}
Ejemplo n.º 2
0
bool ClientSocket::connect(const char* host,const char* nick)
{
	hostent* serv = gethostbyname(host);
	if (!serv) {
		cout<<"gethostbyname failed\n";
		exit(16);
	}
	sockaddr_in addr;
	#ifdef WIN32
	int sockfd = socket(PF_INET, SOCK_STREAM, 0);
	#else
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	#endif
		#ifdef WIN32
	if (sockfd==INVALID_SOCKET) {
		cout<<"binding socket failed "<<WSAGetLastError()<<endl;
		exit(15);
	}
		#endif
	memset(&addr,0,sizeof(addr));
	addr.sin_family = AF_INET;
#ifdef WIN32
	addr.sin_addr.s_addr = ((struct in_addr *)(serv->h_addr))->s_addr;
//    memcpy(serv->h_addr, &addr.sin_addr.s_addr, serv->h_length);
#else
	bcopy(serv->h_addr, &addr.sin_addr.s_addr, serv->h_length);
#endif
	addr.sin_port = htons(SERVER_PORT);
	if (::connect(sockfd, (sockaddr*)&addr, sizeof(addr))<0) {
		perror("Connecting failed");
		#ifdef WIN32
		cout<<"connect error: "<<WSAGetLastError()<<'\n';
		#endif
		return 0;
	}
	#ifndef WIN32
	fcntl(sockfd, F_SETFL, O_NONBLOCK);
	#else
    unsigned long x=1;
    ioctlsocket(sockfd, FIONBIO, &x);
    char flag=1;
    if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag))<0) {
    	perror("setsockopt TCP_NODELAY");
    	exit(16);
    }
	#endif
	conn.fd = sockfd;

    DataWriter w;
    w.writeByte(CLI_NAME);
    char buf[33]={};
    strncpy(buf,nick,31);
    w.write(buf,32);
    conn.write(w);
	return 1;
}