int main() { //用于丢包率的随机数种子 srand(time(NULL)); //启动重叠网络层并获取重叠网络层TCP套接字描述符 int son_conn = son_start(); if(son_conn<0) { printf("can not start overlay network\n"); } //初始化STCP服务器 stcp_server_init(son_conn); //在端口SERVERPORT1上创建STCP服务器套接字 int sockfd= stcp_server_sock(SERVERPORT1); if(sockfd<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd); //在端口SERVERPORT2上创建另一个STCP服务器套接字 int sockfd2= stcp_server_sock(SERVERPORT2); if(sockfd2<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd2); char buf1[6]; char buf2[7]; int i; //接收来自第一个连接的字符串 for(i=0;i<5;i++) { stcp_server_recv(sockfd,buf1,6); printf("recv string: %s from connection 1\n",buf1); } //接收来自第二个连接的字符串 for(i=0;i<5;i++) { stcp_server_recv(sockfd2,buf2,7); printf("recv string: %s from connection 2\n",buf2); } //sleep(WAITTIME); //关闭STCP服务器 if(stcp_server_close(sockfd)<0) { printf("can't destroy stcp server\n"); exit(1); } if(stcp_server_close(sockfd2)<0) { printf("can't destroy stcp server\n"); exit(1); } //停止重叠网络层 son_stop(son_conn); }
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); }
int main() { //用于丢包率的随机数种子 srand(time(NULL)); //启动重叠网络层并获取重叠网络层TCP套接字描述符 int son_conn = son_start(); if(son_conn<0) { printf("can not start overlay network\n"); } //初始化STCP服务器 stcp_server_init(son_conn); //在端口SERVERPORT1上创建STCP服务器套接字 int sockfd= stcp_server_sock(SERVERPORT1); if(sockfd<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd); //首先接收文件长度, 然后接收文件数据 int fileLen; stcp_server_recv(sockfd,&fileLen,sizeof(int)); printf("get file length: %d\n",fileLen); char* buf = (char*) malloc(fileLen); stcp_server_recv(sockfd,buf,fileLen); //int i=0; /*for(;i<fileLen;i++) { printf("%c",buf[i]); }*/ //将接收到的文件数据保存到文件receivedtext.txt中 FILE* f; f = fopen("receivedtext.txt","w"); fwrite(buf,fileLen,1,f); fclose(f); free(buf); sleep(WAITTIME); //关闭STCP服务器 if(stcp_server_close(sockfd)<0) { printf("can't destroy stcp server\n"); exit(1); } //停止重叠网络层 son_stop(son_conn); }
int main() { //用于丢包率的随机数种子 srand(time(NULL)); //启动重叠网络层并获取重叠网络层TCP套接字描述符 int son_conn = son_start(); if(son_conn<0) { printf("can not start overlay network\n"); } //初始化STCP服务器 stcp_server_init(son_conn); //在端口SERVERPORT1上创建STCP服务器套接字 int sockfd= stcp_server_sock(SERVERPORT1); if(sockfd<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd); //在端口SERVERPORT2上创建另一个STCP服务器套接字 int sockfd2= stcp_server_sock(SERVERPORT2); if(sockfd2<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd2); sleep(WAITTIME); //关闭STCP服务器 if(stcp_server_close(sockfd)<0) { printf("can't destroy stcp server\n"); exit(1); } if(stcp_server_close(sockfd2)<0) { printf("can't destroy stcp server\n"); exit(1); } //停止重叠网络层 son_stop(son_conn); }
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); }
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); }