3

此代码正在生成“连接失败错误”,(错误生成部分在代码下方注释)即使我提供正确的输入格式,例如。

./客户端ip文本端口号

./客户端 127.0.0.1 "tushar" 7100

//AUTHOR: TUSHAR MAROO
    //Client.c
    //header files used
    #include <stdio.h> 
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <netinet/in.h>


//constants
#define RCVBUFFERSIZE 32
//functions used
void DieWithError(char *errorMessage);

//main program
int main(int argc, char *argv[]){
int sock;
struct sockaddr_in serverAddr;
unsigned short serverPort;
char *serverIp;
char *message;
unsigned int messageLength;
char buffer[RCVBUFFERSIZE];

//condition check deplyed for nuber of arguements not for data in arguements
if((argc<3) || (argc>4)){
    fprintf(stderr,"Format: %s <Server's IP> <Your Message> <Port Number>\n",argv[0]);
    exit(1);
}

serverIp = argv[1];
message = argv[2];

if(argc == 4){
    serverPort = atoi(argv[3]);
} else {
    serverPort = 7;
}

//create a socket and check success and handle error
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
    fprintf(stderr, "Socket Creation Fail");

//server details
//bzero((struct sockaddr_in *)(&serverAddr),sizeof(serverAddr));
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(serverIp);
serverAddr.sin_port = htons(serverPort);
printf("tusharmaroo");

//not working why??
//if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    //DieWithError("Connection Error..");
    //fprintf(stderr,"Connection error");
//this snippet also not working 
if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    DieWithError("connect() failed");

printf("connected....");
messageLength = strlen(message);
if(send(sock, message, messageLength, 0) > 0)
    printf("message sent....");
    
    
close(sock);    
exit(0);
}

//AUTHOR TUSHAR MAROO
//SERVER CODE
//header files
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>

//constants declared
#define ALLOWEDCONNECTIONS 5

//external functions
void DieWithError(char *error);
void ClientHandle(int sock);

//main code
int main(int argc, char argv[]){

    int serverSock;
    int clientSock;
    struct sockaddr_in serverAddr;
    struct sockaddr_in clientAddr;
    unsigned int serverPort;
    unsigned int clientLength;
    
    if(argc != 2){
        fprintf(stderr,"Format: %d <Port No.>", argv[0]);
        //DieWithError("Pass Correct Number of Arguements...");
        exit(1);
    }
    
    if((serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        DieWithError("Socket not Created");
        exit(1);
    }
    
    serverPort = htons((argv[1]));
    //assign address to the server
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = htons(serverPort);
    
    //socket has been created now bind it to some ip and port
    if((bind(serverSock,(struct sockaddr *)&serverAddr,sizeof(serverAddr))) < 0){
        DieWithError("Binding Failed");
    }
    
    if(listen(serverSock,5) < 0){
        DieWithError("Listen Failed");
    }
    for(;;){
        clientLength = sizeof(clientAddr);
        if((clientSock = accept(serverSock, (struct sockaddr *) &clientAddr, &clientLength)) < 0){
            DieWithError("Accept() failed");
            exit(1);
        }
        
        printf("Handling Client %s ",inet_ntoa(clientAddr.sin_addr));
    
    }
return 0;   
}
4

3 回答 3

1

这在服务器代码中是错误的

 serverPort = htons((argv[1]));

这应该是

serverPort = htons(atoi(argv[1]));
于 2013-04-29T15:05:59.827 回答
0

为我工作

客户端和服务器是 ubuntu 12.04

对于服务器,在 shell 中运行

nc -l 9999

这是在地址为“192.168.56.13”的主机上

对于客户端,编译上面的代码并修复“DieWithError”

void DieWithError(char *errorMessage) {  printf("%s",errorMessage); exit(1); }

cc -o foo foo.c
./foo 192.168.56.13 "hello" 9999</strike>

用 perror() 替换 DieWithError() 然后我猜它会打印出“连接被拒绝”,因为您似乎在让服务器在正确的地址上运行时遇到了网络问题。

但是,如果您的客户端中的地址正确,则 nc 程序将打印“hello”

你刚刚改变了你的程序,以前的版本对我有用。现在的版本,不知道有没有。

就像其他人所说的那样,使用 perror() 来获得正确的诊断

于 2013-04-29T14:46:10.747 回答
0

您确定没有防火墙规则给您带来麻烦吗?确保这件事。

如果连接失败,您应该能够使用 perror 或 strerror 打印出错误:

perror("Could not connect:");
于 2013-04-29T14:43:26.093 回答