日期:2014-05-16 浏览次数:20656 次
int GeneralTCP(){
int socketId1,socketId2;
int length1,length2;
char *s1,*s2;
struct sockaddr_in remote_addr1,remote_addr2;
printf("The General has TCP port 4229 and IP address 127.0.0.1\n");
//set addresses of captain1 and captain2
remote_addr1.sin_family=AF_INET;
remote_addr2.sin_family=AF_INET;
remote_addr1.sin_addr.s_addr=inet_addr("127.0.0.1");
remote_addr2.sin_addr.s_addr=inet_addr("127.0.0.1");
remote_addr1.sin_port=htons(21629);
remote_addr2.sin_port=htons(21729);
//open sockets for captain1 and captain2
if((socketId1=socket(PF_INET,SOCK_STREAM,0))<0)
{
perror("Captain1 socket establish");
return 1;
}
//connect socket to remote addresses
if(connect(socketId1,(struct sockaddr *)&remote_addr1,sizeof(struct sockaddr_in))<0)
{
perror("Captain1 socket connect");
return 1;
}
//discrete which captain has higher chance
if(SR1 > SR2){
printf("<Captain1> has the highest probability of success\n");
s1 = "<Captain1> has started the Mission!!!!!!";
s2 = "<Captain2> is the back up for the mission!";
printf("Command to start the mission sent to <Captain1>\nCommand to be the backup for the mission sent to <Captain2>\n");
}
else{
printf("<Captain2> has the highest probability of success\n");
s1 = "<Captain1> is the back up for the mission!";
s2 = "<Captain2> has started the Mission!!!!!!";
printf("Command to start the mission sent to <Captain2>\nCommand to be the backup for the mission sent to <Captain1>\n");
}
//send messgage to captain1 and captain2 respectively
send(socketId1,s1,strlen(s1),0);
//close sockets of captain1 and captain2
close(socketId1);
if((socketId2=socket(PF_INET,SOCK_STREAM,0))<0)
{
perror("Captain2 socket establish");
return 1;
}
if(connect(socketId2,(struct sockaddr *)&remote_addr2,sizeof(struct sockaddr_in))<0)
{
perror("Captain2 socket connect");
return 1;
}
send(socketId2,s2,strlen(s2),0)