套接字编程中连接被拒绝错误

时间:2013-04-29 14:19:18

标签: c sockets

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

  

./ Client ip text portno

     

./ Client 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;   
}

3 个答案:

答案 0 :(得分:1)

服务器代码中的错误

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

这应该是

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

答案 1 :(得分:0)

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

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

perror("Could not connect:");

答案 2 :(得分: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()来获得适当的诊断