Ejemplo n.º 1
0
int main() {
	//用于丢包率的随机数种子
	srand(time(NULL));

	//启动重叠网络层并获取重叠网络层TCP套接字描述符	
	int son_conn = son_start();
	if(son_conn<0) {
		printf("fail to start overlay network\n");
		exit(1);
	}

	//初始化stcp客户端
	stcp_client_init(son_conn);

	//在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88.
	int sockfd = stcp_client_sock(CLIENTPORT1);
	if(sockfd<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd,SERVERPORT1)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1);
	
	//获取sendthis.txt文件长度, 创建缓冲区并读取文件中的数据.
	FILE *f;
	f = fopen("sendthis.txt","r");
	assert(f!=NULL);
	fseek(f,0,SEEK_END);
	int fileLen = ftell(f);
	fseek(f,0,SEEK_SET);
	char *buffer = (char*)malloc(fileLen);
	fread(buffer,fileLen,1,f);
	fclose(f);

	//首先发送文件长度, 然后发送整个文件.
	stcp_client_send(sockfd,&fileLen,sizeof(int));
    stcp_client_send(sockfd, buffer, fileLen);
	free(buffer);

	//等待一段时间, 然后关闭连接.
	sleep(WAITTIME);

	if(stcp_client_disconnect(sockfd)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd)<0) {
		printf("fail to close stcp client\n");
		exit(1);
	}
	
	//停止重叠网络层
	son_stop(son_conn);
}
Ejemplo n.º 2
0
int main() {
	//用于丢包率的随机数种子
	srand(time(NULL));

	//连接到SIP进程并获得TCP套接字描述符	
	int sip_conn = connectToSIP();
	if(sip_conn<0) {
		printf("fail to connect to the local SIP process\n");
		exit(1);
	}

	//初始化stcp客户端
	stcp_client_init(sip_conn);
	sleep(STARTDELAY);

	char hostname[50];
	printf("Enter server name to connect:");
	scanf("%s",hostname);
	int server_nodeID = topology_getNodeIDfromname(hostname);
	if(server_nodeID == -1) {
		printf("host name error!\n");
		exit(1);
	} else {
		printf("connecting to node %d\n",server_nodeID);
	}

	//在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88.
	int sockfd = stcp_client_sock(CLIENTPORT1);
	if(sockfd<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT1)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1);
	
	//获取sendthis.txt文件长度, 创建缓冲区并读取文件中的数据
	FILE *f;
	f = fopen("sendthis.txt","r");
	assert(f!=NULL);
	fseek(f,0,SEEK_END);
	int fileLen = ftell(f);
	fseek(f,0,SEEK_SET);
	char *buffer = (char*)malloc(fileLen);
	fread(buffer,fileLen,1,f);
	fclose(f);
	//首先发送文件长度, 然后发送整个文件.
	stcp_client_send(sockfd,&fileLen,sizeof(int));
    stcp_client_send(sockfd, buffer, fileLen);
	free(buffer);
	//等待一段时间, 然后关闭连接.
	sleep(WAITTIME);

	if(stcp_client_disconnect(sockfd)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd)<0) {
		printf("fail to close stcp client\n");
		exit(1);
	}
	
	//断开与SIP进程之间的连接
	disconnectToSIP(sip_conn);
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{

	//用于丢包率的随机数种子
	srand(time(NULL));

	//连接到SIP进程并获得TCP套接字描述符	
	int sip_conn = connectToSIP();
	if(sip_conn<0) {
		printf("fail to connect to the local SIP process\n");
		exit(1);
	}

	//初始化stcp客户端
	stcp_client_init(sip_conn);
	sleep(STARTDELAY);

	char hostname[50];
	printf("Enter server name to connect:");
	scanf("%s",hostname);
	int server_nodeID = topology_getNodeIDfromname(hostname);
	if(server_nodeID == -1) {
		printf("host name error!\n");
		exit(1);
	} else {
		printf("connecting to node %d\n",server_nodeID);
	}
	int number;
	printf("Enter client number to connect:");
	scanf("%d",&number);
	if(number ==1 ){
		sockfd = stcp_client_sock(CLIENTPORT1);
		if(sockfd<0) {
			printf("fail to create stcp client sock");
			exit(1);
		}
		if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT1)<0) {
			printf("fail to connect to stcp server\n");
			exit(1);
		}
		printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1);
	}
	else if(number ==2 ){
		sockfd = stcp_client_sock(CLIENTPORT2);
		if(sockfd<0) {
			printf("fail to create stcp client sock");
			exit(1);
		}
		if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT2)<0) {
			printf("fail to connect to stcp server\n");
			exit(1);
		}
		printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT2,SERVERPORT2);
	}
	else if(number ==3 ){
		sockfd = stcp_client_sock(CLIENTPORT3);
		if(sockfd<0) {
			printf("fail to create stcp client sock");
			exit(1);
		}
		if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT3)<0) {
			printf("fail to connect to stcp server\n");
			exit(1);
		}
		printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT3,SERVERPORT3);
	}
	else
	{	
		printf("client number error!\n");
		exit(1);	
	}
	pthread_t msgthd,mainthd;
	pthread_attr_t attr;

	pthread_mutex_init(&wait_mutex,NULL);
	pthread_cond_init(&wait_cond,NULL);
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
	memset(&send_msg,0,YY_MSG_HEADER_LENGTH+1024);
	memset(&rec_msg,0,YY_MSG_HEADER_LENGTH+1024);
	send_msg.protocol_name[0]='Y';
	send_msg.protocol_name[1]='Y';
	send_msg.protocol_name[2]='M';
	send_msg.protocol_name[3]='G';
	bool login=false;
	system("clear");
	printf("Welcome to NJUCS Instant Messenger Demo Program!\nPlease input Your user name to login(The name's length shouled be less than 20 letters;The name only contains letters and digits!)\n(c)cls,(#)exit\n");
	send_msg.service = 0X01;
	while(!login)
	{		
		//main menu
		char a;
		scanf("%c",&a);
		gets(buffer);
		if(strcmp(buffer,"#")==0)
			break;
		else if(strcmp(buffer,"c")==0)
		{
			system("clear");
			printf("Welcome to NJUCS Instant Messenger Demo Program!\nPlease input Your user name to login(The name's length shouled be less than 20 letters;The name only contains letters and digits!)\n(c)cls,(#)exit\n");
			continue;
		}
		else if(strlen(buffer)>20)
		{
			printf("The inputed name is too long!input again:\n");
			continue;
		}
		else
		{
			bool illegal=false;
			int i=0;
			for(i;i<strlen(buffer);i++)
			{
				if(!(('a'<=buffer[i]&& 'z'>=buffer[i]) ||('A'<=buffer[i] && 'Z' >=buffer[i])||('0'<=buffer[i] && '9'>=buffer[i])) )
				{
					printf("The input is illegal!input again:\n");
					illegal=true;
					break;
				}
			}
			if(illegal)
				continue;
		}
		strcpy(send_msg.send_usr,buffer);
		/*send the message to log in!*/
		int len = YY_MSG_HEADER_LENGTH;
		stcp_client_send(sockfd,&len,sizeof(int));
		stcp_client_send(sockfd,&send_msg,len);
		stcp_client_recv(sockfd,&len,sizeof(int));
		stcp_client_recv(sockfd,&rec_msg,len);
		switch(rec_msg.service)
		{
			case 0x11:/*the user is aleady exist*/
				printf("Sorry, The user %s already exists!input another:\n",rec_msg.send_usr);break;
			case 0x12:/*log in success!*/
				strncpy(usrname,buffer,20);
				if(pthread_create(&mainthd, &attr, handle_main, NULL )!= 0)
				{
					perror("Thread creation problem");
					exit(-1);
				}
				if(pthread_create(&msgthd, &attr, handle_msg, NULL)!= 0)
				{
					perror("Thread creation problem");
					exit(-1);
				}
				pthread_join(mainthd,NULL);
				pthread_join(msgthd,NULL);
				login=true;
				break;
			default: printf("message error!error code:0x%02x",rec_msg.service);break;
		}
	}
	pthread_attr_destroy(&attr);
	pthread_mutex_destroy(&wait_mutex);
	pthread_cond_destroy(&wait_cond);
	pthread_exit(NULL);
	disconnectToSIP(sip_conn);
}
Ejemplo n.º 4
0
int main() {
	//用于丢包率的随机数种子
	srand(time(NULL));

	//启动重叠网络层并获取重叠网络层TCP套接字描述符	
	int son_conn = son_start();
	if(son_conn<0) {
		printf("fail to start overlay network\n");
		exit(1);
	}

	//初始化stcp客户端, 传递TCP套接字描述符给STCP层, STCP将该套接字作为一个全局变量维护
	stcp_client_init(son_conn);

	//在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88
	int sockfd = stcp_client_sock(CLIENTPORT1);
	if(sockfd<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd,SERVERPORT1)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connect to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1);
	
	//在端口89上创建STCP客户端套接字, 并连接到STCP服务器端口90
	int sockfd2 = stcp_client_sock(CLIENTPORT2);
	if(sockfd2<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd2,SERVERPORT2)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connect to server, client port:%d, server port %d\n",CLIENTPORT2, SERVERPORT2);

	//等待一段时间, 然后关闭连接
	sleep(WAITTIME);

	if(stcp_client_disconnect(sockfd)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd)<0) {
		printf("failt to close stcp client\n");
		exit(1);
	}
	
	if(stcp_client_disconnect(sockfd2)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd2)<0) {
		printf("failt to close stcp client\n");
		exit(1);
	}

	//停止重叠网络层
	son_stop(son_conn);
}
Ejemplo n.º 5
0
int main() {
	//用于丢包率的随机数种子
	srand(time(NULL));

	//启动重叠网络层并获取重叠网络层TCP套接字描述符	
	int son_conn = son_start();
	if(son_conn<0) {
		printf("fail to start overlay network\n");
		exit(1);
	}

	//初始化stcp客户端
	stcp_client_init(son_conn);

	//在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88
	int sockfd = stcp_client_sock(CLIENTPORT1);
	if(sockfd<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd,SERVERPORT1)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1);
	
	//在端口89上创建STCP客户端套接字, 并连接到STCP服务器端口90
	int sockfd2 = stcp_client_sock(CLIENTPORT2);
	if(sockfd2<0) {
		printf("fail to create stcp client sock");
		exit(1);
	}
	if(stcp_client_connect(sockfd2,SERVERPORT2)<0) {
		printf("fail to connect to stcp server\n");
		exit(1);
	}
	printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT2, SERVERPORT2);

	//通过第一个连接发送字符串
    char mydata[6] = "hello";
	int i;
	for(i=0;i<5;i++){
      	stcp_client_send(sockfd, mydata, 6);
		printf("send string:%s to connection 1\n",mydata);	
      	}
	//通过第二个连接发送字符串
    char mydata2[7] = "byebye";
	for(i=0;i<5;i++){
      	stcp_client_send(sockfd2, mydata2, 7);
		printf("send string:%s to connection 2\n",mydata2);	
      	}

	//等待一段时间, 然后关闭连接
	sleep(WAITTIME);
	if(stcp_client_disconnect(sockfd)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd)<0) {
		printf("fail to close stcp client\n");
		exit(1);
	}
	
	if(stcp_client_disconnect(sockfd2)<0) {
		printf("fail to disconnect from stcp server\n");
		exit(1);
	}
	if(stcp_client_close(sockfd2)<0) {
		printf("fail to close stcp client\n");
		exit(1);
	}

	//停止重叠网络层
	son_stop(son_conn);
}