日期:2014-05-16  浏览次数:20569 次

TCP 套接字 连接拒绝错误 实在想不到什么问题
本帖最后由 usc_su 于 2013-03-26 05:10:33 编辑
如题,请大家给点意见,下面是client部分代码:
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)