Ejemplo n.º 1
0
int main(int argc, char **argv) {
	struct sockaddr_in clientAddr;
	int servSock;
	int clientSock;

	servSock = socket(AF_INET, SOCK_STREAM, 0);
	if(myBind(servSock, NULL, 8000) != 0) {
		perror("bind");
		return 1;
	}
	listen(servSock, 10);

	socklen_t len = sizeof(clientAddr);
	int size;
	char buf[101];
	while(1) {
		clientSock = accept(servSock, (struct sockaddr *)&clientAddr, &len);
		
		if(clientSock > 0) {
			printf("Accepted\n");
			SetRecvTimeOut(clientSock, 5, 0);
			size = recv(clientSock, buf, 100, 0);
			if(size <= 0) {
				if(errno == EAGAIN) {
					printf("EAGAIN\n");
				}
				else {
					printf("errno=%d, %s\n", errno, strerror(errno));
				}
			}

			else {
				buf[size] = 0;
				printf("Recv %s\n", buf);
			}
	//		DoClient(clientSock);		
			close(clientSock);
		}
	}
}
Ejemplo n.º 2
0
/*Test Recplayer  UDP    Demo*/
static int RecPlayProcess(int *puserIndex)
{
	int nLen;
	MsgHeader header, *pheader, sd_header;
	unsigned char szData[1280];
	int fileflags;

	int userIndex = *puserIndex;

	if(userIndex >= gLiveMaxUserNum || userIndex < 0) {
		printf("Exit RecPlayProcess Thread!!!!\n");
		pthread_detach(pthread_self());
		return 0;
	}

	LIVE_SOCKET socket = getRecuserTcpSocket(userIndex);

	printf("enter live RecPlayProcess() function!!\n");
	SetSendTimeOut(socket, 10);
	SetRecvTimeOut(socket, 10);
	memset(&header, 0, sizeof(header));
	pheader = &header;
	sd_header.sLen = htons(sizeof(MsgHeader));
	sd_header.sVer = MSG_VER;
	sd_header.sMsgType = MSG_TYPE_PASSWORD;

	if((fileflags = fcntl(socket, F_GETFL, 0)) == -1) {
		printf("fcntl F_GETFL error:%d,error msg: = %s\n", errno, strerror(errno));
		return -1;
	}

	if(fcntl(socket, F_SETFL, fileflags & (~O_NONBLOCK)) == -1) {
		printf("fcntl F_SETFL error:%d,error msg: = %s\n", errno, strerror(errno));
		return -1;
	}

	//	fcntl(socket, F_SETFL, O_NONBLOCK); 		// 是否为阻塞版本的socket

	while(liveplayTaskStatus == RUNNING) {
		memset(szData, 0, 1280);
		nLen = recv(socket, szData, HEAD_LEN, 0);

		if(nLen < HEAD_LEN || nLen == -1)	{
			printf("nLen < HEAD_LEN  errno = %d  nLen = %d\n", errno, nLen);
			goto EXITTHREAD;
		}

		memcpy(&header, szData, HEAD_LEN);

		pheader->sLen = ntohs(pheader->sLen);

		if(pheader->sLen - HEAD_LEN > 0) {
			nLen = recv(socket, szData + HEAD_LEN, pheader->sLen - HEAD_LEN, 0);

			if(nLen < pheader->sLen - HEAD_LEN)	{
				fprintf(stderr, "nLen < nMsgLen -HEAD_LEN\n");
				goto EXITTHREAD;
			}
		}



		switch(pheader->sMsgType) {
			case MSG_TYPE_PASSWORD:					// 判断密码
				if(!(strncmp(LIVE_CONNECT_KEY_1, (char *)(szData + HEAD_LEN), pheader->sLen - HEAD_LEN)) ||
				   !(strncmp(LIVE_CONNECT_KEY_2, (char *)(szData + HEAD_LEN), pheader->sLen - HEAD_LEN))) {
					int ln = strlen(LIVE_SERVER_VERSION) + strlen(LIVE_MINPLAYER_VERSION) + 4;
					char bver[40], buffer[100];
					int userID = 0;
					time_t timep;

					sprintf(bver, "%s %s", LIVE_SERVER_VERSION, LIVE_MINPLAYER_VERSION);
					sd_header.sData = 0; //密码正确
					sd_header.sLen = htons(sizeof(MsgHeader) + ln - 2);
					send(socket, &sd_header, sizeof(MsgHeader), 0);
					send(socket, bver, ln - 2, 0);
					sd_header.sLen = htons(sizeof(MsgHeader) + 4);
					sd_header.sVer = MSG_VER;
					sd_header.sMsgType = MSG_TYPE_ID;

					time(&timep);
					userID = (int)timep;
					fprintf(stderr, "------ userId = %x\n", userID);
					recplayEnv.recUser[userIndex].userID = userID;
					memcpy(buffer, &sd_header, sizeof(MsgHeader));
					memcpy(buffer + sizeof(MsgHeader), &userID, 4);
					send(socket, buffer, sizeof(MsgHeader) + 4, 0);
				} else {
					sd_header.sData = 1; //密码错误
					printf("passwd error!!!\n");
					send(socket, &sd_header, sizeof(MsgHeader), 0);
					goto EXITTHREAD;
				}

				break;

			case MSG_TYPE_HEART:
				DEBUG(DL_DEBUG, gLiveDebug_Level, "liveplay Heat  OK!!\n");
				break;

			case MSG_TYPE_USEINFO:
				DEBUG(DL_DEBUG, gLiveDebug_Level, "MSG_TYPE_USEINFO\n");
				break;

			case MSG_TYPE_ID:
				DEBUG(DL_DEBUG, gLiveDebug_Level, "MSG_TYPE_ID \n");
				break;

			case MSG_TYPE_BDPROXY:
				DEBUG(DL_DEBUG, gLiveDebug_Level, "MSG_TYPE_BDPROXY \n");
				break;

			case MSG_TYPE_CTRL: {
#if 0
				printf("MSG_TYPE_CTRL");
				unsigned char type, speed, num;
				type = *(szData + HEAD_LEN);
				speed = *(szData + HEAD_LEN + 1);
				num = *(szData + HEAD_LEN + 2);
				CameraControl((int)type, (int)speed);
#endif
			}
			break;

			default:
				DEBUG(DL_DEBUG, gLiveDebug_Level, "default Exit !!! pheader->sMsgType  = %d \n", pheader->sMsgType);
				break;
		}

	}

EXITTHREAD:
	close(socket);
	setUserLogout(userIndex);
	removeRecuserIndex(userIndex);
	clearRecuserNum();
	printf("(((((((((((((((((((((((((((((Exit RecPlayProcess Thread!!!!))))))))))))))))))))))))))))\n");
	pthread_detach(pthread_self());
	return 0;
}